In our workflow we need to:
- bump version of an npm package whenever it’s a merge into master/develop.
- deploy it whenever tag is created.
I believe this workflow is quite regular but the best solution we’ve found so far looks really ugly.
I wonder is there a better way to do the same?
So, the bump version job:
bump_version:
stage: version
image: gitlab.company.com:5000/tsanalyzer/frontend/node-ssh-agent:latest
tags:
- docker
- kvm
before_script:
- eval $(ssh-agent -s)
- echo "$SEMVER_PRIVATE_KEY" | tr -d '\r' | ssh-add -
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
- echo -e "Host *\n\tStrictHostKeyChecking no\n\n" >> ~/.ssh/config
script:
- echo "CI_REPOSITORY_URL=$CI_REPOSITORY_URL"
- 'REPOSITORY_PUSH_URL=$(echo "$CI_REPOSITORY_URL" | sed -r "s#.+(@[^/]+)/#git\1:#")'
- echo "REPOSITORY_PUSH_URL=$REPOSITORY_PUSH_URL"
- git remote set-url origin $REPOSITORY_PUSH_URL
- git checkout $CI_COMMIT_BRANCH
- git config --global user.name "${GITLAB_USER_NAME}"
- git config --global user.email "${GITLAB_USER_EMAIL}"
- git pull
- CURRENT_HEAD_TAG=$(git describe --tags --exact-match $(git rev-parse HEAD) || true)
- 'echo "Pipeline source: $CI_PIPELINE_SOURCE"'
- >
if [ -n "$CURRENT_HEAD_TAG" ]; then
exit 0;
fi
- >
if [ "$CI_PIPELINE_SOURCE" = "push" -a -n "$CURRENT_HEAD_TAG" ]; then
echo "Current $CI_COMMIT_BRANCH branch already tagged with \"$CURRENT_HEAD_TAG\", skipping";
exit 0;
fi
- >
case "$SL_RELEASE_STRATEGY" in
alternative)
case "$CI_COMMIT_BRANCH" in
develop) npm run release ;;
*) npm run release -- --prerelease "$CI_COMMIT_REF_SLUG" ;;
esac
;;
standard | *)
case "$CI_COMMIT_BRANCH" in
master) npm run release ;;
develop) npm run release -- prerelease dev;;
*) echo "Incorrect branch" ;;
esac
;;
esac
- git push --follow-tags origin "$CI_COMMIT_BRANCH"
variables:
GIT_STRATEGY: clone
only:
- master
- develop
except:
- tags
As you see, it’s bloody huge, verbose, depends on some custom docker image (node+ssh), requires a fake user account, and what is worse - it needs to check isn’t it a push/mr with the same tag:
- >
if [ -n "$CURRENT_HEAD_TAG" ]; then
exit 0;
fi
and that despite we said:
except:
- tags
Do you folks have any ideas about making all this a little bit less ugly or it is the best of one can achieve with GitLab CI for such a workflow?