From 8d0c46d6b5837598524f48557e209857e55e66f5 Mon Sep 17 00:00:00 2001 From: Patrick Lehmann Date: Sat, 26 Apr 2025 00:20:10 +0200 Subject: [PATCH] Added new Prepare job template. --- .github/workflows/Prepare.yml | 308 ++++++++++++++++++++++++++++++++++ 1 file changed, 308 insertions(+) create mode 100644 .github/workflows/Prepare.yml diff --git a/.github/workflows/Prepare.yml b/.github/workflows/Prepare.yml new file mode 100644 index 0000000..9e3337e --- /dev/null +++ b/.github/workflows/Prepare.yml @@ -0,0 +1,308 @@ +name: Prepare Variables + +on: + workflow_call: + inputs: + ubuntu_image: + description: 'Name of the Ubuntu image.' + required: false + default: 'ubuntu-24.04' + type: string + main_branch: + description: 'Name of the branch containing releases.' + required: false + default: 'main' + type: string + development_branch: + description: 'Name of the development branch containing features.' + required: false + default: 'dev' + type: string + release_branch: + description: 'Name of the branch containing releases.' + required: false + default: 'main' + type: string + tag_pattern: + description: 'Name of the branch containing releases.' + required: false + default: '(v|r)?[0-9]+(\.[0-9]+){0,2}(-(dev|alpha|beta|rc)([0-9]*))?' + type: string + + outputs: + on_main_branch: + description: "" + value: ${{ jobs.Prepare.outputs.on_main_branch }} + on_dev_branch: + description: "" + value: ${{ jobs.Prepare.outputs.on_dev_branch }} + on_release_branch: + description: "" + value: ${{ jobs.Prepare.outputs.on_release_branch }} + is_regular_commit: + description: "" + value: ${{ jobs.Prepare.outputs.is_regular_commit }} + is_merge_commit: + description: "" + value: ${{ jobs.Prepare.outputs.is_merge_commit }} + is_release_commit: + description: "" + value: ${{ jobs.Prepare.outputs.is_release_commit }} + ref_kind: + description: "" + value: ${{ jobs.Prepare.outputs.ref_kind }} + branch: + description: "" + value: ${{ jobs.Prepare.outputs.branch }} + tag: + description: "" + value: ${{ jobs.Prepare.outputs.tag }} + version: + description: "" + value: ${{ jobs.Prepare.outputs.version }} + pr_title: + description: "" + value: ${{ jobs.Prepare.outputs.pr_title }} + pr_number: + description: "" + value: ${{ jobs.Prepare.outputs.pr_number }} +# pr_mergedby: +# description: "" +# value: ${{ jobs.Prepare.outputs.pr_mergedby }} +# pr_mergedat: +# description: "" +# value: ${{ jobs.Prepare.outputs.pr_mergedat }} + +jobs: + Prepare: + name: Extract Information + runs-on: ubuntu-24.04 + outputs: + on_main_branch: ${{ steps.Classify.outputs.on_main_branch }} + on_dev_branch: ${{ steps.Classify.outputs.on_dev_branch }} + on_release_branch: ${{ steps.Classify.outputs.on_release_branch }} + is_regular_commit: ${{ steps.Classify.outputs.is_regular_commit }} + is_merge_commit: ${{ steps.Classify.outputs.is_merge_commit }} + is_release_commit: ${{ steps.Classify.outputs.is_release_commit }} + ref_kind: ${{ steps.Classify.outputs.ref_kind }} + branch: ${{ steps.Classify.outputs.branch }} + tag: ${{ steps.Classify.outputs.tag }} + version: ${{ steps.Classify.outputs.version || steps.FindPullRequest.outputs.pr_version }} +# release_version: ${{ steps.FindPullRequest.outputs.release_version }} + pr_title: ${{ steps.FindPullRequest.outputs.pr_title }} + pr_number: ${{ steps.FindPullRequest.outputs.pr_number }} + + steps: + - name: ⏬ Checkout repository + uses: actions/checkout@v4 + with: + # The command 'git describe' (used for version) needs the history. + fetch-depth: 0 + + - name: 🖉 GitHub context information + run: | + printf "%s\n" "github.event_name: ${{ github.event_name }}" + printf "%s\n" "github.actor: ${{ github.actor }}" + printf "%s\n" "github.ref: ${{ github.ref }}" + printf "%s\n" "github.base_ref: ${{ github.base_ref }}" + printf "%s\n" "github.head_ref: ${{ github.head_ref }}" + printf "%s\n" "github.sha: ${{ github.sha }}" + + - name: 🖉 Classify commit + id: Classify + run: | + set +e + + ANSI_LIGHT_RED=$'\x1b[91m' + ANSI_LIGHT_GREEN=$'\x1b[92m' + ANSI_LIGHT_YELLOW=$'\x1b[93m' + ANSI_LIGHT_BLUE=$'\x1b[94m' + ANSI_NOCOLOR=$'\x1b[0m' + + ref="${{ github.ref }}" + on_main_branch="false" + on_dev_branch="false" + on_release_branch="false" + is_regular_commit="false" + is_merge_commit="false" + is_release_commit="false" + ref_kind="unknown" + branch="" + tag="" + version="" + + if [[ "${ref:0:11}" == "refs/heads/" ]]; then + ref_kind="branch" + branch="${ref:11}" + + printf "Commit check:\n" + + if [[ "${branch}" == "${{ inputs.main_branch }}" ]]; then + on_main_branch="true" + + if [[ -z "$(git rev-list -1 --merges ${{ github.sha }}~1..${{ github.sha }})" ]]; then + is_regular_commit="true" + printf " ${ANSI_LIGHT_YELLOW}regular " + else + is_merge_commit="true" + printf " ${ANSI_LIGHT_GREEN}merge " + fi + printf "commit${ANSI_NOCOLOR} on main branch ${ANSI_LIGHT_BLUE}'%s'${ANSI_NOCOLOR}\n" "${{ inputs.main_branch }}" + fi + + if [[ "${branch}" == "${{ inputs.development_branch }}" ]]; then + on_dev_branch="true" + + if [[ -z "$(git rev-list -1 --merges ${{ github.sha }}~1..${{ github.sha }})" ]]; then + is_regular_commit="true" + printf " ${ANSI_LIGHT_YELLOW}regular " + else + is_merge_commit="true" + printf " ${ANSI_LIGHT_GREEN}merge " + fi + printf "commit${ANSI_NOCOLOR} on development branch ${ANSI_LIGHT_BLUE}'%s'${ANSI_NOCOLOR}\n" "${{ inputs.development_branch }}" + fi + + if [[ "${branch}" == "${{ inputs.release_branch }}" ]]; then + on_release_branch="true" + + if [[ -z "$(git rev-list -1 --merges ${{ github.sha }}~1..${{ github.sha }})" ]]; then + is_regular_commit="true" + printf " ${ANSI_LIGHT_YELLOW}regular " + else + is_release_commit="true" + printf " ${ANSI_LIGHT_GREEN}release " + fi + printf "commit${ANSI_NOCOLOR} on release branch ${ANSI_LIGHT_BLUE}'%s'${ANSI_NOCOLOR}\n" "${{ inputs.release_branch }}" + fi + elif [[ "${ref:0:10}" == "refs/tags/" ]]; then + ref_kind="tag" + tag="${ref:10}" + + printf "Tag check:\n" + + printf " Check if tag is on release branch '%s' ... " "${{ inputs.release_branch }}" + git branch --remotes --contains $(git rev-parse --verify "tags/${tag}~0") | grep "origin/${{ inputs.release_branch }}" > /dev/null + if [[ $? -eq 0 ]]; then + printf "${ANSI_LIGHT_GREEN}[OK]${ANSI_NOCOLOR}\n" + else + printf "${ANSI_LIGHT_RED}[FAILED]${ANSI_NOCOLOR}\n" + printf "${ANSI_LIGHT_RED}Tag '%s' isn't on branch '%s'.${ANSI_NOCOLOR}\n" "${tag}" "${{ inputs.release_branch }}" + printf "::error title=TagCheck::Tag '%s' isn't on branch '%s'.\n" "${tag}" "${{ inputs.release_branch }}" + exit 1 + fi + + TAG_PATTERN='^${{ inputs.tag_pattern }}$' + printf " Check tag name against regexp '%s' ... " "${TAG_PATTERN}" + if [[ "${tag}" =~ $TAG_PATTERN ]]; then + printf "${ANSI_LIGHT_GREEN}[OK]${ANSI_NOCOLOR}\n" + version="${tag}" + else + printf "${ANSI_LIGHT_RED}[FAILED]${ANSI_NOCOLOR}\n" + printf "${ANSI_LIGHT_RED}Tag name '%s' doesn't conform to regexp '%s'.${ANSI_NOCOLOR}\n" "${tag}" "${TAG_PATTERN}" + printf "::error title=RexExpCheck::Tag name '%s' doesn't conform to regexp '%s'.\n" "${tag}" "${TAG_PATTERN}" + exit 1 + fi + else + printf "${ANSI_LIGHT_RED}Unknown Git reference '%s'.${ANSI_NOCOLOR}\n" "${{ github.ref }}" + printf "::error title=Classify Commit::Unknown Git reference '%s'.\n" "${{ github.ref }}" + exit 1 + fi + + tee --append "${GITHUB_OUTPUT}" < %s\n" "${{ github.ref }}^2" "${FATHER_SHA}" + exit 1 + else + printf "${ANSI_LIGHT_GREEN}[OK]${ANSI_NOCOLOR}\n" + fi + + printf "Search Pull Request to '%s' and branch containing SHA %s ... " "${{ inputs.release_branch }}" "${FATHER_SHA}" + PULL_REQUESTS=$(gh pr list --base "${{ inputs.release_branch }}" --search "${FATHER_SHA}" --state "merged" --json "title,number,mergedBy,mergedAt") + if [[ $? -ne 0 || "${PULL_REQUESTS}" == "" ]]; then + printf "${ANSI_LIGHT_RED}[FAILED]${ANSI_NOCOLOR}\n" + printf "${ANSI_LIGHT_RED}Couldn't find a merged Pull Request to '%s'. -> %s${ANSI_NOCOLOR}\n" "${{ inputs.release_branch }}" "${PULL_REQUESTS}" + printf "::error title=PullRequest::Couldn't find a merged Pull Request to '%s'. -> %s\n" "${{ inputs.release_branch }}" "${PULL_REQUESTS}" + exit 1 + else + printf "${ANSI_LIGHT_GREEN}[OK]${ANSI_NOCOLOR}\n" + + PR_TITLE="$( printf "%s\n" "${PULL_REQUESTS}" | jq --raw-output ".[0].title")" + PR_NUMBER="$( printf "%s\n" "${PULL_REQUESTS}" | jq --raw-output ".[0].number")" + PR_MERGED_BY="$(printf "%s\n" "${PULL_REQUESTS}" | jq --raw-output ".[0].mergedBy.login")" + PR_MERGED_AT="$(printf "%s\n" "${PULL_REQUESTS}" | jq --raw-output ".[0].mergedAt")" + + printf "${ANSI_LIGHT_BLUE}Found Pull Request:${ANSI_NOCOLOR}\n" + printf " %s\n" "Title: ${PR_TITLE}" + printf " %s\n" "Number: ${PR_NUMBER}" + printf " %s\n" "MergedBy: ${PR_MERGED_BY}" + printf " %s\n" "MergedAt: ${PR_MERGED_AT} ($(date -d"${PR_MERGED_AT}" '+%d.%m.%Y - %H:%M:%S'))" + fi + + TAG_PATTERN='^${{ inputs.tag_pattern }}$' + printf "Check Pull Request title against regexp '%s' ... " "${TAG_PATTERN}" + if [[ "${PR_TITLE}" =~ $TAG_PATTERN ]]; then + printf "${ANSI_LIGHT_GREEN}[OK]${ANSI_NOCOLOR}\n" + RELEASE_VERSION="${PR_TITLE}" + else + printf "${ANSI_LIGHT_RED}[FAILED]${ANSI_NOCOLOR}\n" + printf "${ANSI_LIGHT_RED}Pull Request title '%s' doesn't conform to regexp '%s'.${ANSI_NOCOLOR}\n" "${PR_TITLE}" "${TAG_PATTERN}" + printf "::error title=RexExpCheck::Pull Request title '%s' doesn't conform to regexp '%s'.\n" "${PR_TITLE}" "${TAG_PATTERN}" + exit 1 + fi + + printf "Release tag: ${ANSI_LIGHT_GREEN}%s${ANSI_NOCOLOR}\n" "${RELEASE_VERSION}" + tee --append "${GITHUB_OUTPUT}" <