|
| 1 | +name: Promote to production (reusable) |
| 2 | + |
| 3 | +# Reusable "promote to production" workflow. |
| 4 | +# |
| 5 | +# Reads the tag currently deployed to staging from the infrastructure |
| 6 | +# repository's kustomization, copies that image from the staging registry |
| 7 | +# to the production registry (both `:tag` and `:latest`), and updates the |
| 8 | +# production deployment. |
| 9 | + |
| 10 | +on: |
| 11 | + workflow_call: |
| 12 | + inputs: |
| 13 | + image_name: |
| 14 | + description: "Image name, e.g. robotics/sara-anonymizer." |
| 15 | + required: true |
| 16 | + type: string |
| 17 | + staging_registry: |
| 18 | + required: true |
| 19 | + type: string |
| 20 | + production_registry: |
| 21 | + required: true |
| 22 | + type: string |
| 23 | + infrastructure_repository: |
| 24 | + required: true |
| 25 | + type: string |
| 26 | + author_name: |
| 27 | + required: true |
| 28 | + type: string |
| 29 | + secrets: |
| 30 | + staging_registry_username: |
| 31 | + required: true |
| 32 | + staging_registry_password: |
| 33 | + required: true |
| 34 | + production_registry_username: |
| 35 | + required: true |
| 36 | + production_registry_password: |
| 37 | + required: true |
| 38 | + deploy_key: |
| 39 | + required: true |
| 40 | + |
| 41 | +permissions: |
| 42 | + contents: read |
| 43 | + packages: write |
| 44 | + |
| 45 | +jobs: |
| 46 | + get-staging-version: |
| 47 | + name: Get version currently in staging |
| 48 | + runs-on: ubuntu-latest |
| 49 | + outputs: |
| 50 | + version_tag: ${{ steps.get.outputs.tag }} |
| 51 | + steps: |
| 52 | + - name: Checkout infrastructure |
| 53 | + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd #v6 |
| 54 | + with: |
| 55 | + ref: main |
| 56 | + repository: ${{ inputs.infrastructure_repository }} |
| 57 | + ssh-key: ${{ secrets.deploy_key }} |
| 58 | + |
| 59 | + - name: Read staging tag from kustomization |
| 60 | + id: get |
| 61 | + run: | |
| 62 | + KUSTOMIZATION=k8s_kustomize/overlays/staging/kustomization.yaml |
| 63 | + IMAGE_LINE=$(grep -n "newName: ${{ inputs.staging_registry }}/${{ inputs.image_name }}$" "$KUSTOMIZATION" | cut -d: -f1) |
| 64 | + if [ -z "$IMAGE_LINE" ]; then |
| 65 | + echo "Error: image ${{ inputs.staging_registry }}/${{ inputs.image_name }} not found in $KUSTOMIZATION" |
| 66 | + exit 1 |
| 67 | + fi |
| 68 | + TAG_LINE=$((IMAGE_LINE + 1)) |
| 69 | + VERSION_TAG=$(sed -n "${TAG_LINE}p" "$KUSTOMIZATION" | awk -F': ' '{print $2}' | tr -d ' ') |
| 70 | + if [ -z "$VERSION_TAG" ]; then |
| 71 | + echo "Error: could not parse newTag on line ${TAG_LINE} of $KUSTOMIZATION" |
| 72 | + exit 1 |
| 73 | + fi |
| 74 | + echo "tag=$VERSION_TAG" >> "$GITHUB_OUTPUT" |
| 75 | +
|
| 76 | + copy-image-to-production: |
| 77 | + name: Copy staging image to production registry |
| 78 | + needs: get-staging-version |
| 79 | + runs-on: ubuntu-latest |
| 80 | + steps: |
| 81 | + - name: Log in to staging registry |
| 82 | + uses: docker/login-action@4907a6ddec9925e35a0a9e82d7399ccc52663121 #v4 |
| 83 | + with: |
| 84 | + registry: ${{ inputs.staging_registry }} |
| 85 | + username: ${{ secrets.staging_registry_username }} |
| 86 | + password: ${{ secrets.staging_registry_password }} |
| 87 | + |
| 88 | + - name: Log in to production registry |
| 89 | + uses: docker/login-action@4907a6ddec9925e35a0a9e82d7399ccc52663121 #v4 |
| 90 | + with: |
| 91 | + registry: ${{ inputs.production_registry }} |
| 92 | + username: ${{ secrets.production_registry_username }} |
| 93 | + password: ${{ secrets.production_registry_password }} |
| 94 | + |
| 95 | + - name: Copy image (tag + latest) from staging to production |
| 96 | + env: |
| 97 | + TAG: ${{ needs.get-staging-version.outputs.version_tag }} |
| 98 | + run: | |
| 99 | + docker buildx imagetools create \ |
| 100 | + --tag ${{ inputs.production_registry }}/${{ inputs.image_name }}:${TAG} \ |
| 101 | + --tag ${{ inputs.production_registry }}/${{ inputs.image_name }}:latest \ |
| 102 | + ${{ inputs.staging_registry }}/${{ inputs.image_name }}:${TAG} |
| 103 | +
|
| 104 | + deploy: |
| 105 | + name: Update deployment in production |
| 106 | + needs: [get-staging-version, copy-image-to-production] |
| 107 | + uses: ./.github/workflows/update_kubernetes_deployment.yml |
| 108 | + with: |
| 109 | + environment: production |
| 110 | + registry: ${{ inputs.production_registry }} |
| 111 | + image_name: ${{ inputs.image_name }} |
| 112 | + tag: ${{ needs.get-staging-version.outputs.version_tag }} |
| 113 | + author_name: ${{ inputs.author_name }} |
| 114 | + infrastructure_repository: ${{ inputs.infrastructure_repository }} |
| 115 | + secrets: |
| 116 | + deploy_key: ${{ secrets.deploy_key }} |
0 commit comments