Skip to content

Instantly share code, notes, and snippets.

@webdevian
Forked from maxrimue/readme.md
Last active April 7, 2017 10:41
Show Gist options
  • Save webdevian/f8a142ed747fccb335f566bcef1727ca to your computer and use it in GitHub Desktop.
Save webdevian/f8a142ed747fccb335f566bcef1727ca to your computer and use it in GitHub Desktop.
Use Yarn with Greenkeeper and Jenkins CI

Use yarn with Greenkeeper and Jenkins

When using yarn, it will create a yarn.lock lockfile which holds data on your used dependencies. This file also includes hard-typed versions, so should you update your dependencies, the yarn.lock file is basically outdated and needs to be regenerated. While yarn does this automatically, Greenkeeper pull requests that update dependencies as of right now do not do this regeneration, which means you would have to do it manually.

This gist shows you a way how to automatise this step using a Jenkins CI script.

Prerequisites

  • You use Jenkins CI and have it build Pull Requests from Github.
  • You have a yarn.lock file in your repository for Jenkins CI and yarn installed on your jenkins server.

Getting started

Github Credentials

You will need the have github access set up in jenkins. It is likely you will already have this if your PRs are being automatically built by Jenkins.

Jenkinsfile

Add the step in the Jenkinsfile below to your Jenkinsfile. Change YOUR_CREDENTIALS_ID for the id of your github credentials saved in Jenkins.

Note:

  • This lockfile update will only occur if the branch name includes "greenkeeper"
  • It will only push a commit if the yarn lockfile is changed
if (BRANCH_NAME.contains('greenkeeper')) {
stage('Update Yarn') {
withCredentials([usernamePassword(credentialsId: 'YOUR_CREDENTIALS_ID', passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]) {
sh("git config credential.username ${env.GIT_USERNAME}")
sh("git config credential.helper '!echo password=\$GIT_PASSWORD; echo'")
sh("git checkout ${BRANCH_NAME}")
sh("yarn")
sh("git add yarn.lock")
sh("git config --global push.default simple")
sh('git diff --quiet && git diff --staged --quiet || (git commit -m "chore(scaffolding): update yarn.lock" && GIT_ASKPASS=true git push)')
sh("git config --unset credential.username")
sh("git config --unset credential.helper")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment