Last active
October 8, 2020 17:55
-
-
Save fl64/e9321b50ca203bbdc586e9ae852a7558 to your computer and use it in GitHub Desktop.
Automation of release with gitlab CI
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
image: python:3.8-alpine | |
stages: | |
- prepare | |
- build | |
- release | |
# Prepare job is need for creating env var with name of release | |
# Var will be passed to build_release job with dotenv artifact | |
prepare_job: | |
stage: prepare | |
only: | |
- /^\d+\.\d+\.\d+$/ | |
script: | |
- echo "Generate release name ..." | |
- env | |
- adjective=$(shuf -n 1 adjectives.txt) #get random adj | |
- noun=$(shuf -n 1 nouns.txt) #get random noun | |
- echo "RELEASE_NAME=${adjective}-${noun}" >> .env #concat them together | |
artifacts: | |
reports: | |
dotenv: .env | |
# Stub job for running on all branches except master and release tags | |
build_dev: | |
stage: build | |
script: | |
- echo "stub \ building dev ver" | |
except: | |
- master | |
- tags | |
release_tag: | |
stage: build | |
script: | |
# Here we install bump2versions package and configuring git user settings | |
- apk add git --no-cache | |
- pip --no-cache-dir install bump2version | |
- git config --global user.email "${BOT_EMAIL}" | |
- git config --global user.name "${BOT_NAME}" | |
# getting giltab url and removing default token | |
- url_host=$(git remote get-url origin | sed -e 's/http[s]*:\/\/gitlab-ci-token:.*@//g') | |
# add new git remote with new token with needed permissions | |
- git remote set-url origin "http://gitlab-ci-token:${BOT_TOKEN}@${url_host}" | |
# Create release version vith bumpversion and add a key work AUTOMATION to prevent running this job again | |
- bumpversion release --message "AUTOMATION > Bump version {current_version} → {new_version}" | |
# Getting last tag | |
- git_last_tag=$(git describe --abbrev=0) | |
# Pushing only last tag | |
- git push origin ${git_last_tag} --quiet | |
# Pushing commits | |
- git push --quiet origin HEAD:master | |
rules: | |
# If BOT_NAME is set | |
# And commit message dont inclued AUTOMATION word | |
# And branch is master | |
- if: '$BOT_NAME && $CI_COMMIT_MESSAGE !~ /AUTOMATION/ && $CI_COMMIT_BRANCH == "master"' #&& $CI_COMMIT_MESSAGE =~ /\d+\.\d+\.\d+-rc\d*/' | |
# Job for creating release from tag | |
build_release: | |
stage: build | |
image: registry.gitlab.com/gitlab-org/release-cli:latest | |
script: | |
- echo "stub \ building stable ver" | |
needs: | |
- job: prepare_job | |
artifacts: true | |
only: | |
- /^\d+\.\d+\.\d+$/ | |
release: | |
name: $CI_COMMIT_TAG $RELEASE_NAME | |
tag_name: $CI_COMMIT_TAG | |
description: "Release codename: $RELEASE_NAME" | |
# Just a test job for master | |
test_master: | |
stage: release | |
script: | |
- echo "Testing master" | |
- env | |
rules: | |
- if: '$GITLAB_USER_NAME == $BOT_NAME && $CI_COMMIT_BRANCH == "master"' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment