forked from NOAA-OWP/nwm-fcst-mgr
-
Notifications
You must be signed in to change notification settings - Fork 0
270 lines (243 loc) · 8.57 KB
/
Copy pathci-cd.yml
File metadata and controls
270 lines (243 loc) · 8.57 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
name: CI/CD Pipeline
on:
pull_request:
branches:
- ngwpc-candidate
- ngwpc-release
- main
- nwm-main
- development
- release-candidate
push:
branches:
- ngwpc-candidate
- ngwpc-release
- main
- nwm-main
- development
- release-candidate
workflow_dispatch:
inputs:
NGEN_IMAGE_TAG:
description: 'NGEN_IMAGE_TAG'
required: false
type: string
permissions:
contents: read
packages: write
security-events: write
env:
REGISTRY: ghcr.io
PYTHON_VERSION: '3.11'
jobs:
# set variables for use in other jobs
setup:
name: setup
runs-on: ubuntu-latest
outputs:
org: ${{ steps.vars.outputs.org }}
image_base: ${{ steps.vars.outputs.image_base }}
pr_tag: ${{ steps.vars.outputs.pr_tag }}
commit_sha: ${{ steps.vars.outputs.commit_sha }}
commit_sha_short: ${{ steps.vars.outputs.commit_sha_short }}
test_image_tag: ${{ steps.vars.outputs.test_image_tag }}
alias_tag: ${{ steps.vars.outputs.alias_tag }}
build_date: ${{ steps.vars.outputs.build_date }}
clean_ref: ${{ steps.vars.outputs.clean_ref }}
steps:
- name: Compute image vars
id: vars
shell: bash
run: |
set -euo pipefail
# set variables to use with Docker images
ORG="$(echo "${GITHUB_REPOSITORY_OWNER}" | tr '[:upper:]' '[:lower:]')"
REPO="$(basename "${GITHUB_REPOSITORY}")"
IMAGE_BASE="${REGISTRY}/${ORG}/${REPO}"
# one datetime for all time variables
NOW=$(date -u +'%Y-%m-%d %H:%M:%S')
# for OCI labels
BUILD_DATE=$(date -u -d "$NOW" +'%Y-%m-%dT%H:%M:%SZ')
# for Docker image tags
TIMESTAMP=$(date -u -d "$NOW" +'%Y%m%d%H%M%SZ')
# logic to get the real branch name and commit SHA on pull requests
if [ "$GITHUB_EVENT_NAME" = "pull_request" ]; then
REAL_REF="${{ github.head_ref }}"
REAL_SHA="${{ github.event.pull_request.head.sha }}"
else
REAL_REF="${{ github.ref_name }}"
REAL_SHA="${GITHUB_SHA}"
fi
# clean ref name and short commit sha
CLEAN_REF=$(echo "$REAL_REF" | tr '[:upper:]' '[:lower:]' | sed 's/\//-/g')
SHORT_SHA="${REAL_SHA:0:7}"
# logic for the tags:
# test_image_tag (commit short sha): used for the initial build and test
# alias_tag: used for final tagging on successful tests
# test tag is always commit short sha
TEST_TAG="${SHORT_SHA}"
if [ "$GITHUB_EVENT_NAME" = "pull_request" ]; then
# for pull requests, use pr-<pr number>-build
ALIAS="pr-${{ github.event.pull_request.number }}-build"
elif [ "$GITHUB_EVENT_NAME" = "workflow_dispatch" ] && [ "${{ github.ref_type }}" = "tag" ]; then
# for manual workflow dispatch on tags, use the git tag
ALIAS="${CLEAN_REF}"
else
# for pushes to branches, use timestamp-branchname
ALIAS="${TIMESTAMP}-${CLEAN_REF}"
fi
# save outputs
echo "org=${ORG}" >> "$GITHUB_OUTPUT"
echo "image_base=${IMAGE_BASE}" >> "$GITHUB_OUTPUT"
echo "build_date=${BUILD_DATE}" >> "$GITHUB_OUTPUT"
echo "test_image_tag=${TEST_TAG}" >> "$GITHUB_OUTPUT"
echo "alias_tag=${ALIAS}" >> "$GITHUB_OUTPUT"
echo "commit_sha=${REAL_SHA}" >> "$GITHUB_OUTPUT"
echo "commit_sha_short=${SHORT_SHA}" >> "$GITHUB_OUTPUT"
echo "clean_ref=${CLEAN_REF}" >> "$GITHUB_OUTPUT"
# CodeQL scan
codeql-scan:
name: codeql-scan
if: |
(github.event_name == 'pull_request') ||
(github.event_name == 'push') ||
(github.event_name == 'workflow_dispatch')
runs-on: ubuntu-latest
needs: setup
permissions:
actions: read
contents: read
security-events: write
steps:
- uses: actions/checkout@v6
- name: Set up Python
uses: actions/setup-python@v6
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: Initialize CodeQL
uses: github/codeql-action/init@v4
with:
languages: python
- name: Install dependencies
run: |
python -m pip install --upgrade pip
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v4
build:
name: build
if: |
(github.event_name == 'pull_request') ||
(github.event_name == 'push') ||
(github.event_name == 'workflow_dispatch')
runs-on: ubuntu-latest
needs: setup
steps:
- uses: actions/checkout@v6
- name: Log in to registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build & push image
uses: docker/build-push-action@v6
with:
context: .
push: true
tags: ${{ needs.setup.outputs.image_base }}:${{ needs.setup.outputs.test_image_tag }}
build-args: |
ORG=${{ needs.setup.outputs.org }}
NGEN_IMAGE_TAG=${{ inputs.NGEN_IMAGE_TAG || 'latest' }}
IMAGE_SOURCE=https://github.com/${{ github.repository }}
IMAGE_VENDOR=${{ github.repository_owner }}
IMAGE_VERSION=${{ needs.setup.outputs.clean_ref }}
IMAGE_REVISION=${{ needs.setup.outputs.commit_sha }}
IMAGE_CREATED=${{ needs.setup.outputs.build_date }}
CI_COMMIT_REF_NAME=${{ needs.setup.outputs.clean_ref }}
unit-test:
name: unit-test
if: |
(github.event_name == 'pull_request') ||
(github.event_name == 'push') ||
(github.event_name == 'workflow_dispatch')
runs-on: ubuntu-latest
needs:
- setup
- build
container:
image: ${{ needs.setup.outputs.image_base }}:${{ needs.setup.outputs.test_image_tag }}
steps:
- name: Run unit tests
run: |
echo "TODO: add unit tests here"
# run container security scan using Trivy
container-scanning:
if: |
(github.event_name == 'pull_request') ||
(github.event_name == 'push') ||
(github.event_name == 'workflow_dispatch')
runs-on: ubuntu-latest
needs:
- setup
- build
steps:
- name: Install Trivy
uses: aquasecurity/setup-trivy@v0.2.5
with:
cache: true
version: v0.69.3
- name: Trivy scan
env:
TMPDIR: /mnt/trivy-temp
run: |
sudo mkdir -p $TMPDIR
sudo chown -R $USER:$USER $TMPDIR
trivy image \
--format sarif \
--output trivy-results.sarif \
--severity CRITICAL,HIGH \
--scanners vuln \
--ignore-unfixed \
--timeout 45m \
${{ needs.setup.outputs.image_base }}:${{ needs.setup.outputs.test_image_tag }}
# promote Docker image tags after successful tests
promote-tags:
name: Promote Tags
if: |
(github.event_name == 'pull_request') ||
(github.event_name == 'push') ||
(github.event_name == 'workflow_dispatch')
runs-on: ubuntu-latest
needs:
- setup
- codeql-scan
- build
- container-scanning
steps:
- name: Tag image with alias and latest
shell: bash
run: |
set -euo pipefail
# ensure skopeo is available for promotion
if ! command -v skopeo >/dev/null 2>&1; then
sudo apt-get update -y
sudo apt-get install -y --no-install-recommends skopeo
fi
IMAGE_BASE="${{ needs.setup.outputs.image_base }}"
TEST_TAG="${{ needs.setup.outputs.test_image_tag }}"
ALIAS_TAG="${{ needs.setup.outputs.alias_tag }}"
# apply the primary alias (pr tag or timestamp-branch)
skopeo copy \
--src-creds "${{ github.actor }}:${{ secrets.GITHUB_TOKEN }}" \
--dest-creds "${{ github.actor }}:${{ secrets.GITHUB_TOKEN }}" \
--all \
"docker://${IMAGE_BASE}:${TEST_TAG}" "docker://${IMAGE_BASE}:${ALIAS_TAG}"
# tag with 'latest' on development branch push
if [ "$GITHUB_REF_NAME" = "development" ]; then
skopeo copy \
--src-creds "${{ github.actor }}:${{ secrets.GITHUB_TOKEN }}" \
--dest-creds "${{ github.actor }}:${{ secrets.GITHUB_TOKEN }}" \
--all \
"docker://${IMAGE_BASE}:${TEST_TAG}" "docker://${IMAGE_BASE}:latest"
fi