Skip to content

Commit 2ab308b

Browse files
authored
add cosign support for signing container image (#5062)
Signed-off-by: Praful Khanduri <holiodin@gmail.com>
1 parent 3541a96 commit 2ab308b

2 files changed

Lines changed: 58 additions & 4 deletions

File tree

.github/workflows/publish-images.yaml

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ on:
99

1010
env:
1111
PLATFORMS: linux/amd64,linux/arm64,linux/s390x,linux/ppc64le
12+
GHCR_IMAGE: ghcr.io/${{ github.repository_owner }}/opentelemetry-operator/opentelemetry-operator
13+
DOCKERHUB_IMAGE: otel/opentelemetry-operator
1214

1315
permissions:
1416
contents: read
@@ -21,6 +23,9 @@ jobs:
2123
id-token: write
2224
name: Publish container images
2325
runs-on: ubuntu-latest
26+
outputs:
27+
digest: ${{ steps.build.outputs.digest }}
28+
image: ${{ steps.release-image.outputs.image }}
2429
steps:
2530
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
2631

@@ -32,7 +37,10 @@ jobs:
3237
run: git fetch --prune --unshallow
3338

3439
- name: Describe the current state
35-
run: git describe --tags
40+
run: |
41+
# make sure that tags are available, but don't fail the workflow if none exist.
42+
git fetch --tags || true
43+
git describe --tags || echo "no-tag-$(git rev-parse --short HEAD)"
3644
3745
- name: Set env vars for the job
3846
run: |
@@ -47,7 +55,8 @@ jobs:
4755
grep -v '\#' versions.txt | grep autoinstrumentation-apache-httpd | awk -F= '{print "AUTO_INSTRUMENTATION_APACHE_HTTPD_VERSION="$2}' >> $GITHUB_ENV
4856
grep -v '\#' versions.txt | grep autoinstrumentation-nginx | awk -F= '{print "AUTO_INSTRUMENTATION_NGINX_VERSION="$2}' >> $GITHUB_ENV
4957
echo "VERSION_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" >> $GITHUB_ENV
50-
echo "VERSION=$(git describe --tags | sed 's/^v//')" >> $GITHUB_ENV
58+
VERSION="$(git describe --tags 2>/dev/null || echo "no-tag-$(git rev-parse --short HEAD)")"
59+
echo "VERSION=${VERSION#v}" >> $GITHUB_ENV
5160
5261
- name: Build the binary for each supported architecture
5362
run: |
@@ -62,14 +71,18 @@ jobs:
6271
uses: docker/metadata-action@030e881283bb7a6894de51c315a6bfe6a94e05cf # v6.0.0
6372
with:
6473
images: |
65-
otel/opentelemetry-operator
66-
ghcr.io/open-telemetry/opentelemetry-operator/opentelemetry-operator
74+
${{ env.GHCR_IMAGE }}
75+
${{ env.DOCKERHUB_IMAGE }}
6776
tags: |
6877
type=semver,pattern={{version}}
6978
type=semver,pattern={{major}}.{{minor}}
7079
type=semver,pattern={{raw}}
7180
type=ref,event=branch
7281
82+
- name: Resolve release image name
83+
id: release-image
84+
run: echo "image=${GHCR_IMAGE}" >> "$GITHUB_OUTPUT"
85+
7386
- name: Set up QEMU
7487
uses: docker/setup-qemu-action@ce360397dd3f832beb865e1373c09c0e9f86d70a # v4.0.0
7588

@@ -100,6 +113,7 @@ jobs:
100113
password: ${{ secrets.GITHUB_TOKEN }}
101114

102115
- name: Build and push Operator image
116+
id: build
103117
uses: docker/build-push-action@bcafcacb16a39f128d818304e6c9c0c18556b85f # v7.1.0
104118
with:
105119
context: .
@@ -110,3 +124,11 @@ jobs:
110124
labels: ${{ steps.docker_meta.outputs.labels }}
111125
cache-from: type=local,src=/tmp/.buildx-cache
112126
cache-to: type=local,dest=/tmp/.buildx-cache
127+
128+
- name: Sign release images
129+
if: ${{ github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v') }}
130+
env:
131+
DIGEST: ${{ steps.build.outputs.digest }}
132+
run: |
133+
make cosign-sign IMAGE="${GHCR_IMAGE}" DIGEST="${DIGEST}"
134+
make cosign-sign IMAGE="${DOCKERHUB_IMAGE}" DIGEST="${DIGEST}"

Makefile

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -989,6 +989,38 @@ catalog-build: opm bundle-build bundle-push ## Build a catalog image.
989989
catalog-push: ## Push a catalog image.
990990
docker push $(CATALOG_IMG)
991991

992+
##@ Supply Chain Security
993+
994+
# Tool versions for supply chain securitya
995+
# renovate: datasource=github-releases depName=sigstore/cosign
996+
COSIGN_VERSION ?= v2.5.0
997+
COSIGN ?= $(LOCALBIN)/cosign
998+
999+
1000+
# Download cosign locally if necessary
1001+
.PHONY: cosign
1002+
cosign: $(LOCALBIN)
1003+
@{ \
1004+
set -e ;\
1005+
if [ -x "$(COSIGN)" ] && "$(COSIGN)" version 2>/dev/null | grep -q "$(COSIGN_VERSION)"; then exit 0; fi ;\
1006+
OS=$(shell go env GOOS) && ARCH=$(shell go env GOARCH) ;\
1007+
curl -sSfL "https://github.com/sigstore/cosign/releases/download/$(COSIGN_VERSION)/cosign-$${OS}-$${ARCH}" -o "$(COSIGN)" ;\
1008+
chmod +x "$(COSIGN)" ;\
1009+
}
1010+
1011+
# Sign container images with keyless cosign.
1012+
# Usage: make cosign-sign IMAGE=ghcr.io/... DIGEST=sha256:...
1013+
# Both IMAGE and DIGEST must be set.
1014+
.PHONY: cosign-sign
1015+
cosign-sign: cosign
1016+
ifndef IMAGE
1017+
$(error IMAGE is not set. Usage: make cosign-sign IMAGE=<image> DIGEST=<digest>)
1018+
endif
1019+
ifndef DIGEST
1020+
$(error DIGEST is not set. Usage: make cosign-sign IMAGE=<image> DIGEST=<digest>)
1021+
endif
1022+
$(COSIGN) sign --yes "$(IMAGE)@$(DIGEST)"
1023+
9921024
##@ Release
9931025

9941026
.PHONY: create-release-issue

0 commit comments

Comments
 (0)