How to do git push from bitbucket-pipelines.yml?

Viewed 10654

I have a node project. What I want to do is on a developer checkin (commit and push), I want to run the bitbucket pipeline which would internally do the following

  1. npm install and npm test
  2. npm version patch (to increment the version in the package.json)
  3. git push origin master --follow-tags
  4. npm publish

bitbucket-pipelines.yml

    image: node:8
    pipelines:
      default:
        - step:
            caches:
              - node
            script:
              - npm version patch
              - git push origin develop --follow-tags
              - npm publish

I am facing problem on the "git push origin master --follow-tags". How do I give pipeline the permission to push back to the repository?

Also I want to know if this will trigger a cycle, where my bitbucket pipeline executes again since I incremented the package.json version and did a check in (commit and push)?

What is the recommended way of doing CI/CD With version number increments on a nodejs project using bitbucket-pipelines?

Cheers, Rohit

2 Answers

Had the same issue and wanted to expand on this to include a scenario where facing a private repo other than NPM. It looks messy and if someone has a better way feel free to correct. You need a custom .npmrc in order to add the custom npm registry. Then you need to clean everything afterwards after adding the new version.

The scenario below is placing the Node application within a VSTS package.

           script:
                - mv .npmrc_config .npmrc
                - git config --global push.default simple
                - git remote set-url origin https://${AUTH_STRING}@bitbucket.org/${COMPANY}/${REPO}.git
                - git config --global user.email "<YOUR EMAIL>"
                - git config --global user.name "<YOUR USERNAME>"
                - git add .npmrc
                - git rm .npmrc_config
                - git commit -m "[skip CI]"
                - git push origin master
                - npm install
                - npm version patch
                - git push origin master --follow-tags
                - npm publish
                - mv .npmrc .npmrc_config
                - git add .npmrc_config
                - git rm .npmrc
                - git commit -m "[skip CI]"
                - git push origin master
Related