Drone seeming to not respect when condition - runs step anyway

  • Version of Drone: latest docker image as of last week
  • Github integration
  • Kubernetes runner

.drone.yml:

    ---
      kind: pipeline
      type: kubernetes
      name: default

      steps:
      - name: deploy-staging
        when:
          branch:
          - master
        image: node:latest
        commands:
        - bin/deploy.sh
      - name: validate-build
        when:
          event:
            - pull_request
        image: node:latest
        commands:
        - yarn build

What I’m trying to do:

  • Run the step called “deploy-staging” if the branch is master
  • Run ONLY the step called “validate-build” if the event is a PR.

What it does:

  • On a branch that isn’t a PR and isn’t master, just clones and passes right away :white_check_mark:This is working correctly
  • On a PR, source branch NOT named master of course, it runs both the deploy-staging and the validate-build step :disappointed:

Is there something wrong with what I’m doing here?
PS: I realize triggers could maybe be used to maybe prevent those extra “empty” builds from being made, but that’s not my main concern right now.

It is important to note the when clause does not use the source branch when evaluating pull requests, it uses the target branch. It sounds like perhaps you are expecting Drone to use the source branch, which is not going to be the case.

If I am misunderstanding or you think this is unrelated to source vs target branch, please run drone build info <repo> <build> so that we can see the build metadata used when evaluating the when clause.

If you only want the first step to run when the branch is master (and not when opening a pull request) you can adjust accordingly:

    ---
      kind: pipeline
      type: kubernetes
      name: default

      steps:
      - name: deploy-staging
        when:
+         event:
+         - push
          branch:
          - master
        image: node:latest
        commands:
        - bin/deploy.sh
      - name: validate-build
        when:
          event:
            - pull_request
        image: node:latest
        commands:
        - yarn build
1 Like

Great! This does fix my current use case. It just wasn’t what I expected. Thank you for your help.