Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stage conditional on success of a run result #75

Closed
chrissimon-au opened this issue Sep 8, 2024 · 2 comments · Fixed by #76
Closed

Stage conditional on success of a run result #75

chrissimon-au opened this issue Sep 8, 2024 · 2 comments · Fixed by #76

Comments

@chrissimon-au
Copy link
Contributor

chrissimon-au commented Sep 8, 2024

I have a situation where it would be helpful to have a way to conditionally run a stage depending on the success of a run command (in my case, a grep).

I could just run the cmd, and if it fails, then the stage will fail, and I've had a look at using continueStageOnFailure which doesn't run the rest of the stage and move to the next stage - which is exactly what I want, but has a few issues:

  • It prints an error message (which really it should be treated the same as when' false)
  • other steps in the stage will not cause a pipeline failure if they fail, which they should

What it feels like I really need is something like whenRun which accepts the same arguments as run but which acts more like a when' command with the success result of the run being the when' argument.

I'm happy to take a look at opening a PR for this, but wanted to know first if you'd be happy to include it, and if you have any suggestions for implementation.

From a browse through the source it looks like it should go into ConditionsBuilder.fs. But there are quite a lot of _.run custom operations in StageBuilder in order to create all the possible uses of run - do I need to duplicate them all was whenRun variants, or is there an easier way you can see to achieve my goal?

@albertwoo
Copy link
Contributor

albertwoo commented Sep 9, 2024

@chrissimon-au PRs are welcomed.

whenRun is ok, but indeed it may duplicates alot of code. And not easy to extend.

What about when' with stage overload, for example:

pipeline "demo" {
   when' (stage "xxx" {
       run "xxxx"
   })
   ....
}

@chrissimon-au
Copy link
Contributor Author

Interesting idea, yes that would work, and provide more flexibility around managing the run. So in my case, it would be something like:

pipeline "demo" {

   stage "always runs" {
      echo "always'
   }

   stage "sometimes runs" {
      when` (stage "condition" {
         run "grep content file"
      })

      echo "you'll only see this if the grep found something"
   }

   stage "later stage" {
      echo "always"
   }
}

This would even look nicer if the stage is set to a variable earlier, e.g.

let contentInFile = stage "content In File" {
    run "grep content file"
}

pipeline "demo" {

   stage "always runs" {
      echo "always'
   }

   stage "sometimes runs" {
      when` contentInFile

      echo "you'll only see this if the content was found in the file"
   }

   stage "later stage" {
      echo "always"
   }
}

Since it's the when' block it would be composable into whenAny, whenAll etc. - nice!

Let me see if I can work out how to add it... will submit a PR or come back with more questions :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants