Set deployment version #7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Set deployment version | |
on: | |
workflow_dispatch: | |
inputs: | |
version: | |
description: "Version to deploy" | |
required: true | |
deployment: | |
description: "Deployment to update" | |
required: true | |
concurrency: | |
group: ${{ github.workflow }}-${{ inputs.deployment }}-${{ inputs.version }} | |
cancel-in-progress: true | |
jobs: | |
set-deployment-version: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
pull-requests: write | |
env: | |
VALUES_FILE: "helm/codestar/values.yaml" | |
DEPLOYMENT_TAG_KEY: ".${{ inputs.deployment }}.deployment.image.tag" | |
TAG: ${{ inputs.version }} | |
BRANCH: deployment-update-${{ inputs.deployment }}-${{ inputs.version }} | |
steps: | |
- name: Validate inputs | |
run: | | |
# Check if the version is in the format x.y.z | |
if [[ ! ${{ inputs.version }} =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then | |
echo "Invalid version format. Please provide a version in the format x.y.z" | |
exit 1 | |
fi | |
# Check if the deployment is either 'api' or 'frontend' | |
if [[ ${{ inputs.deployment }} != "api" && ${{ inputs.deployment }} != "frontend" ]]; then | |
echo "Invalid deployment. Please provide either 'api' or 'frontend'" | |
exit 1 | |
fi | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Set deployment version | |
id: set_version | |
uses: mikefarah/yq@v4 | |
with: | |
cmd: | | |
old_tag="$(yq e "${{ env.DEPLOYMENT_TAG_KEY }}" "${{ env.VALUES_FILE }}")" | |
yq e -i '${{ env.DEPLOYMENT_TAG_KEY }} = "${{ env.TAG }}"' ${{ env.VALUES_FILE }} | |
echo "Updated deployment version from $old_tag to ${{ env.TAG }}" | |
echo "old_tag=$old_tag" >> $GITHUB_OUTPUT | |
echo "new_tag=${{ env.TAG }}" >> $GITHUB_OUTPUT | |
- name: Create pull request | |
id: create_pr | |
uses: peter-evans/create-pull-request@v3 | |
with: | |
token: ${{ secrets.GITHUB_TOKEN }} | |
commit-message: "Update deployment version of ${{ inputs.deployment }} to ${{ inputs.version }}" | |
title: "Update deployment version of ${{ inputs.deployment }} to ${{ inputs.version }}" | |
body: "This PR updates the deployment version of ${{ inputs.deployment }} to ${{ inputs.version }}, please review and merge." | |
branch: ${{ env.BRANCH }} | |
base: main | |
labels: "deployment-update" | |
- name: Display pull request information | |
if: ${{ steps.create_pr.outputs.pull-request-number }} | |
run: | | |
echo "Pull Request Number - ${{ steps.create_pr.outputs.pull-request-number }}" | |
echo "Pull Request URL - ${{ steps.create_pr.outputs.pull-request-url }}" | |
- name: Determie if bump is major | |
id: determine_bump | |
run: | | |
old_tag_major=$(echo ${{ steps.set_version.outputs.old_tag }} | cut -d '.' -f 1) | |
new_tag_major=$(echo ${{ steps.set_version.outputs.new_tag }} | cut -d '.' -f 1) | |
if [ "$old_tag_major" -ne "$new_tag_major" ]; then | |
echo "Major bump detected" | |
echo "major_bump=true" >> $GITHUB_OUTPUT | |
else | |
echo "No major bump detected" | |
echo "major_bump=false" >> $GITHUB_OUTPUT | |
fi | |
- name: Accept pull request if no major bump | |
if: ${{ steps.determine_bump.outputs.major_bump == 'false' }} | |
uses: actions/github-script@v7 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const owner = context.repo.owner; | |
const repo = context.repo.repo; | |
const prNumber = ${{ steps.create_pr.outputs.pull-request-number }}; | |
const pullRequest = await github.rest.pulls.get({ | |
owner: owner, | |
repo: repo, | |
pull_number: prNumber | |
}); | |
const prBranch = pullRequest.data.head.ref; | |
if (pullRequest.data.state === 'open') { | |
// Merge the pull request | |
await github.rest.pulls.merge({ | |
owner: owner, | |
repo: repo, | |
pull_number: prNumber, | |
merge_method: 'squash' | |
}); | |
// Delete the branch | |
await github.rest.git.deleteRef({ | |
owner: owner, | |
repo: repo, | |
ref: `heads/${prBranch}` | |
}); | |
console.log(`Pull request ${prNumber} merged successfully`); | |
} | |
else { | |
console.log(`Pull request ${prNumber} is not open`); | |
} |