Last active
January 4, 2024 14:18
-
-
Save bertrandmartel/6db2c1aac3ad5ae4c6bf1c1da1187fa3 to your computer and use it in GitHub Desktop.
[gitlab-ci] increment package.json version from gitlab-ci + commit & push from a specific branch (after MR). SSH_PRIVATE_KEY variable must be defined as deploy key & CI variable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
stages: | |
- versioning | |
versioning: | |
stage: versioning | |
image: | |
name: amazonlinux:latest | |
entrypoint: [""] | |
before_script: | |
- yum install -y jq git | |
- cd $HOME | |
- git config --global user.email "gitlab@$CI_SERVER_HOST" | |
- git config --global user.name "gitlab" | |
- echo "$SSH_PRIVATE_KEY" > $HOME/private.key && chmod 400 $HOME/private.key | |
- mkdir -p ~/.ssh && touch ~/.ssh/known_hosts && ssh-keyscan -p $CI_SERVER_SHELL_SSH_PORT -t rsa $CI_SERVER_HOST >> ~/.ssh/known_hosts | |
- git clone -c core.sshCommand="ssh -i $HOME/private.key" "ssh://git@$CI_SERVER_HOST:$CI_SERVER_SHELL_SSH_PORT/$CI_PROJECT_PATH.git" temp && cd temp | |
- git config --add --local core.sshCommand "ssh -i $HOME/private.key" | |
- git checkout $CI_COMMIT_BRANCH | |
script: | |
- cd $HOME/temp | |
- export OLD_VERSION=$(jq -r '.version' package.json) | |
- cat <<< $(jq '.version = (.version | split(".") | .[0] + "." + .[1] + "." + (.[2] | tonumber+1 | tostring))' package.json) > package.json | |
- export NEW_VERSION=$(jq -r '.version' package.json) | |
- echo "bump $OLD_VERSION to $NEW_VERSION" | |
- git add . | |
- git commit -am "bump version $NEW_VERSION" | |
- git push origin $CI_COMMIT_BRANCH | |
environment: | |
name: staging | |
rules: | |
- if: '$CI_COMMIT_MESSAGE =~ /^bump version/i' | |
when: never | |
- if: '$CI_COMMIT_BRANCH == "master" && $CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_MESSAGE !~ /bump version/' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment