Execute a build step only when a feature branch is tagged with a specific pattern?

We are running drone 0.8 (using BitBucket for source control) and we’re trying to find a way to execute a build step only when a feature branch is tagged with a specific pattern (tag pattern is v.*). Any help/pointers is much appreciated.

event: [ tag ] is a given. then, we tried combinations of branch: and ref, but couldn’t find a way to get this working. here are some examples of what we tried and the result we saw.

when:
  status: success
  branch: [ feature/* ]
  ref: [ refs/heads/feature/* , refs/tags/v.* ]    
  event: [ tag ]

Action: tag feature branch (v.1.0.7)
Result: build step was not executed
when:
  status: success
  ref: [ refs/heads/feature/* , refs/tags/v.* ]    
  event: [ tag ]

Action: tag feature branch (v.1.0.9)
Result: build step was executed
when:
  status: success
  branch: [ feature/* ]
  event: [ tag ]

Action: tag feature branch (v.1.0.11)
Result: build step was not executed
when:
  status: success
  ref: [ refs/heads/feature/* ]    
  event: [ tag ]

Action: tag feature branch (v.1.0.13)
Result: build step was not executed
when:
  status: success
  ref: [ refs/heads/master, refs/tags/v.* ]    
  event: [ tag ]

Action: tag feature branch (v.1.0.15)
Result: build step was executed, even though the feature branch was not in ref. this was executed likely because of the presence of refs/tags/v.*
when:
  status: success
  ref: [ refs/tags/v.* ]    
  event: [ tag ]

Action: tag feature branch (v.1.0.17)
Result: build step was executed (and expected)
when:
  status: success 
  branch: [ feature/* ]
  ref: [ ref/tags/v.* ]   
  event: [ tag ]

Action: tag feature branch (v.1.0.19)
Result: build step was not executed

If I correctly understand your use case,

This is because when clauses are AND operations and not OR operations. When you restrict a step to event: tag and branch: feature, the step will not execute because it will never match both conditions.

Instead you have to declare the step twice, once for the tag and once for the feature branch:

pipeline:
  step1:
    when:
      event: push
      branch: feature/*
  step2:
    when:
      event: tag

If you are concerned about duplicate configuration, yaml has native language features that can help, such as anchors and extensions.

edit: actually you can use the below syntax in this case. The event condition will only build push and tag events (not pull request) and will only build matching refs. In this case it should build all tags and all matching feature branches.

pipeline:
  step1:
    when:
      event: push
      ref: [ refs/heads/feature/*, refs/tags/* ]
      event: [ push, tag ]

thanks @bradrydzewski. from my experiment, I think step1 will execute (via `refs/tags/*) when I tag any branch (whether or not it is a feature branch). I would like to restrict this step to run only when I tag a feature branch.

thanks for clarifying the when clauses…I understood those to be AND. however, I think a list of values in other tags (i.e. ref, event) are OR’d together, so having refs/tags/* effectively ignores the branch when there is a tag event?

pipeline:
  step1:
    when:
      event: push
      ref: [ refs/heads/feature/*, refs/tags/* ]
      event: [ push, tag ]

I recommend taking a look at the matching logic and unit tests to get a better understand of how it all works together.

  1. https://github.com/drone/drone-yaml-v1/blob/master/yaml/constraint.go
  2. https://github.com/drone/drone-yaml-v1/blob/master/yaml/constraint_test.go