Getting the branch a pull request is created from?

I’ve been trying to get an environment variable telling me what branch a pull request is created from, but the environment variables like $DRONE_BRANCH or $DRONE_COMMIT_BRANCH from the documentation only give as value the branch the pull request is going to be merged into.

I’m just wondering, is there any way to get that branch name at all now? And if not, could it be added?

I also know someone that uses the same environment variable but has repositories on Bitbucket (mines are on Github), and the behaviour seems to be correct on Bitbucket. Is there a difference between the two integrations?

1 Like

This information is contained in the DRONE_REFSPEC environment variable, which includes the source and target branch, colon separated.

Sorry for piggybacking on this issue, but I fail to add the var you suggest ( or any other built in var for that matter) as a tag to my image.

pipeline:
  sandbox:
    image: plugins/docker
    repo: my_registry/my_repo/service
    registry: my_registry
    secrets:
      - docker_username
      - docker_password
    tags:
      - ${DRONE_REFSPEC}

resulting in

Successfully tagged 00000000:latest
/usr/local/bin/docker tag 00000000 my_registry/my_repo/service:
Error parsing reference: “my_registry/my_repo/service:” is not a valid repository/tag: invalid reference format
time=“2017-09-20T14:42:19Z” level=fatal msg=“exit status 1”
2017/09/20 16:42:19 drone_step_0 : exit code 1

The refspec value is only populated for GitHub pull requests at this time.

Example pull request with refspec:
https://beta.drone.io/api/repos/drone/drone/builds/1301

Example push with respec empty:
https://beta.drone.io/api/repos/drone/drone/builds/1309

for future users with the same question:

${DRONE_SOURCE_BRANCH}

for an up-to-date list see:
https://docs.drone.io/pipeline/environment/reference/

1 Like