|
| 1 | +name: cd |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: main |
| 6 | + |
| 7 | + workflow_dispatch: |
| 8 | + inputs: |
| 9 | + debug_enabled: |
| 10 | + description: "Enable tmate debug" |
| 11 | + type: boolean |
| 12 | + default: "false" |
| 13 | + |
| 14 | +jobs: |
| 15 | + info: |
| 16 | + runs-on: ubuntu-latest |
| 17 | + |
| 18 | + timeout-minutes: 5 |
| 19 | + |
| 20 | + permissions: |
| 21 | + contents: read |
| 22 | + |
| 23 | + outputs: |
| 24 | + branch_tag_kebab: ${{ steps.get-deployment-info.outputs.branch_tag_kebab }} |
| 25 | + branch_tag_screaming: ${{ steps.get-deployment-info.outputs.branch_tag_screaming}} |
| 26 | + image_latest_artefact: ${{ steps.get-deployment-info.outputs.image_latest_artefact}} |
| 27 | + image_latest_tag: ${{ steps.get-deployment-info.outputs.image_latest_tag }} |
| 28 | + image_version_artefact: ${{ steps.get-deployment-info.outputs.image_version_artefact}} |
| 29 | + image_version_tag: ${{ steps.get-deployment-info.outputs.image_version_tag }} |
| 30 | + short_sha: ${{ steps.get-deployment-info.outputs.short_sha }} |
| 31 | + gcp_project_name: ${{ steps.get-deployment-info.outputs.gcp_project_name}} |
| 32 | + gcp_project_number: ${{ steps.get-deployment-info.outputs.gcp_project_number}} |
| 33 | + gcp_region: ${{ steps.get-deployment-info.outputs.gcp_region}} |
| 34 | + gcp_resource_affix: ${{ steps.get-deployment-info.outputs.gcp_resource_affix}} |
| 35 | + gcp_service_name: ${{ steps.get-deployment-info.outputs.gcp_service_name}} |
| 36 | + version: ${{ steps.get-deployment-info.outputs.version }} |
| 37 | + version_slug: ${{ steps.get-deployment-info.outputs.version_slug }} |
| 38 | + |
| 39 | + steps: |
| 40 | + - name: Checkout |
| 41 | + uses: actions/checkout@v3 |
| 42 | + |
| 43 | + - name: Install poetry |
| 44 | + uses: snok/install-poetry@v1 |
| 45 | + |
| 46 | + - name: Get deployment info |
| 47 | + id: get-deployment-info |
| 48 | + |
| 49 | + with: |
| 50 | + gcp_project_name: octue-sdk-python |
| 51 | + gcp_project_number: 437801218871 |
| 52 | + gcp_region: europe-west1 |
| 53 | + gcp_resource_affix: octue |
| 54 | + gcp_service_name: example-service-cloud-run |
| 55 | + gcp_environment: main |
| 56 | + |
| 57 | + build: |
| 58 | + runs-on: ubuntu-latest |
| 59 | + timeout-minutes: 60 |
| 60 | + needs: info |
| 61 | + permissions: |
| 62 | + id-token: write |
| 63 | + contents: read |
| 64 | + |
| 65 | + steps: |
| 66 | + - name: Checkout |
| 67 | + uses: actions/checkout@v3 |
| 68 | + |
| 69 | + - name: Set up QEMU |
| 70 | + uses: docker/setup-qemu-action@v2 |
| 71 | + |
| 72 | + - name: Set up Docker Buildx |
| 73 | + uses: docker/setup-buildx-action@v2 |
| 74 | + |
| 75 | + - name: Authenticate with GCP Workload Identity |
| 76 | + id: auth |
| 77 | + uses: google-github-actions/auth@v0 |
| 78 | + with: |
| 79 | + # NOTE: If setting create_credentials_file=true, .dockerignore file must include `gha-creds-*.json` to avoid baking these credentials into build |
| 80 | + create_credentials_file: true |
| 81 | + workload_identity_provider: projects/${{ needs.info.outputs.gcp_project_number }}/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider |
| 82 | + service_account: github-actions@${{ needs.info.outputs.gcp_project_name }}.iam.gserviceaccount.com |
| 83 | + |
| 84 | + - name: Setup gcloud |
| 85 | + uses: "google-github-actions/setup-gcloud@v0" |
| 86 | + |
| 87 | + - name: Configure Docker for GCP |
| 88 | + run: gcloud auth configure-docker ${{ needs.info.outputs.gcp_region }}-docker.pkg.dev |
| 89 | + |
| 90 | + - name: Setup tmate session [DEBUG] |
| 91 | + if: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.debug_enabled == 'true'}} |
| 92 | + uses: mxschmitt/action-tmate@v3 |
| 93 | + |
| 94 | + - name: Get default Octue Cloud Run Dockerfile |
| 95 | + run: wget https://raw.githubusercontent.com/octue/octue-sdk-python/main/octue/cloud/deployment/google/cloud_run/Dockerfile |
| 96 | + |
| 97 | + - name: Build and push container with latest and version tags |
| 98 | + # Note: We don't push containers with shas because we'd end up with terabytes in storage (an image for every commit) |
| 99 | + uses: docker/build-push-action@v2 |
| 100 | + with: |
| 101 | + context: . |
| 102 | + platforms: linux/amd64 |
| 103 | + file: ./Dockerfile |
| 104 | + push: true |
| 105 | + cache-from: type=gha |
| 106 | + cache-to: type=gha,mode=max |
| 107 | + tags: | |
| 108 | + ${{ needs.info.outputs.image_version_artefact}} |
| 109 | + ${{ needs.info.outputs.image_latest_artefact}} |
| 110 | +
|
| 111 | + - name: Deploy to Cloud Run service |
| 112 | + id: deploy-service |
| 113 | + uses: google-github-actions/deploy-cloudrun@v0 |
| 114 | + with: |
| 115 | + env_vars: | |
| 116 | + OCTUE_SERVICE_NAMESPACE=${{ needs.info.outputs.gcp_resource_affix }} |
| 117 | + OCTUE_SERVICE_NAME=${{ needs.info.outputs.gcp_service_name }} |
| 118 | + OCTUE_SERVICE_REVISION_TAG=${{ needs.info.outputs.version_slug }} |
| 119 | + COMPUTE_PROVIDER=GOOGLE_CLOUD_RUN |
| 120 | + service: ${{ needs.info.outputs.gcp_resource_affix }}-${{ needs.info.outputs.gcp_service_name }} |
| 121 | + image: ${{ needs.info.outputs.image_version_artefact }} |
| 122 | + region: ${{ needs.info.outputs.gcp_region }} |
| 123 | + tag: v${{ needs.info.outputs.version_slug }} |
| 124 | + flags: "--allow-unauthenticated" |
| 125 | + |
| 126 | + - name: Show deployed service URL |
| 127 | + run: echo "${{ steps.deploy-service.outputs.url }}" |
| 128 | + |
| 129 | + - name: Create topic and subscription |
| 130 | + |
| 131 | + with: |
| 132 | + project_name: ${{ needs.info.outputs.gcp_project_name }} |
| 133 | + service_namespace: ${{ needs.info.outputs.gcp_resource_affix }} |
| 134 | + service_name: ${{ needs.info.outputs.gcp_service_name }} |
| 135 | + service_revision_tag: ${{ needs.info.outputs.version_slug }} |
| 136 | + push_endpoint: ${{ steps.deploy-service.outputs.url }} |
0 commit comments