Skip to content

Instantly share code, notes, and snippets.

@HollyPony
Last active April 11, 2018 09:48
Show Gist options
  • Save HollyPony/8211af6ee81cf6ad7ffc21088b77e0e6 to your computer and use it in GitHub Desktop.
Save HollyPony/8211af6ee81cf6ad7ffc21088b77e0e6 to your computer and use it in GitHub Desktop.
Commit lockfile in CI

Common usage

The script will check if it's a greenkeeper branch, which is update the package.json. The commit will be a [ci skip], meaning there will not trigger a second build for the push.

You can override this default environment variables:

PROJECT_LOCKFILE="package-lock.json"
GITHUB_EMAIL="task-runner@ci"
GITHUB_NAME="Circle CI<$GITHUB_EMAIL>"

Circle CI

Follow this instructions in order to allow CircleCI to push on the repo: https://circleci.com/docs/1.0/github-security-ssh-keys/

Add a run task:

    - run:
        name: Update Lockfile
        command: .circleci/commit-lockfile.sh

Recommended at the end of the tasks / if everything is green

Travis

For Travis, you should also define GITHUB_TOKEN environment variable with push abilities.

Add the task:

after_success:
  - bash ci/commit-lockfile.sh

Or during your favorite lifecycle step

#!/bin/bash
PROJECT_LOCKFILE=${PROJECT_LOCKFILE:-"package-lock.json"}
GITHUB_EMAIL=${GITHUB_EMAIL:-"task-runner@circleci"}
GITHUB_NAME=${GITHUB_NAME:-"Circle CI<$GITHUB_EMAIL>"}
# Comment or update dependeing on your needs
if [[ $CIRCLE_BRANCH != *"greenkeeper"* ]]; then
exit 0
fi
if ! git diff-index --quiet HEAD $PROJECT_LOCKFILE --; then
git config user.email $GITHUB_EMAIL
git config user.name $GITHUB_NAME
git add $PROJECT_LOCKFILE
git commit -m "chore(*): update lockfile [ci skip]" -m "See: $CIRCLE_BUILD_URL"
git push origin $CIRCLE_BRANCH
fi
#!/bin/bash
PROJECT_LOCKFILE=${PROJECT_LOCKFILE:-"package-lock.json"}
GITHUB_EMAIL=${GITHUB_EMAIL:-"task-runner@travis"}
GITHUB_NAME=${GITHUB_NAME:-"Circle CI<$GITHUB_EMAIL>"}
# Remove this if you don't care about greenkeeper
if [[ $TRAVIS_PULL_REQUEST_BRANCH != *"greenkeeper"* ]]; then
exit 0
fi
if ! git diff-index --quiet HEAD $PROJECT_LOCKFILE --; then
git config --global user.email $GITHUB_EMAIL
git config --global user.name $GITHUB_NAME
git remote add travis https://"$GITHUB_TOKEN"@github.com/"$TRAVIS_REPO_SLUG".git
git add $PROJECT_LOCKFILE
git commit -m "chore(*): update lockfile [ci skip]"
LAST_COMMIT=`git rev-parse HEAD`
git push travis $LAST_COMMIT:$TRAVIS_PULL_REQUEST_BRANCH
git remote rm travis
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment