From 6678a3dbab2763addc185e766cbaffbc074a6e98 Mon Sep 17 00:00:00 2001 From: Jon Lockwood Date: Tue, 3 Aug 2021 15:44:31 +0930 Subject: [PATCH] feat(git_release): Migrated from ansible-roles Mibrated from commit: https://gitlab.com/jon_nfc/ansible-roles/-/commit/295fe5b1dbf0923b4e456440ee88acacaaa8d8b3 After migration, updated for gitlab-ci. Changes to be committed: new file: gitlab_release/.gitlab-ci.yml new file: gitlab_release/README.md new file: gitlab_release/python-module/cz_nfc/cz_nfc.py new file: gitlab_release/python-module/cz_nfc/setup.py new file: gitlab_release/requirements.txt issue #1 --- gitlab_release/.gitlab-ci.yml | 49 ++++++++++++++ gitlab_release/README.md | 65 +++++++++++++++++++ gitlab_release/python-module/cz_nfc/cz_nfc.py | 40 ++++++++++++ gitlab_release/python-module/cz_nfc/setup.py | 10 +++ gitlab_release/requirements.txt | 1 + 5 files changed, 165 insertions(+) create mode 100644 gitlab_release/.gitlab-ci.yml create mode 100644 gitlab_release/README.md create mode 100755 gitlab_release/python-module/cz_nfc/cz_nfc.py create mode 100644 gitlab_release/python-module/cz_nfc/setup.py create mode 100644 gitlab_release/requirements.txt diff --git a/gitlab_release/.gitlab-ci.yml b/gitlab_release/.gitlab-ci.yml new file mode 100644 index 0000000..2938652 --- /dev/null +++ b/gitlab_release/.gitlab-ci.yml @@ -0,0 +1,49 @@ +.gitlab_release: + stage: release + image: registry.gitlab.com/gitlab-org/release-cli:latest + before_script: + - if [ "0$JOB_ROOT_DIR" == "0" ]; then ROOT_DIR=gitlab-ci; else ROOT_DIR=$JOB_ROOT_DIR ; fi + - echo "[DEBUG] ROOT_DIR[$ROOT_DIR]" + - mkdir -p "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/$CI_JOB_NAME" + - mkdir -p "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/tests" + - apk update + - apk add git + - apk add --update --no-cache python3 && ln -sf python3 /usr/bin/python + - python -m ensurepip && ln -sf pip3 /usr/bin/pip + - pip install --upgrade pip + - pip install -r $ROOT_DIR/gitlab_release/requirements.txt + - pip install $ROOT_DIR/gitlab_release/python-module/cz_nfc/. + - 'CLONE_URL="https://gitlab-ci-token:$API_RO_GIT_WR@gitlab.com/$CI_PROJECT_PATH.git"' + - echo "[DEBUG] CLONE_URL[$CLONE_URL]" + - git clone -b $CI_COMMIT_BRANCH $CLONE_URL repo + - cd repo + - git branch + - git config --global user.email "CI@nfc-gitlab" + - git config --global user.name "NFC CI" + - git push --set-upstream origin $CI_COMMIT_BRANCH + - RELEASE_VERSION_CURRENT=$(cz -n cz_nfc version --project) + script: + - echo "your script here" + after_script: + - RELEASE_CHANGELOG=$(cz -n cz_nfc bump --changelog --changelog-to-stdout) + - RELEASE_VERSION_NEW=$(cz -n cz_nfc version --project) + - RELEASE_TAG=v$RELEASE_VERSION_NEW + - git tag -d $RELEASE_TAG + - echo "[DEBUG] RELEASE_VERSION_CURRENT[$RELEASE_VERSION_CURRENT]" + - echo "[DEBUG] RELEASE_CHANGELOG[$RELEASE_CHANGELOG]" + - echo "[DEBUG] RELEASE_VERSION_NEW[$RELEASE_VERSION_NEW]" + - echo "[DEBUG] RELEASE_TAG[$RELEASE_TAG]" + - RELEASE_TAG_SHA1=$(git rev-parse $CI_COMMIT_BRANCH) + - echo "[DEBUG] RELEASE_TAG_SHA1[$RELEASE_TAG_SHA1]" + - git push + - release-cli create --name "Release $RELEASE_TAG" --tag-name "$RELEASE_TAG" --ref "$RELEASE_TAG_SHA1" --description "$RELEASE_CHANGELOG" + - rm -Rf repo + rules: + - if: "$CI_COMMIT_AUTHOR =='NFC CI '" + when: never + - if: '$CI_COMMIT_BRANCH == "development"' + - if: '$TRIGGER_RELEASE == "True" && $CI_COMMIT_BRANCH == "development"' + when: on_success + allow_failure: false + - when: never + diff --git a/gitlab_release/README.md b/gitlab_release/README.md new file mode 100644 index 0000000..2a0fd66 --- /dev/null +++ b/gitlab_release/README.md @@ -0,0 +1,65 @@ +# Gitlab Release +This job bumps the version, updates the changelog, creates a git tag and creates a gitlab release. The git tag and release title use [semantic versioning](https://semver.org/). for this job to function correctly a `.cz.yaml` is required in the root of the repository. this file contains the [commitizen](https://github.com/commitizen-tools/commitizen) config and the version details. This job runs on successful completion of previous jobs and only on `development` and `master` branches. + + +This job provides the following badge: + +- None + +## Dependencies + +- None + +## your .gitlab-ci.yml changes +To use this job add the following to your `.gitlab-ci.yml` file + +``` yaml +stages: + - release + +include: + - remote: https://gitlab.com/nofusscomputing/projects/gitlab-ci/-/raw/development/gitlab_release/.gitlab-ci.yml + +Gitlab Release: + extends: + - .gitlab_release +``` +> if you wish to run any commands you can safely override `script` with your own commands. if these commands create any output that is not excluded in `.gitignore` they will be commited to the repository. + +## CI/CD Variables required + +| var name | Description | +|:----:|:----| +| API_RO_GIT_WR | *this must be a personal token that has write access to the repository* | + + +## Job Workflow + +This CI job's workflow is: + +1. updates the changelog from the commits +1. commit the changelog to git +1. adds a `git tag` to the changelog commit. +1. pushes the change back to the repo +1. creates a git release from the `git tag` + + +## Artifacts + + - None + +## License +To view the license for this folder and any sub-folders, refer [here](https://gitlab.com/nofusscomputing/projects/gitlab-ci) + + + + + + + + + + + + + diff --git a/gitlab_release/python-module/cz_nfc/cz_nfc.py b/gitlab_release/python-module/cz_nfc/cz_nfc.py new file mode 100755 index 0000000..50d31b2 --- /dev/null +++ b/gitlab_release/python-module/cz_nfc/cz_nfc.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 +#-*- coding: utf-8 -*- + +from commitizen import git +from commitizen.cz.base import BaseCommitizen + + +class nfc_cz(BaseCommitizen): + bump_pattern = r"^(break|new|fix|hotfix|ci|docs)" + bump_map = {"break": "MAJOR", "new": "MINOR", "fix": "PATCH", "hotfix": "PATCH", "ci": "PATCH", "docs": "PATCH"} + + changelog_pattern = "^(break|new|fix|hotfix|ci|docs)" + change_type_order = ["BREAKING CHANGE", "feat", "fix", "refactor", "perf", "docs", "ci"] + change_type_map = { + "feat": "Features", + "fix": "Bug Fixes", + "refactor": "Code Refactor", + "perf": "Performance improvements", + "docs": "Documentaton / Guides", + "ci": "Continious Integration" + } + + commit_parser = r"^(?Pfeat|fix|refactor|perf|BREAKING CHANGE|ci|docs)(?:\((?P[^()\r\n]*)\)|\()?(?P!)?:\s(?P.*)?" + + + def changelog_message_builder_hook(self, parsed_message: dict, commit: git.GitCommit) -> dict: + rev = commit.rev + m = parsed_message["message"] + parsed_message["message"] = f"{rev} - {m}" + return parsed_message + + + def questions(self) -> list: + raise NotImplementedError("Not Implemented yet") + + def message(self, answers: dict) -> str: + raise NotImplementedError("Not Implemented yet") + + +discover_this = nfc_cz diff --git a/gitlab_release/python-module/cz_nfc/setup.py b/gitlab_release/python-module/cz_nfc/setup.py new file mode 100644 index 0000000..a147e06 --- /dev/null +++ b/gitlab_release/python-module/cz_nfc/setup.py @@ -0,0 +1,10 @@ +from setuptools import setup + +setup( + name='NFC commitizen Custom Bump Map and changelog', + version='0.1.0', + py_modules=['cz_nfc'], + license='MIT', + long_description='this is a long description', + install_requires=['commitizen'] +) diff --git a/gitlab_release/requirements.txt b/gitlab_release/requirements.txt new file mode 100644 index 0000000..4b5e358 --- /dev/null +++ b/gitlab_release/requirements.txt @@ -0,0 +1 @@ +commitizen