Skip to content

Commit e388657

Browse files
committed
ci: improved deployment
1 parent d12d847 commit e388657

3 files changed

Lines changed: 119 additions & 21 deletions

File tree

.github/workflows/deploy-prod.yaml

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ name: Deploy Production Environments
33
# Amends the open release-please PR: copy `sha-*` image tags from staging (`*-s`) into
44
# production yamls on whatever is currently on that branch, then disable staging indexers
55
# when prod matches (always true after a verbatim promote).
6+
#
7+
# GitHub deployment environments are recorded in record-deployment-environments.yaml
8+
# when this lands on main — not when committing to the release-please branch.
69

710
on:
811
push:
@@ -19,12 +22,6 @@ permissions:
1922
jobs:
2023
promote-api-v3-main:
2124
runs-on: ubuntu-latest
22-
environment:
23-
name: api-v3-main
24-
url: https://api-v3-main.cfg.embrio.tech
25-
permissions:
26-
contents: write
27-
deployments: write
2825
steps:
2926
- name: Create GitHub App token
3027
id: app-token
@@ -73,12 +70,6 @@ jobs:
7370
promote-api-v3-test:
7471
needs: promote-api-v3-main
7572
runs-on: ubuntu-latest
76-
environment:
77-
name: api-v3-test
78-
url: https://api-v3-test.cfg.embrio.tech
79-
permissions:
80-
contents: write
81-
deployments: write
8273
steps:
8374
- name: Create GitHub App token
8475
id: app-token

.github/workflows/deploy-staging.yaml

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ concurrency:
1313

1414
permissions:
1515
contents: read
16+
actions: read
1617

1718
jobs:
18-
patch-staging-yaml:
19+
verify-docker-build-ran:
1920
if: >-
2021
${{
2122
github.event.workflow_run.conclusion == 'success'
@@ -24,25 +25,45 @@ jobs:
2425
|| github.event.workflow_run.event == 'release'
2526
)
2627
}}
28+
runs-on: ubuntu-latest
29+
outputs:
30+
image_built: ${{ steps.check.outputs.image_built }}
31+
steps:
32+
- name: Check build-and-push job succeeded
33+
id: check
34+
env:
35+
GH_TOKEN: ${{ github.token }}
36+
run: |
37+
set -euo pipefail
38+
RUN_ID="${{ github.event.workflow_run.id }}"
39+
REPO="${{ github.repository }}"
40+
# Only update staging tags when a new image was built — skip retag-only / changes-only runs.
41+
if gh api "repos/${REPO}/actions/runs/${RUN_ID}/jobs?per_page=100" \
42+
--jq '.jobs[] | select(.name == "build-and-push" and .conclusion == "success") | .name' \
43+
| head -1 | grep -q .; then
44+
echo "image_built=true" >> "$GITHUB_OUTPUT"
45+
echo "Docker build-and-push succeeded; will refresh staging image tags."
46+
else
47+
echo "image_built=false" >> "$GITHUB_OUTPUT"
48+
echo "::notice::Skipping staging tag update: no successful build-and-push job in this workflow run (no new image built)."
49+
fi
50+
51+
patch-staging-yaml:
52+
needs: verify-docker-build-ran
53+
if: needs.verify-docker-build-ran.outputs.image_built == 'true'
2754
strategy:
2855
fail-fast: true
2956
matrix:
3057
include:
3158
- name: api-v3-main-s
3259
staging: environments/main-s.yaml
3360
prod: environments/main.yaml
34-
url: https://api-v3-main-s.cfg.embrio.tech
3561
- name: api-v3-test-s
3662
staging: environments/test-s.yaml
3763
prod: environments/test.yaml
38-
url: https://api-v3-test-s.cfg.embrio.tech
3964
runs-on: ubuntu-latest
40-
environment:
41-
name: ${{ matrix.name }}
42-
url: ${{ matrix.url }}
4365
permissions:
4466
contents: read
45-
deployments: write
4667
steps:
4768
- name: Checkout repository
4869
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd
@@ -79,8 +100,10 @@ jobs:
79100
path: ${{ matrix.staging }}
80101

81102
open-staging-pr:
82-
needs: patch-staging-yaml
83-
if: success()
103+
needs: [verify-docker-build-ran, patch-staging-yaml]
104+
if: >-
105+
needs.verify-docker-build-ran.outputs.image_built == 'true'
106+
&& needs.patch-staging-yaml.result == 'success'
84107
runs-on: ubuntu-latest
85108
permissions:
86109
contents: write
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Records GitHub Deployments only after values are on main (post-merge).
2+
# For each changed `environments/<name>.yaml`, registers environment `api-v3-<name>` with URL
3+
# `https://api-v3-<name>.cfg.embrio.tech` (no per-env job definitions to maintain).
4+
5+
name: Record deployment environments
6+
7+
on:
8+
push:
9+
branches:
10+
- main
11+
12+
concurrency:
13+
group: record-deployment-envs-${{ github.ref }}
14+
cancel-in-progress: true
15+
16+
jobs:
17+
detect:
18+
runs-on: ubuntu-latest
19+
outputs:
20+
matrix: ${{ steps.build.outputs.matrix }}
21+
has_changes: ${{ steps.build.outputs.has_changes }}
22+
permissions:
23+
contents: read
24+
steps:
25+
- name: Checkout repository
26+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd
27+
with:
28+
fetch-depth: 0
29+
30+
- name: Build matrix from changed environment files
31+
id: build
32+
env:
33+
BEFORE: ${{ github.event.before }}
34+
SHA: ${{ github.sha }}
35+
run: |
36+
set -euo pipefail
37+
if [ "$BEFORE" = "0000000000000000000000000000000000000000" ]; then
38+
mapfile -t changed < <(git diff-tree --no-commit-id --name-only -r "$SHA" | grep -E '^environments/[^/]+\.yaml$' || true)
39+
else
40+
mapfile -t changed < <(git diff --name-only "$BEFORE" "$SHA" | grep -E '^environments/[^/]+\.yaml$' || true)
41+
fi
42+
if [ "${#changed[@]}" -eq 0 ]; then
43+
echo 'matrix=[]' >> "$GITHUB_OUTPUT"
44+
echo 'has_changes=false' >> "$GITHUB_OUTPUT"
45+
exit 0
46+
fi
47+
json=$(jq -c -n --args '
48+
[
49+
$ARGS.positional[] |
50+
select(test("^environments/[^/]+\\.yaml$")) |
51+
. as $path |
52+
($path | split("/") | .[-1] | sub("\\.yaml$"; "")) as $stem |
53+
{
54+
file: $path,
55+
stem: $stem,
56+
env: ("api-v3-" + $stem),
57+
url: ("https://api-v3-" + $stem + ".cfg.embrio.tech")
58+
}
59+
]
60+
' -- "${changed[@]}")
61+
{
62+
echo 'matrix<<MATRIX_JSON_EOF'
63+
echo "$json"
64+
echo 'MATRIX_JSON_EOF'
65+
} >> "$GITHUB_OUTPUT"
66+
echo 'has_changes=true' >> "$GITHUB_OUTPUT"
67+
68+
record:
69+
needs: detect
70+
if: needs.detect.outputs.has_changes == 'true'
71+
runs-on: ubuntu-latest
72+
strategy:
73+
fail-fast: false
74+
matrix:
75+
include: ${{ fromJson(needs.detect.outputs.matrix) }}
76+
environment:
77+
name: ${{ matrix.env }}
78+
url: ${{ matrix.url }}
79+
permissions:
80+
contents: read
81+
deployments: write
82+
steps:
83+
- name: Record deployment
84+
run: echo "${{ matrix.file }} updated on main @ ${{ github.sha }}"

0 commit comments

Comments
 (0)