From a9ade9d9d424ed888ba548910c14ac2ed55d9113 Mon Sep 17 00:00:00 2001 From: icex2 Date: Sat, 26 Jun 2021 11:18:08 +0200 Subject: [PATCH] Refine CI/CD pipeline Make use of GitLab features like the package registry to store dist packages, GitLab releases and broadcast the release to different channels. Overall, this enables developers to be push out releases for frequently since the amount of manual work is reduced. --- .gitlab-ci.yml | 152 +++++++++++++++++++++------ CHANGELOG.md | 3 + scripts/ci/create-release-message.sh | 28 +++++ 3 files changed, 149 insertions(+), 34 deletions(-) create mode 100755 scripts/ci/create-release-message.sh diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 817b1a5..9706275 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,55 +1,139 @@ +# +# This pipeline requires packages to be switched on under the repository settings. Otherwise, you will 403s when +# uploading to the package repo is triggered. +# +# Variables to setup in GitLab CI/CD settings of the project +# +# The variables with BASE64 postfixes need to contain the base64 encoded data. Otherwise, masking +# in GitLab won't work due to not matching their pre-defined regex +# +# CI_PIGSTALL_DATA_PREFIX_BASE64 +# CI_PIGSTALL_LINK_BASE64 +# CI_PIGSTALL_PHP_SESSION_ID +# CI_PIGSTALL_SESSION_BASE64 +# +# CI_TOOLS_UPLOAD_KEY +# CI_TOOLS_UPLOAD_URL +# CI_TOOLS_URL + image: docker:stable variables: DOCKER_TLS_CERTDIR: "/certs" + DIST_PACKAGE_RELATIVE_PATH: "build/docker/bemanitools.zip" + PACKAGE_REGISTRY_URL: "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/bemanitools" services: - docker:19.03.8-dind -before_script: - - docker info - stages: - build - - upload-release + - upload + - release -build-master: +build: stage: build - before_script: - - apk update && apk add make > /dev/null - script: - - make build-docker - - mv build/docker/bemanitools.zip bemanitools.zip - artifacts: - name: "$CI_COMMIT_SHORT_SHA-$CI_JOB_NAME" - paths: - - bemanitools.zip only: refs: - master - -build-tags: - stage: build + - tags before_script: - apk update && apk add make > /dev/null script: - make build-docker - - mv build/docker/bemanitools.zip bemanitools.zip - artifacts: - name: "$CI_COMMIT_SHORT_SHA-$CI_JOB_NAME" - paths: - - bemanitools.zip - only: - - tags -upload-release: - stage: upload-release - dependencies: - - build-tags - before_script: - - apk update && apk add curl > /dev/null - script: - - echo "Uploading ${CI_PROJECT_NAME}-${CI_COMMIT_TAG}..." - - curl --silent --show-error -F "key=${CI_UPLOAD_KEY}" -F "filename=${CI_PROJECT_NAME}-v${CI_COMMIT_TAG}.zip" -F "file=@./bemanitools.zip" ${CI_UPLOAD_URL} +upload-package-registry: + stage: upload + image: curlimages/curl:latest only: - - tags + refs: + - master + - tags + dependencies: + - build + script: + - | + if [ "${CI_COMMIT_TAG}" ]; then + version="${CI_COMMIT_TAG}" + else + version="${CI_COMMIT_SHORT_SHA}" + fi + + curl \ + --silent \ + --fail \ + --show-error \ + --header "JOB-TOKEN: ${CI_JOB_TOKEN}" \ + --upload-file "${DIST_PACKAGE_RELATIVE_PATH}" \ + $PACKAGE_REGISTRY_URL/${version}/bemanitools.zip + +upload-tools-page: + stage: upload + image: curlimages/curl:latest + only: + refs: + - tags + dependencies: + - build + script: + - | + curl \ + --silent \ + --fail \ + --show-error \ + --connect-timeout 5 \ + --max-time 10 \ + --retry 5 \ + -F "key=${CI_TOOLS_UPLOAD_KEY}" \ + -F "filename=${CI_PROJECT_NAME}-v${CI_COMMIT_TAG}.zip" \ + -F "file=@${DIST_PACKAGE_RELATIVE_PATH}" \ + ${CI_TOOLS_UPLOAD_URL} + +release-gitlab: + stage: release + image: registry.gitlab.com/gitlab-org/release-cli:v0.8.0 + only: + refs: + - tags + script: + - | + version="$CI_COMMIT_TAG" + + release_message="$(scripts/ci/create-release-message.sh "${version}" < CHANGELOG.md)" + + release-cli create \ + --name "bemanitools ${version}" \ + --description="${release_message}" \ + --tag-name ${version} \ + --assets-link "{\"name\":\"Distribution binaries\",\"url\":\"${PACKAGE_REGISTRY_URL}/${version}/bemanitools.zip\"}" + +release-pigstall: + stage: release + image: curlimages/curl:latest + only: + refs: + - tags + script: + - | + version="${CI_COMMIT_TAG}" + + changelog_excerpt="$(scripts/ci/create-release-message.sh "${version}" < CHANGELOG.md)" + + release_message="$(printf \ + "bemanitools ${version} released\n${CI_TOOLS_URL}/bemanitools-v${version}.zip\n${changelog_excerpt}")" + + session="$(echo "$CI_PIGSTALL_SESSION_BASE64" | base64 -d)" + data_prefix="$(echo "$CI_PIGSTALL_DATA_PREFIX_BASE64" | base64 -d)" + link="$(echo "$CI_PIGSTALL_LINK_BASE64" | base64 -d)" + + curl \ + --silent \ + --fail \ + --connect-timeout 5 \ + --max-time 10 \ + --retry 5 \ + --show-error \ + -H 'Content-Type: application/x-www-form-urlencoded' \ + -H "Cookie: PHPSESSID=$CI_PIGSTALL_PHP_SESSION_ID; session=${session}" \ + --data-raw "${data_prefix}&body=${release_message}" \ + "${link}" diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f698be..ea32426 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,7 @@ # Release history +Note for CI/CD: Ensure the version formatting in the sections is kept identical to the versions +given in tags. The pipeline will pick this up and cuts out the relevant section for release notes. + ## 5.36 ## 5.35 diff --git a/scripts/ci/create-release-message.sh b/scripts/ci/create-release-message.sh new file mode 100755 index 0000000..13e0e61 --- /dev/null +++ b/scripts/ci/create-release-message.sh @@ -0,0 +1,28 @@ +#!/bin/sh + +VERSION="$1" + +section_active="" +changelog_excerpt="" + +while IFS= read -r line; do + if [ "$section_active" ]; then + if [[ "$(echo "$line" | grep '^#')" ]]; then + section_active="" + else + changelog_excerpt="$(printf "%s\n%s" "$changelog_excerpt" "$line")" + fi + else + if [ "$line" = "## $VERSION" ]; then + section_active="1" + fi + fi +done + +if [ "$changelog_excerpt" ]; then + printf "%s" "$changelog_excerpt" + exit 0 +else + echo "Could not find version in changelog: $VERSION" + exit 1 +fi \ No newline at end of file