-
Notifications
You must be signed in to change notification settings - Fork 5
193 lines (175 loc) · 7.17 KB
/
Copy pathbuild-and-publish.yaml
File metadata and controls
193 lines (175 loc) · 7.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# This workflow builds and pushes a Docker image to GHCR and Docker Hub
# under multiple repository aliases (auto_docker_proxy and traefik_network_connector).
#
# Triggers:
# 1. Push to 'main' branch (tags as 'latest')
# 2. Push of tags 'v*.*.*' (tags as SemVer)
# 3. Pull Requests (builds 'pr-XXX' tag, only if author is trusted or PR is approved)
#
# Features:
# - Multi-platform build
# - Multi-registry push (GHCR & Docker Hub)
# - GitHub Actions cache
# - Cosign OIDC signing for main/tag pushes
name: Build and Push Docker (Multi-Repo Alias)
on:
push:
branches:
- main
tags:
- 'v*.*.*' # Trigger on version tags like v1.0.0
pull_request:
types: [opened, synchronize, reopened]
jobs:
build-push-sign:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
id-token: write # Required for OIDC signing
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
# --- PR Security Checks (Start) ---
- name: Determine if PR author is trusted
if: ${{ github.event_name == 'pull_request' }}
id: check-author
shell: bash
run: |
# Define a list of trusted authors. Consider using a GitHub Team for larger projects.
trusted_authors=("obeone")
trusted="false"
for author in "${trusted_authors[@]}"; do
if [[ "$author" == "$GITHUB_ACTOR" ]]; then
trusted="true"
break
fi
done
echo "Author ($GITHUB_ACTOR) trusted: $trusted"
echo "trusted=$trusted" >> "$GITHUB_OUTPUT"
- name: Check for admin approval
if: ${{ github.event_name == 'pull_request' }}
id: check-approval
env:
GH_TOKEN: ${{ github.token }}
run: |
PR_NUMBER=${{ github.event.pull_request.number }}
# Check for approval by an organization OWNER or MEMBER.
APPROVALS=$(gh api repos/${{ github.repository }}/pulls/$PR_NUMBER/reviews \
--jq '.[] | select(.state=="APPROVED" and (.author_association=="OWNER" or .author_association=="MEMBER")) | .user.login')
if [[ -n "$APPROVALS" ]]; then
echo "PR is approved by an organization member: $APPROVALS"
echo "approved=true" >> "$GITHUB_OUTPUT"
else
echo "PR is not approved by an organization member."
echo "approved=false" >> "$GITHUB_OUTPUT"
fi
# --- PR Security Checks (End) ---
- name: Security Gatekeeper
id: gatekeeper
shell: bash
run: |
# This step consolidates the logic to determine if the build should proceed.
# The build will run if any of the following conditions are met:
# 1. The event is a 'push' (to 'main' branch or a tag).
# 2. The event is a 'pull_request' AND the author is explicitly trusted.
# 3. The event is a 'pull_request' AND the PR has been approved by an administrator.
if [[ "${{ github.event_name }}" == "push" ]]; then
echo "Event is 'push', proceeding with build."
echo "run_build=true" >> "$GITHUB_OUTPUT"
elif [[ "${{ steps.check-author.outputs.trusted }}" == "true" ]]; then
echo "PR author is trusted, proceeding with build."
echo "run_build=true" >> "$GITHUB_OUTPUT"
elif [[ "${{ steps.check-approval.outputs.approved }}" == "true" ]]; then
echo "PR is approved by an admin, proceeding with build."
echo "run_build=true" >> "$GITHUB_OUTPUT"
else
echo "PR event does not meet security criteria. Build will be skipped."
echo "run_build=false" >> "$GITHUB_OUTPUT"
fi
- name: Docker metadata (multi-repo)
id: meta
uses: docker/metadata-action@v5
with:
# Define all four image names. The generated tags will be applied to each of them.
images: |
ghcr.io/obeone/auto_docker_proxy
docker.io/obeoneorg/auto_docker_proxy
ghcr.io/obeone/traefik_network_connector
docker.io/obeoneorg/traefik_network_connector
tags: |
# For pushes to the 'main' branch, tag the image as 'latest'.
type=ref,event=branch,enable=${{ github.ref_name == 'main' }},prefix=,suffix=latest
# For 'v*.*.*' tags, generate SemVer tags (e.g., v1.2.3, v1.2, v1).
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
# For 'pull_request' events, tag the image as 'pr-XXX' (where XXX is the PR number).
type=ref,event=pr
- name: Build and push (multi-repo)
if: steps.gatekeeper.outputs.run_build == 'true'
id: build-and-push
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile
push: true
platforms: |
linux/amd64
linux/arm64
linux/i386
linux/armhf
linux/armel
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
build-args: |
# Pass the clean version (e.g., 1.0.0) extracted from metadata to the Dockerfile.
VERSION=${{ steps.meta.outputs.version }}
- name: Set up cosign
if: steps.gatekeeper.outputs.run_build == 'true'
uses: sigstore/cosign-installer@v3
- name: Sign the container image with cosign
# This step only signs official images, which are those built from 'main' branch pushes or tag pushes.
if: >-
${{
steps.gatekeeper.outputs.run_build == 'true' &&
github.event_name == 'push' &&
(github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v'))
}}
env:
COSIGN_EXPERIMENTAL: true
# Retrieve the image digest from the previous build step.
DIGEST: ${{ steps.build-and-push.outputs.digest }}
shell: bash
run: |
if [ -z "${DIGEST}" ]; then
echo "Digest is empty, aborting image signing."
exit 1
fi
echo "Signing digest: ${DIGEST}"
# List all four base image names that need to be signed.
IMAGES=(
"ghcr.io/obeone/auto_docker_proxy"
"docker.io/obeoneorg/auto_docker_proxy"
"ghcr.io/obeone/traefik_network_connector"
"docker.io/obeoneorg/traefik_network_connector"
)
for image in "${IMAGES[@]}"; do
echo "Signing ${image}@${DIGEST}"
cosign sign --yes "${image}@${DIGEST}"
done