diff --git a/.github/workflows/deploy-cloudrun.yaml b/.github/workflows/deploy-cloudrun.yaml new file mode 100644 index 00000000..5108a32a --- /dev/null +++ b/.github/workflows/deploy-cloudrun.yaml @@ -0,0 +1,92 @@ +name: 'Reusable gcp cloudrun deploy workflow' + +on: + workflow_call: + inputs: + job-name: + description: 'Cloud Run Job name (at least one of job-name or service-name required)' + required: false + type: string + service-name: + description: 'Cloud Run Service name (at least one of job-name or service-name required)' + required: false + type: string + registry: + description: 'Container registry URL (e.g., gcr.io, europe-west3-docker.pkg.dev)' + required: true + type: string + artifact-repository: + description: 'Image repository name without registry prefix (e.g., my-image)' + required: true + type: string + artifact-tag: + description: 'Image tag of the container image to deploy' + required: true + type: string + region: + description: 'GCP region for Cloud Run deployment (e.g., europe-west3)' + required: true + type: string + flags: + description: 'Additional flags to pass to the gcloud deploy command' + type: string + container: + description: 'Container name for multi-container deployments' + type: string + workload-identity-provider: + description: 'GCP Workload Identity Provider resource name' + required: true + type: string + workload-identity-service-account-mail: + description: 'GCP service account email for workload identity OIDC authentication' + required: true + type: string +jobs: + deploy_cloudrun: + name: Deployment job + permissions: + contents: 'read' + id-token: 'write' + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Validate inputs + run: | + if [[ -z "${{ inputs.job-name }}" && -z "${{ inputs.service-name }}" ]]; then + echo "::error::At least one of 'job-name' or 'service-name' must be provided" + exit 1 + fi + - uses: 'google-github-actions/auth@v3' + with: + service_account: ${{ inputs.workload-identity-service-account-mail }} + workload_identity_provider: ${{ inputs.workload-identity-provider }} + - name: 'Set up gcloud CLI' + uses: 'google-github-actions/setup-gcloud@v3' + with: + install_components: 'beta' + + # we cannot use google-github-actions/deploy-cloudrun here due to: + # https://github.com/google-github-actions/deploy-cloudrun/issues/558 + - name: 'deploy cloudrun' + env: + CONTAINER: ${{ inputs.container }} + run: | + if [ -n "$CONTAINER" ]; then + CONTAINER_FLAG="--container $CONTAINER" + else + CONTAINER_FLAG="" + fi + IMAGE="${{ inputs.registry }}/${{ inputs.artifact-repository }}:${{ inputs.artifact-tag }}" + EXTRA_FLAGS="${{ inputs.flags }}" + gcloud config set run/region "${{ inputs.region }}" + if [ -n "${{ inputs.job-name }}" ]; then + gcloud beta run jobs deploy "${{ inputs.job-name }}" \ + ${CONTAINER_FLAG} \ + --image "$IMAGE" \ + ${EXTRA_FLAGS} + else + gcloud run deploy "${{ inputs.service-name }}" \ + ${CONTAINER_FLAG} \ + --image "$IMAGE" \ + ${EXTRA_FLAGS} + fi \ No newline at end of file diff --git a/README.md b/README.md index 93d97246..023d70a2 100644 --- a/README.md +++ b/README.md @@ -122,6 +122,12 @@ Template: When a GH commit URL is included in commit message, link the commit from said comment. +### Cloud Run Deployment + +Deploy images to Google Cloud Run services or jobs. + +See [deploy-cloudrun.yaml](./.github/workflows/deploy-cloudrun.yaml) for details. + ### Cloud Function Deployment Deploy code to Google Cloud Function.