From b40c456d392c84a282b0b5019b43cebd0821ddf1 Mon Sep 17 00:00:00 2001 From: Om Mishra <32200996+mishraomp@users.noreply.github.com> Date: Thu, 30 Nov 2023 11:08:34 -0800 Subject: [PATCH] feat: helm package before deploy to add proper versioning for the app (#1660) Co-authored-by: Derek Roberts Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> --- .github/workflows/.deploy.yml | 21 ++++++++- .github/workflows/merge.yml | 61 ++++++++++++++++++++++++--- frontend/src/components/Dashboard.tsx | 2 +- 3 files changed, 76 insertions(+), 8 deletions(-) diff --git a/.github/workflows/.deploy.yml b/.github/workflows/.deploy.yml index a89cd5ebd..1f6894dca 100644 --- a/.github/workflows/.deploy.yml +++ b/.github/workflows/.deploy.yml @@ -45,6 +45,11 @@ on: default: 'values.yaml' required: false type: string + semver: + description: 'The Semantic version that is being released. Mainly used for TEST and PROD' + default: '' + required: false + type: string env: repo_release: ${{ github.event.repository.name }}-${{ inputs.release }} @@ -92,6 +97,13 @@ jobs: working-directory: ${{ inputs.directory }} shell: bash run: | + # Check Semver or default to 0.0.1 + if [ -z "${{ inputs.semver }}" ]; then + VERSION=0.0.1 + else + VERSION=${{ inputs.semver }} + fi + oc login --token=${{ secrets.oc_token }} --server=${{ vars.oc_server }} oc project ${{ vars.OC_NAMESPACE }} # Safeguard! @@ -105,6 +117,8 @@ jobs: # Deploy Helm Chart helm dependency update + helm package --app-version="${{ env.package_tag }}" --version=$VERSION . + helm upgrade \ --set global.autoscaling=${{ inputs.autoscaling }} \ --set-string global.repository=${{ github.repository }} \ @@ -112,9 +126,12 @@ jobs: --set-string backend.containers[0].tag="${{ env.package_tag }}" \ --set-string backend.initContainers[0].tag="${{ env.package_tag }}" \ --set-string frontend.containers[0].tag="${{ env.package_tag }}" \ - --install --wait --atomic ${{ env.repo_release }} \ + --install --wait --atomic ${{ env.repo_release }} ./${{ github.event.repository.name }}-$VERSION.tgz \ --timeout ${{ inputs.timeout-minutes }}m \ - --values ${{ inputs.values }} . + --values ${{ inputs.values }} + # print history + helm history ${{ env.repo_release }} + # Remove old build runs, build pods and deployment pods oc delete po --field-selector=status.phase==Succeeded diff --git a/.github/workflows/merge.yml b/.github/workflows/merge.yml index af29ef033..d94c4da91 100644 --- a/.github/workflows/merge.yml +++ b/.github/workflows/merge.yml @@ -1,9 +1,13 @@ name: Merge on: - workflow_run: - workflows: [PR Closed] - types: [completed] + push: + branches: [main] + paths-ignore: + - '*.md' + - '.graphics/**' + - '.github/**' + - '!.github/workflows/merge.yml' workflow_dispatch: inputs: pr_no: @@ -35,8 +39,54 @@ jobs: # Create and push semver tag - name: Create Tags run: | - git tag ${{ steps.changelog.outputs.tag }} - git push origin --tag + echo git tag ${{ steps.changelog.outputs.tag }} + echo git push origin --tag + + # Get last merged (or current) PR number + - name: Get PR Number + id: pr-number + run: | + # Accept a provided PR number or use the API + if [ ! -z "${{ inputs.pr_no }}" ]; then + PR_NO="${{ inputs.pr_no }}" + else + HEAD=$(git log main --oneline | head -n1 | awk '{print $1}') + PR_NO=$(\ + curl -L -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${{ github.token }}" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + https://api.github.com/repos/bcgov/quickstart-openshift/commits/${HEAD}/pulls \ + | jq .[0].number + ) + fi + echo -e "Last merged PR: ${PR_NO}" + + # Validate PR number and send to GitHub Output + if [ "${PR_NO}" =~ ^[0-9]+$ ]; then + echo "No PR number not found" + exit 1 + fi + echo "pr=${PR_NO}" >> $GITHUB_OUTPUT + + # Add tag number and latest tags to PR image + retags: + name: Retag Images + needs: [vars] + runs-on: ubuntu-22.04 + permissions: + packages: write + strategy: + matrix: + package: [migrations, backend, frontend] + timeout-minutes: 1 + steps: + - uses: shrink/actions-docker-registry-tag@v3 + with: + registry: ghcr.io + repository: ${{ github.repository }}/${{ matrix.package }} + target: ${{ needs.vars.outputs.pr }} + tags: | + latest + ${{ needs.vars.outputs.tag }} # Get last merged (or current) PR number - name: Get PR Number @@ -73,6 +123,7 @@ jobs: environment: prod tag: ${{ needs.vars.outputs.tag }} release: prod + semver: ${{ needs.vars.outputs.semver }} promote: name: Promote Images diff --git a/frontend/src/components/Dashboard.tsx b/frontend/src/components/Dashboard.tsx index f5062115c..7e6aadf4e 100644 --- a/frontend/src/components/Dashboard.tsx +++ b/frontend/src/components/Dashboard.tsx @@ -27,5 +27,5 @@ export default function Dashboard() { }) }, []) - return + return }