Newsletter
TechAnV Blog
Get updates on security engineering, Rust, eBPF, and DevSecOps. No spam, unsubscribe anytime.
Check your inbox and click the confirmation link to complete your subscription.
Using build-arg variables with Cloud Run deployments#
For datasette/issues/1522 I wanted to use a Docker build argument in a Dockerfile that would then be deployed to Cloud Run.
I needed this to be able to control the version of Datasette that was deployed. Here’s my simplified Dockerfile:
1FROM python:3-alpine2
3ARG DATASETTE_REF4# Copy to environment variable for use in CMD later5ENV VERSION_NOTE=$DATASETTE_REF6
7RUN pip install https://github.com/simonw/datasette/archive/${DATASETTE_REF}.zip8
9# Need to use "shell form" here to get variable substition:10CMD datasette -h 0.0.0.0 -p 8080 --version-note $VERSION_NOTEI can build this on my laptop like so:
1docker build -t datasette-build-arg-demo . \2 --build-arg DATASETTE_REF=c617e1769ea27e045b0f2907ef49a9a1244e577dThen run it like this:
1docker run -p 5000:8080 --rm datasette-build-arg-demoAnd visit http://localhost:5000/-/versions to see the version number to confirm it worked.
I wanted to deploy this to Cloud Run, using this recipe.
Unfortunately, the gcloud builds submit command doesn’t have a mechanism for specifying --build-arg.
Instead, you need to use a YAML file and pass it with the gcloud builds submit --config cloudbuild.yml option. The YAML should look like this:
1steps:2- name: 'gcr.io/cloud-builders/docker'3 args: ['build', '-t', 'gc.io/MY-PROJECT/MY-NAME', '.', '--build-arg', 'DATASETTE_REF=c617e1769ea27e045b0f2907ef49a9a1244e577d']4- name: 'gcr.io/cloud-builders/docker'5 args: ['push', $IMAGE]Since I want to dynamically populate my YAML file, I ended up using the following pattern in a ./deploy.sh script:
1#!/bin/bash2# https://til.simonwillison.net/cloudrun/using-build-args-with-cloud-run3
4if [[ -z "$DATASETTE_REF" ]]; then5 echo "Must provide DATASETTE_REF environment variable" 1>&26 exit 17fi8
9NAME="datasette-apache-proxy-demo"10PROJECT=$(gcloud config get-value project)11IMAGE="gcr.io/$PROJECT/$NAME"12
13# Need YAML so we can set --build-arg14echo "15steps:16- name: 'gcr.io/cloud-builders/docker'17 args: ['build', '-t', '$IMAGE', '.', '--build-arg', 'DATASETTE_REF=$DATASETTE_REF']18- name: 'gcr.io/cloud-builders/docker'19 args: ['push', '$IMAGE']20" > /tmp/cloudbuild.yml21
22gcloud builds submit --config /tmp/cloudbuild.yml23
24rm /tmp/cloudbuild.yml25
26gcloud run deploy $NAME \27 --allow-unauthenticated \28 --platform=managed \29 --image $IMAGE \30 --port 80