Make GCP artifact publish idempotent (#57)
Some checks are pending
CI / lint-and-test (push) Waiting to run
Some checks are pending
CI / lint-and-test (push) Waiting to run
This commit is contained in:
parent
464d1b3fd9
commit
b9acc4855c
2 changed files with 23 additions and 12 deletions
7
.github/workflows/gcp-artifact.yml
vendored
7
.github/workflows/gcp-artifact.yml
vendored
|
|
@ -48,7 +48,13 @@ jobs:
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
tag="${GITHUB_SHA::7}"
|
tag="${GITHUB_SHA::7}"
|
||||||
image_uri="${REGION}-docker.pkg.dev/${PROJECT_ID}/${ARTIFACT_REPOSITORY}/${IMAGE_NAME}:${tag}"
|
image_uri="${REGION}-docker.pkg.dev/${PROJECT_ID}/${ARTIFACT_REPOSITORY}/${IMAGE_NAME}:${tag}"
|
||||||
|
image_digest="$(gcloud artifacts docker images describe "${image_uri}" --format='value(image_summary.digest)' 2>/dev/null || true)"
|
||||||
|
|
||||||
|
if [[ -n "${image_digest}" ]]; then
|
||||||
|
echo "Image tag already exists; reusing immutable Artifact Registry image."
|
||||||
|
docker pull "${image_uri}@${image_digest}"
|
||||||
|
docker run --rm "${image_uri}@${image_digest}"
|
||||||
|
else
|
||||||
docker build \
|
docker build \
|
||||||
-f Dockerfile.gcp-staging \
|
-f Dockerfile.gcp-staging \
|
||||||
--label "org.opencontainers.image.source=https://github.com/${GITHUB_REPOSITORY}" \
|
--label "org.opencontainers.image.source=https://github.com/${GITHUB_REPOSITORY}" \
|
||||||
|
|
@ -62,6 +68,7 @@ jobs:
|
||||||
docker run --rm "${image_uri}"
|
docker run --rm "${image_uri}"
|
||||||
docker push "${image_uri}" | tee docker-push.log
|
docker push "${image_uri}" | tee docker-push.log
|
||||||
image_digest="$(awk '/digest: sha256:/ {print $3}' docker-push.log | tail -1)"
|
image_digest="$(awk '/digest: sha256:/ {print $3}' docker-push.log | tail -1)"
|
||||||
|
fi
|
||||||
test -n "${image_digest}"
|
test -n "${image_digest}"
|
||||||
|
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,10 @@ from pathlib import Path
|
||||||
def test_gcp_artifact_workflow_retains_immutable_image_digest() -> None:
|
def test_gcp_artifact_workflow_retains_immutable_image_digest() -> None:
|
||||||
workflow = Path(".github/workflows/gcp-artifact.yml").read_text()
|
workflow = Path(".github/workflows/gcp-artifact.yml").read_text()
|
||||||
|
|
||||||
|
assert "gcloud artifacts docker images describe" in workflow
|
||||||
|
assert "Image tag already exists; reusing immutable Artifact Registry image." in workflow
|
||||||
|
assert 'docker pull "${image_uri}@${image_digest}"' in workflow
|
||||||
|
assert 'docker run --rm "${image_uri}@${image_digest}"' in workflow
|
||||||
assert "docker push" in workflow
|
assert "docker push" in workflow
|
||||||
assert "tee docker-push.log" in workflow
|
assert "tee docker-push.log" in workflow
|
||||||
assert "image_digest=" in workflow
|
assert "image_digest=" in workflow
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue