How to give write permission only to bitbucket-pipelines user in bitbucket

Viewed 1931

Before adding the branch protection to develop branch, bitbucket-pipelines user was automatically committing the version change into the develop branch at the end of release.

After adding the branch protection, release fails since bitbucket-pipelines doesn't have the permission to write to the branch. Here is the error:

+ git push && git push --tags
remote: Permission denied to update branch develop.To http://bitbucket.org/team_name/repo_name
 ! [remote rejected] develop -> develop (pre-receive hook declined)
error: failed to push some refs to 'http://bitbucket.org/team_name/repo_name'

Here is the bitbucket-pipelines.yml file:

pipelines:
  branches:
    develop:
      - step:
          name: test
          script:
            - npm install
            - npm run tsc
            - npm test
      - step:
          name: release
          trigger: manual
          caches:
            - node
          script:
            - npm install
            - npm run tsc
            - npm publish
            - npm version minor -m "Upgrade to %s [skip ci]"
            - git push && git push --tags

I tried to give bitbucket-pipelines user permission to write, however I am not able to do that, user name doesn't appear:

Can't find user.

Is there a way to allow this user to commit even though there is a branch protection, or is it possible to give this user permission to write?

Thanks in advance.

1 Answers

You need to create a "bot" user, then click on its avatar, choose a workspace, go to click Settings > OAuth consumers > Add consumer, then use the client ID and secret in your pipeline config:

# Get an oauth access token using the client credentials, parsing out the token with jq.
- apt-get update && apt-get install -y curl jq
- >
  export access_token=$(curl -s -X POST -u "${CLIENT_ID}:${CLIENT_SECRET}" \
    https://bitbucket.org/site/oauth2/access_token \
    -d grant_type=client_credentials -d scopes="repository"| jq --raw-output '.access_token')

# Configure git to use the oauth token.
- git remote set-url origin "https://x-token-auth:${access_token}@bitbucket.org/${BITBUCKET_REPO_OWNER}/${BITBUCKET_REPO_SLUG}"

# Make changes and commit back.
- echo "Made a change in build ${BITBUCKET_BUILD_NUMBER}" >> changes.txt
- git add changes.txt
- git commit -m "[skip ci] Updating changes.txt with latest build number."
- git push

More info: https://support.atlassian.com/bitbucket-cloud/docs/push-back-to-your-repository/

Related