[solved] Conditional builds / multiple `when` clauses

Howdy ho!

I’ve been using Drone for a brief period of time now and am enjoying it a great deal, but I simply cannot figure out how to do the following:

  • Trigger my test runs on pull request hooks.
  • Trigger my test runs on push hooks from master only.

I gave the following a shot:

pipeline:
  test:
    image: node:latest
    commands:
      - npm install --no-optional --silent > /dev/null
      - npm run ci
    when: 
      event: push
      branch: master
    when:
      event: pull_request

However this appears to ignore the first when clause.

I believe I’m looking for something like:

  when: 
    - branch == 'master' && event == 'push'
    - branch != 'master' && event == 'pull_request' 

Looking through the docs at http://readme.drone.io/0.5/usage/conditional-builds/ and http://readme.drone.io/0.5/usage/conditional-build-steps/ I haven’t been able to figure out how to achieve what I need.


An initial workaround I used:

# first step of `test`
- if [ "$DRONE_COMMIT_BRANCH" != master ] && [ "$DRONE_BUILD_EVENT" = push ]; then exit 0; fi

I’ve since dropped that workaround and now run with a duplicate test clause:

pipeline:
  test:
    when: 
      branch: master
      event: push
  test:
    when: 
      event: pull_request

This does indeed do the job, but I would love to scratch the duplication.

You will need to declare two YAML steps for this:

pipeline:
  build_push:
    image: golang
    commands:
      - go test
    when:
      event: push
      branch: master
  build_pr:
    image: golang
    commands:
      - go test
      - go build
    when:
      event: pull_request
      branch:
        excludes: master

If you want to avoid duplicate configuration and keep things DRY you can use YAML anchors.

Ooh, I did not know about anchors. I will most definitely check those out (& keep my two separate steps).

Thank you!

And to close it all off: anchors worked brilliantly! Many thanks.

run_tests: &run_tests
  image: node:7.6.0
  commands:
    - npm install --no-optional --silent > /dev/null
    - npm run ci

pipeline:
  pr:
    <<: *run_tests
    when:
      event: pull_request
      branch:
        excludes: master
  push:
    <<: *run_tests
    when:
      event: push
      branch: master
4 Likes

Figured I’d necro this little issue.

drone-cli 0.7.0 doest not respect anchors anymore.

“Invalid or missing image”.

The build works fine on the server (also runing 0.7.0).

Stepping my drone-cli version down to 0.5.0-dev works dandy.

For now we’ve removed the anchors and gone back to a slightly less DRY approach.

It’s still the case with 0.8

Anchors would be lovely though.

Strike my last.

Worked on my Yaml a bit and this one works:

bb_image: &image debian:stable-slim
steps: &commands
  - echo "Hello"

pipeline:
  build_push:
    image: *image
    commands: *commands
    when:
      event: push
      branch: master
  build_pr:
    image: *image
    commands: *commands
    when:
      event: pull_request
      branch: master

Merging doesn’t work though. Can someone spot an error with my yaml?

It says “Invalid or missing image”

steps: &steps
  image: debian:stable-slim
  commands:
    - echo "hello"

pipeline:
  build:
    image: debian:stable-slim
    commands:
      - echo "hello"
  deploy_stage:
    <<: *steps
    when:
      branch: master
  deploy_prod:
    <<: *steps
    when:
      branch: prod

Same here. Would really love to be able to run two defined (in pipeline) actions against one environment using arguments.

like:

drone deploy repo build dev (for deployment)
drone deploy repo build dev --event restore-db-backup - separate set of steps for db restoration


  PREFIX: &id
    image: plugins/docker
    group: publish
    username: scliang
    password: scliang 
    registry: xxxx
    email: scliang@xxx.com
    insecure: true  

  dispatch:
    <<: *id
    repo: foo
    tag: 0.9.8
    dockerfile: bar