woodpecker-ci / woodpecker

Woodpecker is a simple, yet powerful CI/CD engine with great extensibility.
https://woodpecker-ci.org
Apache License 2.0
4.2k stars 364 forks source link

plugin pipeline conditionals #1591

Closed koehn closed 1 year ago

koehn commented 1 year ago

Clear and concise description of the problem

Here's what I'm trying to accomplish; following is a suggestion on how to do it that may/may not be The Right Way™ to do it.

I want a step in a pipeline that runs on a schedule using the soon-to-be-released cron scheduler, but I want to be able to limit the execution based upon the results of execution of a plugin I specify. I have written such a plugin (well, I copied the code from a GitHub Action). It returns ”true" if the build should proceed, else ”false".

Suggested solution

What I was thinking of in terms of usage was something like this, given an image koehn/foo that is built FROM an image debian:latest:

pipeline:
  build:
    image: woodpeckerci/plugin-docker-buildx
    secrets: [docker_username, docker_password]
    settings:
      repo: koehn/foo
      platforms: windows/amd64,darwin/amd64,darwin/arm64,freebsd/amd64,linux/amd64,linux/arm64/v8
    when:
      - event:
        cron: cron_job_name
      - condition:
          image: koehn/docker-update-image-checker:latest
          settings:
            base: debian:latest
            image: koehn/foo:latest
            platforms: windows/amd64,darwin/amd64,darwin/arm64,freebsd/amd64,linux/amd64,linux/arm64/v8

The build step would be considered when any of the events occur, only if all the conditions are met (i.e., return ”true").

The conditions would be executed in order, and the first to return non ”true” would prevent the build from proceeding.

Alternative

The example of how Github Actions does it is interesting (a context is passed from step to step throughout the build, JSON output from the step is added to the context, and expressions can be built from it), but more sophisticated and more disruptive to the pipeline taxonomy than what I'm proposing (read: it would break a lot of stuff).

Another alternative would be to use the exit code rather than stdout to connote the result of the condition.

A last alternative would be to extends the buildx plugin so that it would (based on a setting) check to see if the update needs to be performed, and only proceed in that case. This would require no change to the server, but would limit the utility considerably, as you’d need to merge all your conditional logic into a build plugin.

Additional context

No response

Validations

koehn commented 1 year ago

I thought of another way to achieve the result: allow a setting on a stop to stop the build if it returns a non-zero code, but don’t fail the build; simply end it:

when:
  - event:
    cron: cron_job_name

pipeline:
  check-if-base-image-changed:
    image: koehn/docker-update-image-checker:latest
    fail-on-bad-exit: false
    settings:
      base: debian:latest
      image: koehn/foo:latest
      platforms: windows/amd64,darwin/amd64,darwin/arm64,freebsd/amd64,linux/amd64,linux/arm64/v8

  build:
    image: woodpeckerci/plugin-docker-buildx
    secrets: [docker_username, docker_password]
    settings:
      repo: koehn/foo
      platforms: windows/amd64,darwin/amd64,darwin/arm64,freebsd/amd64,linux/amd64,linux/arm64/v8
anbraten commented 1 year ago

Seems to be a duplicate of: https://github.com/woodpecker-ci/woodpecker/issues/294

For the second part: https://woodpecker-ci.org/docs/next/usage/pipeline-syntax#failure