Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions .github/workflows/deploy-cloudrun.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
name: 'Reusable gcp cloudrun deploy workflow'

on:
workflow_call:
inputs:
job-name:
description: 'Name of the Cloud Run job to deploy'
required: false
type: string
service-name:
description: 'Name of the Cloud Run service to deploy'
required: false
type: string
Comment thread
coderabbitai[bot] marked this conversation as resolved.
registry:
description: 'Container registry URL (e.g., gcr.io, europe-west3-docker.pkg.dev)'
required: true
type: string
artifact-repository:
description: 'Repository name in the artifact registry'
required: true
type: string
artifact-tag:
description: 'Tag of the container image to deploy'
required: true
type: string
region:
description: 'GCP region where the Cloud Run resource will be deployed'
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: 'Workload Identity Provider for authentication'
required: true
type: string
workload-identity-service-account-mail:
description: 'Service account email for Workload Identity'
required: true
type: string
Comment thread
coderabbitai[bot] marked this conversation as resolved.
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"
Comment thread
marek-saji marked this conversation as resolved.
Outdated
Comment thread
marek-saji marked this conversation as resolved.
Outdated
exit 1
fi
Comment thread
coderabbitai[bot] marked this conversation as resolved.
- 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:
version: '>= 363.0.0'
Comment thread
marek-saji marked this conversation as resolved.
Outdated
install_components: 'beta'
- name: 'Prepare deployment flags'
id: flags
run: |
if [ -n "${{ inputs.container }}" ]; then
echo "container_flag=--container ${{ inputs.container }}" >> "$GITHUB_OUTPUT"
Comment thread
marek-saji marked this conversation as resolved.
Outdated
else
echo "container_flag=" >> "$GITHUB_OUTPUT"
fi
Comment thread
marek-saji marked this conversation as resolved.
Outdated
# we cannot use google-github-actions/deploy-cloudrun here due to:
# https://github.com/google-github-actions/deploy-cloudrun/issues/558
- name: 'deploy cloudrun'
run: |
IMAGE="${{ inputs.registry }}/${{ inputs.artifact-repository }}:${{ inputs.artifact-tag }}"
CONTAINER_FLAG="${{ steps.flags.outputs.container_flag }}"
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}
Comment on lines +71 to +91

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick | 🔵 Trivial

Improve shell robustness: quote variable assignments to prevent word-splitting issues.

The deployment step constructs flag strings that could be vulnerable to word-splitting if inputs contain spaces or special characters. While container names and flags typically don't have such characters in practice, shell best practices suggest explicit quoting.

Current approach:

CONTAINER_FLAG="--container $CONTAINER"
# Later used as: ${CONTAINER_FLAG}

If inputs.container contains unexpected characters, the construction could fail.

Consider refactoring to apply the env var directly and quote the variable value:

  env:
    CONTAINER: ${{ inputs.container }}
  run: |
    if [ -n "$CONTAINER" ]; then
-     CONTAINER_FLAG="--container $CONTAINER"
+     CONTAINER_FLAG="--container \"$CONTAINER\""
    else
      CONTAINER_FLAG=""
    fi
    IMAGE="${{ inputs.registry }}/${{ inputs.artifact-repository }}:${{ inputs.artifact-tag }}"
-   EXTRA_FLAGS="${{ inputs.flags }}"
+   EXTRA_FLAGS="${{ inputs.flags }}"

Alternatively, conditionally inject the flag directly into the gcloud command to avoid intermediate variable construction.

This aligns with the previous review suggestion to guard against weird characters in inputs.

🤖 Prompt for AI Agents
.github/workflows/deploy-cloudrun.yaml lines 71-91: the script builds flag
strings with unquoted variable expansions (e.g., CONTAINER_FLAG="--container
$CONTAINER" and later ${CONTAINER_FLAG}), which can cause word-splitting if
inputs contain spaces/special chars; change to either (a) avoid intermediate
flag concatenation and inject the flag directly into the gcloud call using a
conditional that passes "--container" "$CONTAINER" when CONTAINER is non-empty,
or (b) quote expansions when building and expanding the variables (e.g.,
CONTAINER_FLAG="--container \"$CONTAINER\"" and use "${CONTAINER_FLAG}" ), and
also quote IMAGE and EXTRA_FLAGS usages in the gcloud commands to ensure robust
handling of spaces/special characters.

Comment thread
smelchior marked this conversation as resolved.
fi
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,16 @@ Template:

When a GH commit URL is included in commit message, link the commit from said comment.

### Cloudrun Deployment
Comment thread
smelchior marked this conversation as resolved.
Outdated

Template:
<https://github.com/verkstedt/.github/tree/main/workflow-templates/deploy-cloudrun.yaml>
Comment thread
marek-saji marked this conversation as resolved.
Outdated

Deploy images to google cloudrun services or jobs.

See [deploy-cloudrun.yaml](./.github/workflows/deploy-cloudrun.yaml) for details.
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated


## Deploying new versions of actions and workflows

You might have noticed that main branch in this repository is called
Expand Down