Skip to content

Commit d54415b

Browse files
committed
Github workflow: build test-cloud-server for each PR
1 parent ed2623a commit d54415b

File tree

4 files changed

+217
-86
lines changed

4 files changed

+217
-86
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
# This workflow uses actions that are not certified by GitHub.
2+
# They are provided by a third-party and are governed by
3+
# separate terms of service, privacy policy, and support
4+
# documentation.
5+
6+
name: Build and Publish Docker Image
7+
8+
on:
9+
workflow_call:
10+
inputs:
11+
name:
12+
description: Name of the container
13+
type: string
14+
required: true
15+
directory:
16+
description: Directory of service
17+
type: string
18+
required: true
19+
file:
20+
description: Dockerfile to build
21+
type: string
22+
required: true
23+
template-file:
24+
description: Template dockefile to resolve
25+
type: string
26+
required: false
27+
28+
29+
env:
30+
REGISTRY: ghcr.io
31+
32+
jobs:
33+
build-and-publish-with-cfg:
34+
runs-on: ubuntu-latest
35+
permissions:
36+
contents: read
37+
packages: write
38+
steps:
39+
- name: CPU Info
40+
run: |
41+
cat /proc/cpuinfo
42+
echo "Number of cores: $(nproc)"
43+
echo "Number of threads: $(nproc --all)"
44+
- name: Checkout repository
45+
uses: actions/checkout@v4
46+
with:
47+
fetch-depth: 0
48+
49+
- name: Set up QEMU
50+
uses: docker/setup-qemu-action@v3
51+
52+
- name: Set up Docker Buildx
53+
id: buildx
54+
uses: docker/setup-buildx-action@v3
55+
56+
- name: Log in to the Container registry
57+
uses: docker/login-action@v3
58+
with:
59+
registry: ${{ env.REGISTRY }}
60+
username: ${{ github.actor }}
61+
password: ${{ secrets.GITHUB_TOKEN }}
62+
63+
- name: Extract metadata (tags, labels) for docker image
64+
id: meta
65+
uses: docker/metadata-action@v5
66+
with:
67+
images: ${{ env.REGISTRY }}/${{ github.repository }}/${{ inputs.name }}
68+
tags: |
69+
type=raw,enable={{is_default_branch}},value=vnext
70+
type=sha,enable={{is_default_branch}},prefix=vnext-,format=short
71+
type=ref,event=pr,prefix=vnext-pr,suffix=-{{sha}}
72+
type=ref,event=pr,prefix=vnext-pr
73+
type=ref,enable={{is_default_branch}},event=branch
74+
type=semver,pattern={{version}}
75+
type=semver,pattern={{major}}.{{minor}}
76+
type=semver,pattern={{major}}
77+
78+
- name: Set build args
79+
id: build-args
80+
run: |
81+
echo "commit_date=$(date -u +%FT%TZ --date=@$(git show --format=%ct HEAD --quiet))" >> $GITHUB_OUTPUT
82+
short_commit=$(git show --format=%h HEAD --quiet)
83+
echo "short_commit=${short_commit}" >> $GITHUB_OUTPUT
84+
echo "date=$(date -u +%FT%TZ)" >> $GITHUB_OUTPUT
85+
version=$(git describe --tags --abbrev=0 | sed 's/^v//')
86+
release_url="${{ github.server_url }}/${{ github.repository }}/releases/tag/${{ github.ref_name }}"
87+
if [[ "${{ github.ref }}" != "refs/tags/"* ]]; then
88+
version="${version}-pr${{ github.event.number }}"
89+
elif curl --fail-with-body -s "$release_url" > /dev/null; then
90+
echo "release_url=$release_url" >> $GITHUB_OUTPUT
91+
fi
92+
echo "version=$version" >> $GITHUB_OUTPUT
93+
94+
- name: First try to build and push docker image
95+
uses: docker/build-push-action@v5
96+
# This needs to be true to proceed to the next step of failure
97+
continue-on-error: true
98+
# You need to specify an id to be able to tell what the status of this action was
99+
id: BuildAndPush1
100+
with:
101+
context: ./
102+
platforms: linux/amd64,linux/arm64
103+
builder: ${{ steps.buildx.outputs.name }}
104+
build-args: |
105+
DIRECTORY=${{ inputs.directory }}
106+
NAME=${{ inputs.name }}
107+
COMMIT_DATE=${{ steps.build-args.outputs.commit_date }}
108+
SHORT_COMMIT=${{ steps.build-args.outputs.short_commit }}
109+
DATE=${{ steps.build-args.outputs.date }}
110+
VERSION=${{ steps.build-args.outputs.version }}
111+
RELEASE_URL=${{ steps.build-args.outputs.release_url }}
112+
file: ${{ inputs.file }}
113+
push: true
114+
tags: ${{ steps.meta.outputs.tags }}
115+
labels: ${{ steps.meta.outputs.labels }}
116+
117+
- name: Second try to build and push docker image when first build fails
118+
# Only run this step if step 1 fails. It knows that step one failed because we specified an `id` for the first step
119+
if: steps.BuildAndPush1.outcome == 'failure'
120+
uses: docker/build-push-action@v5
121+
with:
122+
context: ./
123+
platforms: linux/amd64,linux/arm64
124+
builder: ${{ steps.buildx.outputs.name }}
125+
build-args: |
126+
DIRECTORY=${{ inputs.directory }}
127+
NAME=${{ inputs.name }}
128+
COMMIT_DATE=${{ steps.build-args.outputs.commit_date }}
129+
SHORT_COMMIT=${{ steps.build-args.outputs.short_commit }}
130+
DATE=${{ steps.build-args.outputs.date }}
131+
VERSION=${{ steps.build-args.outputs.version }}
132+
RELEASE_URL=${{ steps.build-args.outputs.release_url }}
133+
file: ${{ inputs.file }}
134+
push: true
135+
tags: ${{ steps.meta.outputs.tags }}
136+
labels: ${{ steps.meta.outputs.labels }}

.github/workflows/build-publish.yaml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# This workflow uses actions that are not certified by GitHub.
2+
# They are provided by a third-party and are governed by
3+
# separate terms of service, privacy policy, and support
4+
# documentation.
5+
6+
name: Create and publish a docker images to github
7+
8+
concurrency:
9+
group: ${{ github.workflow }}-${{ github.ref }}
10+
cancel-in-progress: ${{ github.ref_name != 'main' }}
11+
12+
on:
13+
push:
14+
branches:
15+
- main
16+
tags:
17+
- "*"
18+
pull_request:
19+
branches:
20+
- main
21+
workflow_dispatch:
22+
23+
jobs:
24+
build-and-publish-image:
25+
strategy:
26+
fail-fast: false
27+
matrix:
28+
include:
29+
- name: test-cloud-server
30+
directory: test/cloud-server
31+
file: test/cloud-server/Dockerfile
32+
uses: ./.github/workflows/build-publish-cfg.yaml
33+
with:
34+
name: ${{ matrix.name }}
35+
directory: ${{ matrix.directory }}
36+
file: ${{ matrix.file }}
37+

.github/workflows/ghcr-cleanup.yaml

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,53 @@ name: Delete old ghcr images
22
on:
33
schedule:
44
- cron: "0 1 * * *" # every day at 1:00am
5+
pull_request:
6+
types: [closed]
57
workflow_dispatch:
68

79
jobs:
8-
ghcr-cleanup:
10+
pull-request-ghcr-cleanup:
11+
if: ${{ github.event_name == 'pull_request' }}
12+
name: Delete images related to closed PR
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Delete images related to closed PR (1)
16+
id: run-1
17+
continue-on-error: true
18+
uses: snok/container-retention-policy@v2
19+
with:
20+
image-names: device/*
21+
cut-off: now UTC
22+
account-type: org
23+
org-name: plgd-dev
24+
filter-tags: vnext-pr${{ github.event.pull_request.number }}*
25+
token: ${{ secrets.GHCR_CLEANUP_PAT }}
26+
token-type: pat
27+
- name: Delete images related to closed PR (2)
28+
id: run-2
29+
if: ${{ steps.run-1.outcome == 'failure' }}
30+
continue-on-error: true
31+
uses: snok/container-retention-policy@v2
32+
with:
33+
image-names: device/*
34+
cut-off: now UTC
35+
account-type: org
36+
org-name: plgd-dev
37+
filter-tags: vnext-pr${{ github.event.pull_request.number }}*
38+
token: ${{ secrets.GHCR_CLEANUP_PAT }}
39+
token-type: pat
40+
- name: Delete images related to closed PR (3)
41+
if: ${{ steps.run-2.outcome == 'failure' }}
42+
uses: snok/container-retention-policy@v2
43+
with:
44+
image-names: device/*
45+
cut-off: now UTC
46+
account-type: org
47+
org-name: plgd-dev
48+
filter-tags: vnext-pr${{ github.event.pull_request.number }}*
49+
token: ${{ secrets.GHCR_CLEANUP_PAT }}
50+
token-type: pat
51+
nightly-ghcr-cleanup:
952
if: ${{ github.event_name == 'schedule' || github.event_name == 'workflow_dispatch' }}
1053
name: Delete stale or untagged images
1154
runs-on: ubuntu-latest

.github/workflows/publishDockerImages.yml

Lines changed: 0 additions & 85 deletions
This file was deleted.

0 commit comments

Comments
 (0)