Skip to content

Commit

Permalink
Add ID token auth for invoking cloud run service (#707)
Browse files Browse the repository at this point in the history
* liquibase manual deploy workflow

* fixed lint

* added cloud run files

* fixed credentials typing

* added exception

* added cloud run config

* fixed the image and updated commands

* moved liquibase to Dockerfile

* added workflow to trigger the schema update

* added region param to workflow gcloud calls

* added temp dir and cleaned code

* amended job name on workflow

* added job permissions for google auth

* added gcs for cloud build logs

* added branch check to restrict to main and dev

* added branch check to restrict to main and dev

* fixed logical operator for branch check

* bumped actions versions and added ID token auth

* refactor step ids

* reverted to print-identity-token with audiences param
  • Loading branch information
nevoodoo authored Mar 15, 2024
1 parent 515e8da commit fa166b6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
13 changes: 10 additions & 3 deletions .github/workflows/deploy_schema_updater.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ jobs:
if: github.ref == 'refs/heads/main'

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- id: "branch-check"
name: Fail if branch is not main or dev
if: github.event_name == 'workflow_dispatch' && (github.ref != 'refs/heads/main' && github.ref != 'refs/heads/dev')
run: |
echo "This workflow should not be triggered with workflow_dispatch on a branch other than main or dev"
exit 1
- id: "branch-check"
name: Fail if branch is not main or dev
Expand All @@ -25,14 +32,14 @@ jobs:
- id: "google-cloud-auth"
name: "Authenticate to Google Cloud"
uses: "google-github-actions/auth@v1"
uses: "google-github-actions/auth@v2"
with:
workload_identity_provider: "projects/774248915715/locations/global/workloadIdentityPools/gh-deploy-pool/providers/gh-provider"
service_account: "[email protected]"

- id: "google-cloud-sdk-setup"
name: "Set up Cloud SDK"
uses: google-github-actions/setup-gcloud@v1
uses: google-github-actions/setup-gcloud@v2

- name: Build image with Cloud Build
run: |
Expand Down
27 changes: 15 additions & 12 deletions .github/workflows/trigger_schema_updater.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@ jobs:
invoke-cloud-run:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- id: "branch-check"
name: Fail if branch is not main or dev
if: github.event_name == 'workflow_dispatch' && (github.ref != 'refs/heads/main' && github.ref != 'refs/heads/dev')
run: |
echo "This workflow should not be triggered with workflow_dispatch on a branch other than main or dev"
exit 1
- id: "branch-check"
name: Fail if branch is not main or dev
Expand All @@ -31,22 +38,18 @@ jobs:
- id: "google-cloud-auth"
name: "Authenticate to Google Cloud"
uses: "google-github-actions/auth@v1"
uses: "google-github-actions/auth@v2"
with:
workload_identity_provider: "projects/774248915715/locations/global/workloadIdentityPools/gh-deploy-pool/providers/gh-provider"
service_account: "[email protected]"

- id: "google-cloud-sdk-setup"
name: "Set up Cloud SDK"
uses: google-github-actions/setup-gcloud@v1

- id: get_url
name: Get Cloud Run service URL
run: |
echo "CLOUD_RUN_URL=$(gcloud run services describe schema-updater --region australia-southeast1 --format 'value(status.url)')/execute-liquibase?environment=${{ github.event.inputs.environment }}" >> $GITHUB_ENV
uses: google-github-actions/setup-gcloud@v2

- name: Manual Trigger - Invoke Cloud Run
- id: "invoke-cloud-run"
name: "Invoke Cloud Run"
run: |
curl -X POST -H "Authorization: Bearer $(gcloud auth print-identity-token)" -H "Content-Type: application/xml" --data-binary "@db/project.xml" $CLOUD_RUN_URL
env:
CLOUD_RUN_URL: ${{ env.CLOUD_RUN_URL }}
CLOUD_RUN_URL=$(gcloud run services describe schema-updater --region australia-southeast1 --format 'value(status.url)')/execute-liquibase?environment=${{ github.event.inputs.environment }}"
TOKEN=$(gcloud auth print-identity-token --impersonate-service-account="[email protected]" --audiences="$CLOUD_RUN_URL" --include-email)
curl -X POST -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/xml" --data-binary "@db/project.xml" $CLOUD_RUN_URL

0 comments on commit fa166b6

Please sign in to comment.