# \[solved\] Conditional builds / multiple \`when\` clauses

**URL:** <https://drone.discourse.group/t/solved-conditional-builds-multiple-when-clauses/8885>\
**Category:** Drone Support\
**Created:** [February 25, 2017, 11:43am UTC](https://drone.discourse.group/t/solved-conditional-builds-multiple-when-clauses/8885 "2017-02-25T11:43:11Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![kasperlewau](https://yyz1.discourse-cdn.com/flex003/user_avatar/drone.discourse.group/kasperlewau/32/3967_2.png) [@kasperlewau](https://drone.discourse.group/u/kasperlewau)\
**Post date:** [February 25, 2017, 11:43am UTC](https://drone.discourse.group/t/solved-conditional-builds-multiple-when-clauses/8885/1 "2017-02-25T11:43:11Z")

</div>

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:

```auto
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:

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

```

Looking through the docs at [http://readme.drone.io/0.5/usage/conditional-builds/](http://readme.drone.io/0.5/usage/conditional-builds/) and [http://readme.drone.io/0.5/usage/conditional-build-steps/](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:

```auto
# 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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![bradrydzewski](https://yyz1.discourse-cdn.com/flex003/user_avatar/drone.discourse.group/bradrydzewski/32/3513_2.png) [@bradrydzewski](https://drone.discourse.group/u/bradrydzewski)\
**Post date:** [February 28, 2017, 12:09pm UTC](https://drone.discourse.group/t/solved-conditional-builds-multiple-when-clauses/8885/2 "2017-02-28T12:09:36Z")

</div>

You will need to declare two YAML steps for this:

```auto
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](https://learnxinyminutes.com/docs/yaml/).

---

<div class="post-metadata">

**Author:** ![kasperlewau](https://yyz1.discourse-cdn.com/flex003/user_avatar/drone.discourse.group/kasperlewau/32/3967_2.png) [@kasperlewau](https://drone.discourse.group/u/kasperlewau)\
**Post date:** [February 28, 2017, 12:57pm UTC](https://drone.discourse.group/t/solved-conditional-builds-multiple-when-clauses/8885/3 "2017-02-28T12:57:50Z")

</div>

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

Thank you!

---

<div class="post-metadata">

**Author:** ![kasperlewau](https://yyz1.discourse-cdn.com/flex003/user_avatar/drone.discourse.group/kasperlewau/32/3967_2.png) [@kasperlewau](https://drone.discourse.group/u/kasperlewau)\
**Post date:** [February 28, 2017, 1:07pm UTC](https://drone.discourse.group/t/solved-conditional-builds-multiple-when-clauses/8885/4 "2017-02-28T13:07:36Z")

</div>

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

```auto
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

```

---

<div class="post-metadata">

**Author:** ![kasperlewau](https://yyz1.discourse-cdn.com/flex003/user_avatar/drone.discourse.group/kasperlewau/32/3967_2.png) [@kasperlewau](https://drone.discourse.group/u/kasperlewau)\
**Post date:** [September 21, 2017, 9:37am UTC](https://drone.discourse.group/t/solved-conditional-builds-multiple-when-clauses/8885/5 "2017-09-21T09:37:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![laszlocph](https://yyz1.discourse-cdn.com/flex003/user_avatar/drone.discourse.group/laszlocph/32/3233_2.png) [@laszlocph](https://drone.discourse.group/u/laszlocph)\
**Post date:** [January 31, 2018, 1:28pm UTC](https://drone.discourse.group/t/solved-conditional-builds-multiple-when-clauses/8885/6 "2018-01-31T13:28:27Z")

</div>

It’s still the case with 0.8

Anchors would be lovely though.

---

<div class="post-metadata">

**Author:** ![laszlocph](https://yyz1.discourse-cdn.com/flex003/user_avatar/drone.discourse.group/laszlocph/32/3233_2.png) [@laszlocph](https://drone.discourse.group/u/laszlocph)\
**Post date:** [February 23, 2018, 8:02pm UTC](https://drone.discourse.group/t/solved-conditional-builds-multiple-when-clauses/8885/7 "2018-02-23T20:02:29Z")

</div>

Strike my last.

Worked on my Yaml a bit and this one works:

```auto
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

```

---

<div class="post-metadata">

**Author:** ![laszlocph](https://yyz1.discourse-cdn.com/flex003/user_avatar/drone.discourse.group/laszlocph/32/3233_2.png) [@laszlocph](https://drone.discourse.group/u/laszlocph)\
**Post date:** [February 23, 2018, 8:23pm UTC](https://drone.discourse.group/t/solved-conditional-builds-multiple-when-clauses/8885/8 "2018-02-23T20:23:43Z")

</div>

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

It says “Invalid or missing image”

```auto
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

```

---

<div class="post-metadata">

**Author:** ![yellowmegaman](https://yyz1.discourse-cdn.com/flex003/user_avatar/drone.discourse.group/yellowmegaman/32/3786_2.png) [@yellowmegaman](https://drone.discourse.group/u/yellowmegaman)\
**Post date:** [March 2, 2018, 12:33pm UTC](https://drone.discourse.group/t/solved-conditional-builds-multiple-when-clauses/8885/9 "2018-03-02T12:33:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![shaocongliang](https://avatars.discourse-cdn.com/v4/letter/s/e47c2d/32.png) [@shaocongliang](https://drone.discourse.group/u/shaocongliang)\
**Post date:** [November 10, 2018, 6:37pm UTC](https://drone.discourse.group/t/solved-conditional-builds-multiple-when-clauses/8885/10 "2018-11-10T18:37:37Z")

</div>

```auto

  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

```
