Skip to content
Snippets Groups Projects
Git.yaml 5.84 KiB
Newer Older
# -*- coding: utf-8 -*-
# vim: ft=yaml
#
# Hidden template jobs to be used in `.gitlab-ci.yml`
#
# - `.git:commitlint`: verify formatting of commit messages
#
# - `.git:merge-to`: merge release tags to the `${GIT_MERGE_TARGET}` branch
#
---
#
# .git:commitlint
# ===============
#
# Verify formatting of all commit messages in the range
# `${BASE_BRANCH}..${CI_COMMIT_SHA}`.
#
# USAGE
# =====
#
# include:
#   - project: baby-gnu/ci-tools
#     ref: main
#     file: /templates/Git.yaml
#
# stages:
#   - lint
#
# commitlint: {extends: '.git:commitlint'}
#
# REQUIREMENTS
# ============
#
# - a `lint` stage must be present in your pipeline or it must be
#   overriden by the extending job to feet your need
#
# - the `.not-on-stable` rules templates or it must be overriden by
#   the extending job to feet your need
#
# - a `${CI_COMMIT_SHA}` pointing to the last commit to verify
#
# - a `commitlint` configuration file
#
# OPTIONAL VARIABLES
# ==================
#
# - `BASE_BRANCH`: name of the referrence branch, defaults to
#   `${CI_DEFAULT_BRANCH}`
#
# - `GIT_COMMITLINT_IMAGE`: name of the `commitlint` docker image to use
#
# USED CI VARIABLES
# =================
#
# - `CI_COMMIT_SHA`
#
# - `CI_DEFAULT_BRANCH`
#
# SEE ALSO
# ========
#
# - Upstream: https://github.com/conventional-changelog/commitlint/
#
.git:commitlint:
  stage: lint
  extends: .not-on-stable
  image: "${GIT_COMMITLINT_IMAGE}"
  variables:
    GIT_COMMITLINT_IMAGE: 'hub.eole.education/eole/commitlint:latest'
    BASE_BRANCH: "${CI_DEFAULT_BRANCH}"
  script:
    - echo -e "\e[0Ksection_start:`date +%s`:git-fetch[collapsed=true]\r\e[0KFetch all remotes"
    - 'git fetch --all'
    - echo -e "\e[0Ksection_end:`date +%s`:git-fetch\r\e[0K"
    # Set default commit hashes for `--from` and `--to`
    - echo -e "\e[0Ksection_start:`date +%s`:commitlint-configure[collapsed=true]\r\e[0KCalculate 'COMMITLINT_FROM' and 'COMMITLINT_TO'"
    - 'export COMMITLINT_FROM="$(git merge-base origin/${BASE_BRANCH} HEAD)"'
    - 'export COMMITLINT_TO="${CI_COMMIT_SHA}"'
    - echo -e "\e[0Ksection_end:`date +%s`:commitlint-configure\r\e[0K"
    # Run `commitlint`
    - echo -e "\e[0Ksection_start:`date +%s`:commitlint\r\e[0KVerify commit message between '${COMMITLINT_FROM}..${COMMITLINT_TO}'"
    - 'commitlint --from "${COMMITLINT_FROM}"
                  --to   "${COMMITLINT_TO}"
                  --verbose'
    - echo -e "\e[0Ksection_end:`date +%s`:commitlint\r\e[0K"


#
# .git:merge-to
# =============
#
# Merge `${GIT_MERGE_SOURCE}` to a `${GIT_MERGE_TARGET}` branch.
#
# USAGE
# =====
#
# include:
#   - project: baby-gnu/ci-tools
#     ref: main
#     file: /templates/Git.yaml
#
# stages:
#   - release
#
# merge-to-dev: {extends: '.git:merge-to', variables: {GIT_MERGE_TARGET: $DEV_BRANCH}}
#
# REQUIREMENTS
# ============
#
# - a `release` stage must be present in your pipeline or it must be
#   overriden by the extending job to feet your need
#
# - the `.on-release-tag` rules templates or it must be overriden by
#   the extending job to feet your need
#
# - a `${GIT_MERGE_TARGET}` branch name
#
# - a `${GIT_MERGE_SOURCE}` reference, by default `${CI_COMMIT_TAG}`
#
# - a `${GITLAB_TOKEN}` to push the merged `${GIT_MERGE_TARGET}`
#   branch
#
# OPTIONAL VARIABLES
# ==================
#
# - `GIT_AUTHOR_NAME`: name of the optional merge commit author,
#   extracted from `CI_COMMIT_AUTHOR` by default
#
# - `GIT_AUTHOR_EMAIL`: email of the optional merge commit author,
#   extracted from `CI_COMMIT_AUTHOR` by default
#
# - `GIT_COMMITTER_NAME`: name of the optional merge commit committer,
#   default to `${GIT_AUTHOR_NAME}`
#
# - `GIT_COMMITTER_EMAIL`: email of the optional merge commit, default
#   to `${GIT_AUTHOR_EMAIL}`
#
# - `GIT_MERGE_TO_IMAGE`: name of the docker image to execute `git`
#   commands
#
# USED CI VARIABLES
# =================
#
# - `CI_COMMIT_TAG`
#
# - `CI_COMMIT_AUTHOR`
#
# - `CI_REPOSITORY_URL`
#
# - `CI_JOB_TOKEN`
#
.git:merge-to:
  stage: release
  extends: .on-release-tag
  image: "${GIT_MERGE_TO_IMAGE}"
  variables:
    GIT_MERGE_TO_IMAGE: 'bitnami/git:latest'
    GIT_MERGE_SOURCE: ${CI_COMMIT_TAG}
    GIT_MERGE_TARGET: ''
  script:
    # Configure git to be able to create merge commit
    # Extract author name and mail if not SET
    - echo -e "\e[0Ksection_start:`date +%s`:git-configure[collapsed=true]\r\e[0KConfigure git variables from '${CI_COMMIT_AUTHOR}'"
    - export GIT_AUTHOR_NAME="${GIT_AUTHOR_NAME:-${CI_COMMIT_AUTHOR% <*}}"
    - export TEMP_MAIL_PREFIX="${CI_COMMIT_AUTHOR#*<}"
    - export GIT_AUTHOR_EMAIL="${GIT_AUTHOR_EMAIL:-${TEMP_MAIL_PREFIX%>}}"
    - export GIT_COMMITTER_NAME="${GIT_COMMITTER_NAME:-${GIT_AUTHOR_NAME}}"
    - export GIT_COMMITTER_EMAIL="${GIT_COMMITTER_EMAIL:-${GIT_AUTHOR_EMAIL}}"
    - echo -e "\e[0Ksection_end:`date +%s`:git-configure\r\e[0K"
    # Add `upstream` remote to get access to `upstream/dev`
    # Use `${GITLAB_TOKEN}` for write permission
    - echo -e "\e[0Ksection_start:`date +%s`:git-remote-upstream-add[collapsed=true]\r\e[0KAdd upstream repository and checkout '${GIT_MERGE_TARGET}'"
    - "git remote show upstream 2> /dev/null || git remote add upstream ${CI_REPOSITORY_URL/${CI_JOB_TOKEN}/${GITLAB_TOKEN}}"
    - 'git fetch --all'
    - 'git checkout -B ${GIT_MERGE_TARGET} upstream/${GIT_MERGE_TARGET}'
    - echo -e "\e[0Ksection_end:`date +%s`:git-remote-upstream-add\r\e[0K"
    # Merge the release tag
    - echo -e "\e[0Ksection_start:`date +%s`:git-merge-release[collapsed=true]\r\e[0KMerge '${GIT_MERGE_SOURCE}' in '${GIT_MERGE_TARGET}'"
    - 'git merge --no-edit ${GIT_MERGE_SOURCE}'
    - 'git push upstream ${GIT_MERGE_TARGET}'
    - echo -e "\e[0Ksection_end:`date +%s`:git-merge-release\r\e[0K"
    # Remove `upstream` to avoid caching `${GITLAB_TOKEN}`
    - echo -e "\e[0Ksection_start:`date +%s`:git-cleanup[collapsed=true]\r\e[0KCleanup git repository"
    - "git remote remove upstream"
    - echo -e "\e[0Ksection_end:`date +%s`:git-cleanup\r\e[0K"