Repository navigation
310 lines (280 loc) · 12.5 KB
/
Copy pathrelease-function.yaml
File metadata and controls
310 lines (280 loc) · 12.5 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
name: Build and Deploy OCI Go Function Image
on:
push:
branches:
- main
paths:
- 'logs-function/**'
pull_request:
types: [opened, synchronize, reopened]
paths:
- 'logs-function/**'
workflow_dispatch:
permissions:
contents: read
jobs:
check_version_bump:
runs-on: ubuntu-latest
if: github.event_name == 'pull_request'
permissions:
contents: read
steps:
- name: Checkout code
uses: actions/checkout@v7.0.1
with:
fetch-depth: 0
# A shipped function change must carry a new version, otherwise the push to
# Docker Hub would skip the existing version tag and nothing would be released.
- name: Require a version bump for function changes
env:
BASE_REF: ${{ github.base_ref }}
run: |
changed="$(git diff --name-only "origin/${BASE_REF}...HEAD" -- logs-function \
':(exclude,glob)logs-function/**/*_test.go' ':(exclude)logs-function/VERSION')"
if [ -z "$changed" ]; then
echo "No shipped function changes; no version bump needed."
exit 0
fi
old="$(git show "origin/${BASE_REF}:logs-function/VERSION" 2>/dev/null | tr -d '[:space:]')"
new="$(tr -d '[:space:]' < logs-function/VERSION)"
echo "Function changes:"; echo "$changed"
if [ "$old" = "$new" ] || [ "$(printf '%s\n%s\n' "$old" "$new" | sort -V | tail -n 1)" != "$new" ]; then
echo "logs-function/VERSION must be bumped above ${old:-none} (found ${new})."
exit 1
fi
echo "Version bumped: ${old:-none} -> ${new}"
build_docker_image:
runs-on: ubuntu-latest
environment:
name: build-test-env
steps:
- name: Checkout code
uses: actions/checkout@v7.0.1
- name: Set permissions for build script
run: chmod +x scripts/build_image.sh
- name: Build Docker Image
run: ./scripts/build_image.sh
push_docker_image:
runs-on: ubuntu-latest
needs: build_docker_image
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
strategy:
matrix:
region:
- us-ashburn-1
- us-phoenix-1
- ap-batam-1
- ap-sydney-1
- ap-melbourne-1
- sa-saopaulo-1
- sa-vinhedo-1
- ca-montreal-1
- ca-toronto-1
- sa-santiago-1
- sa-valparaiso-1
- sa-bogota-1
- eu-paris-1
- eu-marseille-1
- eu-frankfurt-1
- ap-hyderabad-1
- ap-mumbai-1
- il-jerusalem-1
- eu-milan-1
- ap-osaka-1
- ap-tokyo-1
- mx-queretaro-1
- mx-monterrey-1
- eu-amsterdam-1
- me-riyadh-1
- me-jeddah-1
- ap-singapore-1
- ap-singapore-2
- af-johannesburg-1
- ap-seoul-1
- ap-chuncheon-1
- eu-madrid-1
- eu-stockholm-1
- eu-zurich-1
- me-abudhabi-1
- me-dubai-1
- uk-london-1
- uk-cardiff-1
- us-chicago-1
- us-sanjose-1
steps:
- name: Checkout code
uses: actions/checkout@v7.0.1
- name: Set permissions for build script
run: chmod +x scripts/build_and_push_image.sh
- name: Push Docker Image to ${{ matrix.region }}
run: ./scripts/build_and_push_image.sh ${{ matrix.region }}
env:
ORACLE_AUTH_TOKEN: ${{ secrets.ORACLE_AUTH_TOKEN }}
OCI_REGION: ${{ matrix.region }}
OCI_TENANCY_NAMESPACE: ${{ secrets.OCI_TENANCY_NAMESPACE }}
OCI_USERNAME: ${{ secrets.OCI_USERNAME }}
# Publishes the canonical image to Docker Hub. Customer stacks copy this image into
# their own OCIR at apply time (see terraform/image_mirror.py) instead of pulling
# directly from the region matrix above, which stays in place during the migration.
push_dockerhub_image:
runs-on: ubuntu-latest
needs: build_docker_image
# github.ref must be main for both event types -- without it, workflow_dispatch could be
# run from a feature branch and push its unreviewed code to the canonical Docker Hub repo.
if: github.ref == 'refs/heads/main' && (github.event_name == 'workflow_dispatch' || github.event_name == 'push')
environment:
name: build-test-env
permissions:
contents: write
pull-requests: write
steps:
- name: Checkout code
uses: actions/checkout@v7.0.1
with:
fetch-depth: 0
# On a push to main, an already-published version tag is only acceptable when the
# push did not change shipped function files (e.g. tests only); otherwise the
# publish script fails and asks for a VERSION bump. Manual dispatches and re-runs
# never fail on an existing tag.
- name: Detect shipped function changes in this push
if: github.event_name == 'push'
env:
BEFORE_SHA: ${{ github.event.before }}
RUN_ATTEMPT: ${{ github.run_attempt }}
run: |
# A re-run finds the tag its own first attempt published; that is not a missed release.
if [ "$RUN_ATTEMPT" -gt 1 ] || echo "$BEFORE_SHA" | grep -qE '^0+$' || ! git cat-file -e "${BEFORE_SHA}^{commit}" 2>/dev/null; then
echo "REQUIRE_NEW_VERSION=false" >> $GITHUB_ENV
exit 0
fi
changed="$(git diff --name-only "${BEFORE_SHA}" "${GITHUB_SHA}" -- logs-function \
':(exclude,glob)logs-function/**/*_test.go' ':(exclude)logs-function/VERSION')"
if [ -n "$changed" ]; then
echo "Shipped function changes in this push:"; echo "$changed"
echo "REQUIRE_NEW_VERSION=true" >> $GITHUB_ENV
else
echo "REQUIRE_NEW_VERSION=false" >> $GITHUB_ENV
fi
# The version tag comes from logs-function/VERSION; latest is pushed alongside it.
- name: Set FUNCTION_BUILD_VERSION
run: echo "FUNCTION_BUILD_VERSION=$(tr -d '[:space:]' < logs-function/VERSION)" >> $GITHUB_ENV
# Only the newest version on main may move latest (guards re-runs of old runs
# and manual dispatches from other branches).
- name: Decide whether this version moves latest
run: |
git fetch --depth=1 origin main
main_version="$(git show origin/main:logs-function/VERSION | tr -d '[:space:]')"
if [ "$main_version" = "$FUNCTION_BUILD_VERSION" ]; then
echo "PUSH_LATEST=true" >> $GITHUB_ENV
else
echo "PUSH_LATEST=false" >> $GITHUB_ENV
fi
- name: Set permissions for publish script
run: chmod +x scripts/push_dockerhub_image.sh
# DOCKERHUB_USERNAME/DOCKERHUB_TOKEN are repository-level GitHub Actions secrets on
# newrelic/oci-log-integration (Settings -> Secrets and variables -> Actions) -- not
# scoped to the build-test-env environment below, which only gates approval.
- name: Push Docker Image to Docker Hub
id: push_dockerhub
run: ./scripts/push_dockerhub_image.sh
env:
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
DOCKERHUB_REPOSITORY: newrelic/oci-log-forwarder
FUNCTION_BUILD_VERSION: ${{ env.FUNCTION_BUILD_VERSION }}
PUSH_LATEST: ${{ env.PUSH_LATEST }}
REQUIRE_NEW_VERSION: ${{ env.REQUIRE_NEW_VERSION }}
# Keeps the Docker Hub repository's "Overview" page in sync with this repo's
# README -- without this, the Docker Hub page is blank for anyone pulling the
# image directly instead of going through the Terraform stack.
- name: Update Docker Hub description
uses: peter-evans/dockerhub-description@1b9a80c056b620d92cedb9d9b5a223409c68ddfa # v5.0.0
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
repository: newrelic/oci-log-forwarder
short-description: "Forwards OCI logs to New Relic via OCI Functions and Service Connector Hub."
enable-url-completion: true
# SLC-recommended digest pinning (SLCREV-1730): the released stack should ship
# pinned to the exact image this run just pushed, not a floating :latest tag.
# Only on real pushes to main -- not on a manual workflow_dispatch test run --
# so this commit lands in lockstep with the release-terraform.yaml zip it feeds.
- name: Pin function_image default to the pushed digest
id: bump
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
run: |
digest_ref="${{ steps.push_dockerhub.outputs.image_digest_ref }}"
echo "digest_ref=${digest_ref}" >> "$GITHUB_OUTPUT"
# Scoped to the function_image block only -- variables.tf has several other
# `default = ...` lines (image_version, create_vcn, metrics_tier, ...) that
# an unscoped substitution would corrupt. Uses awk, not sed -E, because BSD
# and GNU sed disagree on brace-escaping for the block-boundary match.
awk -v ref="${digest_ref}" '
/variable "function_image" \{/ { in_block=1 }
in_block && /default[ \t]*=/ { sub(/default[ \t]*=.*/, "default = \"" ref "\""); in_block=0 }
{ print }
' terraform/variables.tf > terraform/variables.tf.tmp
mv terraform/variables.tf.tmp terraform/variables.tf
if git diff --quiet -- terraform/variables.tf; then
echo "terraform/variables.tf already pinned to ${digest_ref} -- nothing to commit."
echo "changed=false" >> "$GITHUB_OUTPUT"
else
echo "changed=true" >> "$GITHUB_OUTPUT"
fi
# Opens a pull request rather than pushing to main directly -- main's branch protection
# rejects direct pushes, and this matches the pattern already used by
# oci_logs_pin_image_digest.yml / oci_metrics_sync_image_pin.yml in terraform-provider-newrelic
# for the same reason. Pinned by commit SHA rather than a tag since this action receives
# contents and pull-requests write.
- name: Open a pull request
if: steps.bump.outputs.changed == 'true'
uses: peter-evans/create-pull-request@5f6978faf089d4d20b00c7766989d076bb2fc7f1 # v8.1.1
with:
token: ${{ secrets.GITHUB_TOKEN }}
# Fixed branch, so a later push updates this pull request instead of opening another.
branch: oci/logs-function-digest-pin
delete-branch: true
commit-message: |
chore(oci): pin function_image default to ${{ steps.bump.outputs.digest_ref }}
title: 'chore(oci): pin function_image default to the published digest'
body: |
This push published a new image to Docker Hub, so this bumps the ORM template's
`function_image` default to the digest it was just pushed under:
```
${{ steps.bump.outputs.digest_ref }}
```
Opened automatically by `.github/workflows/release-function.yaml`.
release_function:
runs-on: ubuntu-latest
needs: push_dockerhub_image
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
permissions:
# Required to create tags and releases
contents: write
steps:
- name: Checkout code
uses: actions/checkout@v7.0.1
- name: Read function version
id: version
run: echo "value=$(tr -d '[:space:]' < logs-function/VERSION)" >> "$GITHUB_OUTPUT"
- name: Check if release already exists
id: check
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
if gh release view "function-${{ steps.version.outputs.value }}" >/dev/null 2>&1; then
echo "exists=true" >> "$GITHUB_OUTPUT"
else
echo "exists=false" >> "$GITHUB_OUTPUT"
fi
- name: Create GitHub Release
if: steps.check.outputs.exists == 'false'
uses: softprops/action-gh-release@v2
with:
tag_name: function-${{ steps.version.outputs.value }}
name: "oci-log-forwarder function ${{ steps.version.outputs.value }}"
body: |
OCI log forwarder function image, published to Docker Hub (`docker.io/newrelic/oci-log-forwarder`) as `${{ steps.version.outputs.value }}` and `latest`.
Leave `function_image` on the `:latest` tag to always use the newest function, or set it to `docker.io/newrelic/oci-log-forwarder:${{ steps.version.outputs.value }}` to pin this release.
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}