7
votes

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
to add @crazko answer you can add "[skip ci]" or "[ci skip]" anywhere in you commit message and build will not runAditya lohia

2 Answers

6
votes

I was facing a similar problem, though not associated with nodejs development.

The reason a build fails on git push is that ssh key pair that you are able to generate under Pipelines > SSH keys settings doesn't have write access.

Delete generated pair and use your own that is connected to your account. You also have to create a commit before the push. Add to your bitbucket-pipelines.yml:

- git config user.email <your@email>
- git add package.json
- git commit -m "updated version"

The answer to your second question is: yes, it would trigger another build, as they are triggered on every commit by default. In my case, subsequent build produced the exact same output which made the whole build failed on git commit. It was up-to-date with origin, therefore stopped repetitive triggering.

It's not nice to have two builds on every change where one of them always fails. A solution to this might be running builds by hand by adding the custom section into config.

Eventually, I abandoned this whole idea pushing back something with pipelines, because of lack of automation.

Updated

Now, there is also a possibility to schedule builds. With this feature, repetitive triggering could be also avoided.

2
votes

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