@@ -3,14 +3,20 @@ name: ROS 2 Jazzy Docker Compose Build and Test
33on :
44 push :
55 branches : [ "main" ]
6+ # Every pull request, including ones stacked on another pull request's branch.
67 pull_request :
7- branches : [ "main" ]
88
99permissions :
1010 contents : read
1111
12+ env :
13+ IMAGE : ghcr.io/waybionic/waybionic_ground_station
14+
1215jobs :
16+ # Pull requests build and test with a read-only token. Keep these check names
17+ # stable for review.
1318 build-and-test :
19+ if : github.event_name == 'pull_request'
1420 runs-on : ubuntu-24.04
1521
1622 steps :
2127 run : docker compose --progress plain build test
2228
2329 build-and-test-arm64 :
30+ if : github.event_name == 'pull_request'
2431 runs-on : ubuntu-24.04-arm
2532
2633 steps :
@@ -29,3 +36,117 @@ jobs:
2936
3037 - name : Build and Test with Docker Compose
3138 run : docker compose --progress plain build test
39+
40+ # Pushes to main build and test the same way, then push the tested image under this commit.
41+ publish-amd64 :
42+ if : github.event_name == 'push'
43+ runs-on : ubuntu-24.04
44+ outputs :
45+ digest : ${{ steps.push.outputs.digest }}
46+ permissions :
47+ contents : read
48+ packages : write
49+
50+ steps :
51+ - name : Checkout Repository
52+ uses : actions/checkout@v4
53+
54+ # No registry cache: a published layer must come from this build, not from :jazzy.
55+ - name : Build and Test with Docker Compose
56+ run : docker compose --progress plain build --no-cache test
57+
58+ - name : Log in to the GitHub Container Registry
59+ env :
60+ GHCR_TOKEN : ${{ secrets.GITHUB_TOKEN }}
61+ run : printf '%s' "$GHCR_TOKEN" | docker login ghcr.io --username "$GITHUB_ACTOR" --password-stdin
62+
63+ - name : Push the tested x86-64 image
64+ id : push
65+ shell : bash
66+ run : |
67+ docker tag "$IMAGE:jazzy" "$IMAGE:$GITHUB_SHA-amd64"
68+ push_log=$(docker push "$IMAGE:$GITHUB_SHA-amd64")
69+ printf '%s\n' "$push_log"
70+ if [[ "$push_log" =~ digest:\ (sha256:[0-9a-f]{64}) ]]; then
71+ echo "digest=${BASH_REMATCH[1]}" >> "$GITHUB_OUTPUT"
72+ else
73+ echo "::error::docker push did not report the tested image digest"
74+ exit 1
75+ fi
76+
77+ publish-arm64 :
78+ if : github.event_name == 'push'
79+ runs-on : ubuntu-24.04-arm
80+ outputs :
81+ digest : ${{ steps.push.outputs.digest }}
82+ permissions :
83+ contents : read
84+ packages : write
85+
86+ steps :
87+ - name : Checkout Repository
88+ uses : actions/checkout@v4
89+
90+ - name : Build and Test with Docker Compose
91+ run : docker compose --progress plain build --no-cache test
92+
93+ - name : Log in to the GitHub Container Registry
94+ env :
95+ GHCR_TOKEN : ${{ secrets.GITHUB_TOKEN }}
96+ run : printf '%s' "$GHCR_TOKEN" | docker login ghcr.io --username "$GITHUB_ACTOR" --password-stdin
97+
98+ - name : Push the tested ARM64 image
99+ id : push
100+ shell : bash
101+ run : |
102+ docker tag "$IMAGE:jazzy" "$IMAGE:$GITHUB_SHA-arm64"
103+ push_log=$(docker push "$IMAGE:$GITHUB_SHA-arm64")
104+ printf '%s\n' "$push_log"
105+ if [[ "$push_log" =~ digest:\ (sha256:[0-9a-f]{64}) ]]; then
106+ echo "digest=${BASH_REMATCH[1]}" >> "$GITHUB_OUTPUT"
107+ else
108+ echo "::error::docker push did not report the tested image digest"
109+ exit 1
110+ fi
111+
112+ publish-image :
113+ if : github.event_name == 'push'
114+ needs : [publish-amd64, publish-arm64]
115+ runs-on : ubuntu-24.04
116+ # One run at a time moves the shared tag, after checking that its commit is still newest.
117+ concurrency :
118+ group : publish-image
119+ cancel-in-progress : false
120+ permissions :
121+ contents : read
122+ packages : write
123+
124+ steps :
125+ - name : Log in to the GitHub Container Registry
126+ env :
127+ GHCR_TOKEN : ${{ secrets.GITHUB_TOKEN }}
128+ run : printf '%s' "$GHCR_TOKEN" | docker login ghcr.io --username "$GITHUB_ACTOR" --password-stdin
129+
130+ - name : Publish one image for both architectures
131+ shell : bash
132+ run : |
133+ amd64="${{ needs.publish-amd64.outputs.digest }}"
134+ arm64="${{ needs.publish-arm64.outputs.digest }}"
135+ if [[ ! "$amd64" =~ ^sha256:[0-9a-f]{64}$ || ! "$arm64" =~ ^sha256:[0-9a-f]{64}$ ]]; then
136+ echo "::error::Missing tested image digest"
137+ exit 1
138+ fi
139+ tags=(--tag "$IMAGE:${GITHUB_SHA::7}")
140+ # Runs can finish out of order: only the newest commit on main moves :jazzy.
141+ head=$(git ls-remote "https://github.com/$GITHUB_REPOSITORY" refs/heads/main | cut -f1)
142+ if [ -z "$head" ]; then
143+ echo "::error::Could not resolve main before moving :jazzy"
144+ exit 1
145+ fi
146+ if [ "$head" = "$GITHUB_SHA" ]; then
147+ tags+=(--tag "$IMAGE:jazzy")
148+ else
149+ echo "main has moved on to $head, so :jazzy stays where it is"
150+ fi
151+ docker buildx imagetools create "${tags[@]}" \
152+ "$IMAGE@$amd64" "$IMAGE@$arm64"
0 commit comments