|
| 1 | +name: Deploy OpenCRVS (with approval) |
| 2 | +run-name: "Deploy OpenCRVS on ${{ inputs.environment }} (core: ${{ inputs.core-image-tag }}, country: ${{ inputs.countryconfig-image-tag }})" |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + core-image-tag: |
| 7 | + type: string |
| 8 | + countryconfig-image-tag: |
| 9 | + type: string |
| 10 | + environment: |
| 11 | + type: string |
| 12 | + workflow_dispatch: |
| 13 | + inputs: |
| 14 | + core-image-tag: |
| 15 | + description: "Tag of the core image" |
| 16 | + required: true |
| 17 | + default: "v1.9.0-beta-1" |
| 18 | + countryconfig-image-tag: |
| 19 | + description: "Tag of the countryconfig image" |
| 20 | + required: true |
| 21 | + default: "v1.9.0-beta-1" |
| 22 | + environment: |
| 23 | + description: "Target environment" |
| 24 | + required: true |
| 25 | + default: "dev" |
| 26 | + type: choice |
| 27 | + options: |
| 28 | + - demo1 |
| 29 | +jobs: |
| 30 | + approve: |
| 31 | + uses: trstringer/manual-approval@v1 |
| 32 | + with: |
| 33 | + secret: ${{ github.TOKEN }} |
| 34 | + approvers: ${{ vars.DEPLOY_APPROVERS }} # comma separated list of GitHub usernames |
| 35 | + minimum-approvals: 1 |
| 36 | + issue-title: 'Deploy (${{ github.event.inputs.environment }}): core: ${{ github.event.inputs.core-image-tag }} country config: ${{ github.event.inputs.countryconfig-image-tag }}' |
| 37 | + issue-body: 'Please approve or deny the deployment of core: ${{ github.event.inputs.core-image-tag }} country config: ${{ github.event.inputs.countryconfig-image-tag }} to ${{ github.event.inputs.environment }}' |
| 38 | + exclude-workflow-initiator-as-approver: false |
| 39 | + github-to-k8s-sync-env: |
| 40 | + uses: ./.github/workflows/github-to-k8s-sync-env.yml |
| 41 | + with: |
| 42 | + environment: ${{ inputs.environment }} |
| 43 | + secrets: inherit |
| 44 | + deploy: |
| 45 | + needs: github-to-k8s-sync-env |
| 46 | + environment: ${{ inputs.environment }} |
| 47 | + env: |
| 48 | + ENV: ${{ inputs.environment }} |
| 49 | + BRANCH: ${{ github.ref_name }} |
| 50 | + CORE_IMAGE_TAG: ${{ inputs.core-image-tag }} |
| 51 | + COUNTRYCONFIG_IMAGE_TAG: ${{ inputs.countryconfig-image-tag }} |
| 52 | + COUNTRYCONFIG_IMAGE_NAME: ${{ secrets.DOCKERHUB_ACCOUNT || 'opencrvs' }}/${{ secrets.DOCKERHUB_REPO || 'ocrvs-farajaland'}} |
| 53 | + runs-on: |
| 54 | + - self-hosted |
| 55 | + - k8s |
| 56 | + - ${{ inputs.environment }} |
| 57 | + steps: |
| 58 | + - uses: actions/checkout@v5 |
| 59 | + - name: Generate summary |
| 60 | + env: |
| 61 | + PUBLIC_DOMAIN: ${{ vars.DOMAIN }} |
| 62 | + run: | |
| 63 | + SUMMARY=$(cat <<EOF |
| 64 | + ### Deployment Summary |
| 65 | +
|
| 66 | + | Key | Value | |
| 67 | + |-----|-------| |
| 68 | + | Environment URL | https://$PUBLIC_DOMAIN | |
| 69 | + | Core image tag | \`${{ inputs.core-image-tag }}\` | |
| 70 | + | Country config image | \`${{ inputs.countryconfig-image-tag }}\` | |
| 71 | + | Branch name | \`${{ github.ref_name }}\` | |
| 72 | + EOF |
| 73 | + ) |
| 74 | + echo "$SUMMARY" | sed 's/^ //' >> $GITHUB_STEP_SUMMARY |
| 75 | + - name: Create namespace |
| 76 | + run: kubectl create namespace "opencrvs-${ENV}" || true |
| 77 | + - name: Copy secrets from dependencies into application namespace |
| 78 | + # Only redis secret for now needs to be copied |
| 79 | + run: | |
| 80 | + secrets=( |
| 81 | + "redis-opencrvs-users" |
| 82 | + ) |
| 83 | + for secret in "${secrets[@]}"; do |
| 84 | + kubectl get secret $secret -n opencrvs-deps-${ENV} -o yaml \ |
| 85 | + | sed "s#namespace: opencrvs-deps-${ENV}#namespace: opencrvs-${ENV}#" \ |
| 86 | + | grep -vE 'resourceVersion|uid|creationTimestamp' \ |
| 87 | + | kubectl apply -n opencrvs-${ENV} -f - \ |
| 88 | + || echo "Secret $secret doesn't exist in opencrvs-deps-${ENV} namespace" |
| 89 | + done |
| 90 | + - name: Deploy with Helm |
| 91 | + run: | |
| 92 | + helm upgrade --install opencrvs oci://ghcr.io/opencrvs/opencrvs-services \ |
| 93 | + --timeout 15m \ |
| 94 | + --namespace "opencrvs-${ENV}" \ |
| 95 | + -f environments/${ENV}/opencrvs-services/values.yaml \ |
| 96 | + --create-namespace \ |
| 97 | + --atomic \ |
| 98 | + --wait \ |
| 99 | + --wait-for-jobs \ |
| 100 | + --set image.tag="$CORE_IMAGE_TAG" \ |
| 101 | + --set countryconfig.image.tag="$COUNTRYCONFIG_IMAGE_TAG" \ |
| 102 | + --set countryconfig.image.name="$COUNTRYCONFIG_IMAGE_NAME" \ |
| 103 | + --set hostname=${{ vars.DOMAIN }} |
| 104 | + - name: Cleanup Helm Locks |
| 105 | + if: failure() || cancelled() |
| 106 | + run: | |
| 107 | + kubectl -n "opencrvs-${ENV}" get secrets -l owner=helm -o json | \ |
| 108 | + jq -r '.items[] | select(.metadata.labels.status=="pending-install" or .metadata.labels.status=="pending-upgrade") | .metadata.name' | \ |
| 109 | + xargs -r kubectl -n "opencrvs-${ENV}" delete secret || \ |
| 110 | + echo "No helm locks found, all is good" |
0 commit comments