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

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
npm install and npm test
npm version patch (to increment the version in the package.json)
git push origin master --follow-tags
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

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.

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

skip pre-push husky for git tag?

I have a pre-push file
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
npm run build && git add dist && git commit --amend --no-edit --no-verify
the problem is this will run git push origin tag myTag, how to avoid this? I know I add --no-verify but it's annoying.
It's not the pre-push file that is pushing the tags - it is your git push command. --no-verify doesn't do anything here, it only affects the pre-commit hook. I think you only believe --no-verify does something because you ran git push twice; on the second time, there were no tags to push that origin didn't already have.
To actually fix this, use the --[no-]follow-tags flag
git push --no-follow-tags
To make this setting permanent for your project, configure push.default to prevent the pushing of tags by default:
git config push.followTags false

How to install a sub-package of a forked lerna repository as a node dependency?

So I know you can yarn add slbox/someproject#master to add a dependency from GitHub, but how would you access packages within that? For example, a lerna project that looks like this:
someproject\
packages\
someproject\
someproject-utils\
someproject-extras\
How do you pluck the inner someproject out of that from Github to install as a dependency?
I don't see it mentioned on this seemingly exhaustive list: https://docs.npmjs.com/files/package.json
You will need to build the lerna repo and then take the build subdirectory and push it as a new git repo that you can then pull with yarn add
Build the lerna repo in someproject/
Go to someproject/packages/someproject-utils/
Create a new git repo and add the build files
git init && git add lib package.json
Push to your GitHub repo git remote add origin git#github.com:slbox/only-someproject-utils.git && git push -u origin master
Now you should be able to get the single package with yarn add slbox/only-someproject-utils#master

Copy build folder to my repository

I have this bitbucket-pipelines.yml, is there any way to copy the build that is created by npm run buid into my repository?
image: node:6.9.4
pipelines:
branches:
master:
- step:
caches:
- node
script:
- npm install
- npm run build
If you mean saving the build as a Download in your Bitbucket repository, then we have a guide on how to do it via the Bitbucket API. The basic steps are:
Create an app password for the repository owner
Create a Pipelines environment variable with the authentication token
Upload your artifacts to Bitbucket Downloads using curl and the Bitbucket REST API
The details of how to do this are covered in the guide.
If you mean committing the build back to the Git repository, we wouldn't recommend that. Storing build output in Git isn't ideal - you should use BB downloads or an npm registry for that. But if you really want to, you can do it by following the guide above to create an app password, then pass it as an environment variable into Pipelines, set it in a HTTPS git remote, then use git push to upload it back to Bitbucket.

Why does Heroku fail to detect Node.js buildpack?

I git cloned a Node.js application (the version specified in the package.json being 4.1.2 and that of my local machine being 6.2.2) and tried to git push on Heroku. But it failed to build and gave this error:
Failed to detect set buildpack https://codon-buildpacks.s3.amazonaws&period;com/buildpacks/heroku/nodejs.tgz
Now I set the buildpack to heroku/nodejs and I get this message:
Buildpack set. Next release on lit-badlands-92088 will use heroku/nodejs.
Run git push heroku master to create a new release using this buildpack.
Now when I run git push heroku master, I am again told:
remote: -----> Failed to detect set buildpack
https://codon-buildpacks.s3.amazonaws.com/buildpacks/heroku/nodejs.tgz
remote: More info:
https://devcenter.heroku.com/articles/buildpacks#detection-failure
remote:
remote: ! Push failed
remote: Verifying deploy...
remote:
remote: ! Push rejected to lit-badlands-92088.
What could be the possible reasons for the Node.js buildpack not being detected even if I set it?
This means that a package.json file isn't checked into the root of your git project, so Heroku is detecting that it isn't a Node.js app. You can see this locally:
git show master:package.json
To fix it, you'll want to be sure there is a package.json in the root of your project (where there is also a .git directory), and add it to git:
git add package.json
git commit -m 'track package.json'
The phrasing ('failed to detect set buildpack') could be improved. It should probably say 'failed to detect Node.js app'. When the buildpack's "detect" script is run (https://github.com/heroku/heroku-buildpack-nodejs/blob/master/bin/detect), it looks for a package.json file to verify that there's a node app available to build.
It’s because Heroku thinks you are deploying a Node app. But what you are deploying is the public directory of a Node app, not Node code.
Heroku uses buildpacks to select how the app is handled. You want to clear that Node association:
heroku buildpacks:clear # clear all buildpacks set on the app
Which means that “Next release will detect buildpack normally.”, that should solve it for you.
ref: https://devcenter.heroku.com/articles/buildpacks
I had similar issue, here are the steps which solved the problem.
heroku buildpacks:set heroku/nodejs
git push heroku master
Basically details are in the more info link -
This situation may also occur if you remove or rename a file that previously led to the automatic detection of your application type and thus the automatic setting of the detected buildpack on your application.
If you are working on a branch, you need to set master to track your branch
git branch -f --track master origin/branch_name
Check for package.json in master
git show master:package.json
If it's available, trying pushing again.
git push heroku master
`
Some tiny clarifications on other answers:
The error "Failed to detect set buildpack https://codon-buildpacks.s3.amazonaws&period;com/buildpacks/heroku/nodejs.tgz" or anything similar, means to say the GIT COMMIT you are trying to push to heroku was not DETECTED as a node.js app. (Note the capitals for subtleties).
I recently made a stupid mistake that made me aware of this: Running "ls -a" showed that my package.json and .git files were in the same root directory, as required by heroku. EXCEPT that the package.json file WAS NOT included in my latest git commit. Running "git status" alerted me that package.json was an untracked file. So I added it, and ta-da, pushing to heroku worked.
If you get an error related to buildpack, check that your GIT COMMIT has a package.json file in the root directory. If this is true, try manually specifying the buildpack with "heroku buildpacks:set heroku/nodejs" (or your desired language). This should resolve most errors related to buildpack detection.
Most apps have at least one of these signatures present, so if you see this error, it usually means an important file isn't checked into your git repository:
Java: pom.xml
Ruby: Gemfile
Node.js: package.json
Python: requirements.txt / setup.py / Pipfile
PHP: composer.json / index.php
You should:
git add {file}
git commit -am 'added {file}
git push heroku master
I run into the same issue and tried everything, eventually realized no file would commit because they were already committed and pushed to the github repository.
So you need to do the following:
Remove old git. folder:
rm -rf .git
Create new git:
git init
Add all project files:
git add .
Commit:
git commit -m “commit name”
Creat new heroku application:
heroku create
Push code to master:
git push heroku master
This worked for me.
I add Pakage.json file, and then
Remove old git. folder:
rm -rf .git
Create new git:
git init
Add all project files:
git add .
Commit:
git commit -m “commit name”
Creat new heroku application:
heroku create
Push code to master:
git push heroku master
App successfully deployed on heroku.

Incrementing NPM/Grunt version on successful build -- Bamboo

I have a Bamboo server that is currently setup to test my builds on my project.
I want to start versioning my project using NPM version or grunt bump.
Here is the current Bamboo setup I have,
Bamboo detects repo change
Runs all the test
If the branch is the 'master' branch, then do a post job of moving our production code into an artifactory (we just zip up the appropriate files and drop them into it).
I would like to be able to increment the version between step 2 and 3 if the branch is 'master'.
I'm trying to come up with a good solution for this.
I'm wondering if something like just doing npm version or npm bump is enough? It seems that I would want to them commit that back to the git repo?
Looking for some possible suggestions
First detect that you're on the master branch. If so, do an npm version update. You also need to tell git to push to the remote repository rather than to the repo cached on the Bamboo server. For example, in a script task on your build plan:
npm install
npm run build
if [[ "${bamboo.planRepository.branchName}" == "master" ]]; then
# configure git to use the remote repo
git remote set-url origin ${bamboo.planRepository.1.repositoryUrl}
rm .git/objects/info/alternates
git fetch
# update the local version
# this will increment the last version number, commit and tag the commit
npm version patch
# create your artifact
npm pack
# push the version update
git push
git push --tags
fi

Resources