|
| 1 | +name: Build and deploy to GKE |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + project_id: |
| 7 | + description: 'Google Cloud Project ID' |
| 8 | + required: true |
| 9 | + type: string |
| 10 | + cluster: |
| 11 | + description: 'GKE Cluster Name' |
| 12 | + required: true |
| 13 | + type: string |
| 14 | + cluster_region: |
| 15 | + description: 'GKE Cluster Region' |
| 16 | + required: true |
| 17 | + type: string |
| 18 | + artifact_registry: |
| 19 | + description: 'Artifact Registry Name' |
| 20 | + required: true |
| 21 | + type: string |
| 22 | + registry_region: |
| 23 | + description: 'Artifact Registry Region' |
| 24 | + required: true |
| 25 | + type: string |
| 26 | + deployment: |
| 27 | + description: 'Kubernetes Deployment Name' |
| 28 | + required: true |
| 29 | + type: string |
| 30 | + image: |
| 31 | + description: 'Docker Image Name' |
| 32 | + required: true |
| 33 | + type: string |
| 34 | + project_number: |
| 35 | + description: 'Google Cloud Project Number' |
| 36 | + required: true |
| 37 | + type: string |
| 38 | + service_account: |
| 39 | + description: 'Google Cloud Service Account Email' |
| 40 | + required: true |
| 41 | + type: string |
| 42 | + |
| 43 | +concurrency: |
| 44 | + group: deploy-${{ github.repository }}-${{ github.ref }} |
| 45 | + cancel-in-progress: true |
| 46 | + |
| 47 | +permissions: |
| 48 | + contents: read |
| 49 | + id-token: write |
| 50 | + |
| 51 | +jobs: |
| 52 | + deploy: |
| 53 | + runs-on: ubuntu-latest |
| 54 | + steps: |
| 55 | + - name: Checkout Repository |
| 56 | + uses: actions/checkout@v4 |
| 57 | + |
| 58 | + - name: Authenticate to Google Cloud |
| 59 | + id: auth |
| 60 | + uses: google-github-actions/auth@v2 |
| 61 | + with: |
| 62 | + workload_identity_provider: projects/${{ inputs.project_number }}/locations/global/workloadIdentityPools/orsp-dev-workloadpool/providers/github |
| 63 | + service_account: ${{ inputs.service_account }} |
| 64 | + |
| 65 | + - name: Setup Gcloud |
| 66 | + uses: google-github-actions/setup-gcloud@v2 |
| 67 | + |
| 68 | + - name: Docker Login |
| 69 | + uses: docker/login-action@v3 |
| 70 | + with: |
| 71 | + registry: ${{ inputs.registry_region }}-docker.pkg.dev |
| 72 | + username: oauth2accesstoken |
| 73 | + password: ${{ steps.auth.outputs.auth_token }} |
| 74 | + |
| 75 | + - name: Extract Tag |
| 76 | + run: | |
| 77 | + TAG=${GITHUB_REF#refs/tags/} |
| 78 | + echo "IMAGE_URI=${{ inputs.registry_region }}-docker.pkg.dev/${{ inputs.project_id }}/${{ inputs.artifact_registry }}/${{ inputs.image }}:$TAG" >> $GITHUB_ENV |
| 79 | +
|
| 80 | + - name: Build and Push Docker Image |
| 81 | + uses: docker/build-push-action@v5 |
| 82 | + with: |
| 83 | + context: . |
| 84 | + push: true |
| 85 | + tags: ${{ env.IMAGE_URI }} |
| 86 | + cache-from: type=gha |
| 87 | + cache-to: type=gha,mode=max |
| 88 | + |
| 89 | + - name: Get GKE Credentials |
| 90 | + uses: google-github-actions/get-gke-credentials@v2 |
| 91 | + with: |
| 92 | + cluster_name: ${{ inputs.cluster }} |
| 93 | + location: ${{ inputs.cluster_region }} |
| 94 | + |
| 95 | + - name: Deploy to Kubernetes |
| 96 | + run: | |
| 97 | + set -e |
| 98 | + kubectl set image deployment/${{ inputs.deployment }} orsp-pub=$IMAGE_URI |
| 99 | + kubectl rollout status deployment/${{ inputs.deployment }} --timeout=10m |
| 100 | + kubectl get pods |
| 101 | + kubectl describe deployment ${{ inputs.deployment }} |
0 commit comments