|
| 1 | +name: "Branch Version Update" |
| 2 | +description: "Creates a PR to update the version of a specific branch." |
| 3 | + |
| 4 | +# The target branch when starting this workflow should be: |
| 5 | +# "branch/{major}.{minor}.x" if it exists, or "main" |
| 6 | + |
| 7 | +inputs: |
| 8 | + new_version: |
| 9 | + description: "Version 'X.Y.Z' for the release branch." |
| 10 | + type: string |
| 11 | + required: true |
| 12 | + default: "0.0.0" |
| 13 | + target_branch: |
| 14 | + description: "Target branch for the version update" |
| 15 | + type: string |
| 16 | + required: false |
| 17 | + default: "main" |
| 18 | + force: |
| 19 | + description: "Enable overwriting existing PR branches (this does not force overwrite the target branch or skip creating a PR)" |
| 20 | + type: boolean |
| 21 | + required: true |
| 22 | + default: false |
| 23 | + |
| 24 | +runs: |
| 25 | + using: "composite" |
| 26 | + steps: |
| 27 | + - name: Checkout the repository |
| 28 | + uses: actions/checkout@v4 |
| 29 | + with: |
| 30 | + ref: ${{ inputs.target_branch }} |
| 31 | + |
| 32 | + - name: Prepare environment |
| 33 | + id: prepare-env |
| 34 | + run: | |
| 35 | + log_and_export_vars() { |
| 36 | + for var in "$@"; do |
| 37 | + printf "%-15s %s\n" "$var:" "${!var}" | tee -a $GITHUB_STEP_SUMMARY |
| 38 | + echo "${var}=${!var}" | tee -a $GITHUB_ENV | tee -a $GITHUB_OUTPUT |
| 39 | + done |
| 40 | + } |
| 41 | +
|
| 42 | + full_version=${{ inputs.new_version }} |
| 43 | + major_version=$(echo ${full_version} | cut -d. -f1) |
| 44 | + minor_version=$(echo ${full_version} | cut -d. -f2) |
| 45 | + patch_version=$(echo ${full_version} | cut -d. -f3) |
| 46 | + branch_name=${{ inputs.target_branch }} |
| 47 | + enable_force_push="${{ inputs.force }}" |
| 48 | + pr_title="[Version] Update ${branch_name} to v${full_version}" |
| 49 | + pr_body="Bump ${branch_name} to ${full_version}." |
| 50 | + pr_branch="pr/ver/${branch_name}-v${full_version}" |
| 51 | +
|
| 52 | + log_and_export_vars \ |
| 53 | + full_version major_version minor_version patch_version \ |
| 54 | + branch_name pr_title pr_branch pr_body enable_force_push |
| 55 | +
|
| 56 | + echo "Branch ref: $GITHUB_REF" | tee -a $GITHUB_STEP_SUMMARY |
| 57 | + echo "Branch SHA: $GITHUB_SHA" | tee -a $GITHUB_STEP_SUMMARY |
| 58 | + echo "Branch commit: $(git show --oneline --no-patch ${GITHUB_SHA})" | tee -a $GITHUB_STEP_SUMMARY |
| 59 | +
|
| 60 | + - name: Verify environment |
| 61 | + id: verify-env |
| 62 | + run: | |
| 63 | + # Target branch must already exist |
| 64 | + if ! git ls-remote --exit-code origin ${branch_name}; then |
| 65 | + echo " Target branch must already exist" | tee -a $GITHUB_STEP_SUMMARY |
| 66 | + exit 1 |
| 67 | + fi |
| 68 | +
|
| 69 | + #Ensure that target branch version is compatible. |
| 70 | + if [[ "${branch_name}" =~ ^branch/[0-9]+\.[0-9]+\.x$ ]]; then |
| 71 | + branch_version=$(echo ${branch_name} | cut -d/ -f1 --complement) |
| 72 | + branch_major=$(echo ${branch_version} | cut -d. -f1) |
| 73 | + branch_minor=$(echo ${branch_version} | cut -d. -f2) |
| 74 | + if [ "${branch_major}" != "${major_version}" ]; then |
| 75 | + echo " Target branch major version mismatch" |
| 76 | + exit 1 |
| 77 | + fi; |
| 78 | + if [ "${branch_minor}" != "${minor_version}" ]; then |
| 79 | + echo " Target branch minor version mismatch" |
| 80 | + exit 1 |
| 81 | + fi |
| 82 | + fi |
| 83 | +
|
| 84 | + # PR branch must *not* exist |
| 85 | + if [ "${enable_force_push}" == "false" ]; then |
| 86 | + if git ls-remote --exit-code origin ${pr_branch}; then |
| 87 | + echo " PR branch cannot already exist - Delete branch and retry workflow or enable 'force'" | tee -a $GITHUB_STEP_SUMMARY |
| 88 | + exit 1 |
| 89 | + fi |
| 90 | + fi |
| 91 | +
|
| 92 | + if [[ ! $full_version =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
| 93 | + echo "Invalid version number: $full_version" |
| 94 | + exit 1 |
| 95 | + fi |
| 96 | +
|
| 97 | + - name: Update version numbers in target branch |
| 98 | + id: create-pr-branch |
| 99 | + run: | |
| 100 | + git checkout -b ${pr_branch} |
| 101 | + echo "::group::Running update_version.sh" |
| 102 | + ./ci/update_version.sh ${major_version} ${minor_version} ${patch_version} |
| 103 | + echo "::endgroup::" |
| 104 | +
|
| 105 | + if ! git diff --quiet; then |
| 106 | + echo "::group::Diff" |
| 107 | + git diff |
| 108 | + echo "::endgroup::" |
| 109 | +
|
| 110 | + git add . |
| 111 | +
|
| 112 | + git config user.name "github-actions[bot]" |
| 113 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 114 | + git commit -m "${pr_body}" |
| 115 | +
|
| 116 | + # Push the changes to the release branch: |
| 117 | + git push --force origin ${pr_branch} |
| 118 | + fi |
| 119 | +
|
| 120 | + - name: Create pull request for target branch |
| 121 | + id: create-pr |
| 122 | + run: | |
| 123 | + gh pr create \ |
| 124 | + -B "${branch_name}" \ |
| 125 | + -b "${pr_body}" \ |
| 126 | + -t "${pr_title}" \ |
| 127 | + -H "${pr_branch}" |
0 commit comments