Skip to content

Commit 10e9e65

Browse files
committed
Add GitHub Actions CI
Builds and pushes all images when their source changed.
1 parent 1738fbf commit 10e9e65

File tree

5 files changed

+182
-1
lines changed

5 files changed

+182
-1
lines changed

.ci/changed-images

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
PATH=".ci:$PATH"
5+
6+
syntax=plain
7+
if [[ "${1:-}" = "--matrix" ]]; then
8+
syntax=matrix
9+
fi
10+
11+
images=()
12+
for image in * ;do
13+
if [[ ! -d "${image}" ]]; then
14+
continue
15+
fi
16+
if ! git-changed "$image" &> /dev/null; then
17+
continue
18+
fi
19+
images+=("$image")
20+
done
21+
22+
if [[ "$syntax" = plain ]]; then
23+
for image in "${images[@]}"; do
24+
echo "$image"
25+
done
26+
elif [[ "$syntax" = matrix ]]; then
27+
output='{"image":['
28+
first=true
29+
for image in "${images[@]}"; do
30+
if [[ "$first" = true ]]; then
31+
first=false
32+
else
33+
output="$output,"
34+
fi
35+
output="$output"'"'"$image"'"'
36+
done
37+
output="$output]}"
38+
echo -n "$output"
39+
fi

.ci/git-changed

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
path="${1:-$(pwd)}"
5+
old_ref="origin/master"
6+
new_ref="HEAD"
7+
8+
if [[ "$(git rev-parse "$old_ref")" = "$(git rev-parse "$new_ref")" ]]; then
9+
old_ref="HEAD~1"
10+
fi
11+
12+
excluded_patterns=(
13+
'.*/README.md$'
14+
)
15+
16+
is_excluded() {
17+
local path="$1"
18+
for excl in "${excluded_patterns[@]}"; do
19+
if [[ "${path}" =~ ${excl} ]]; then
20+
return 1
21+
fi
22+
done
23+
return 0
24+
}
25+
26+
filter_excluded() {
27+
while read line; do
28+
if ! is_excluded "$line"; then
29+
echo "$line"
30+
fi
31+
done
32+
}
33+
34+
git_changed_files() {
35+
local old="$1"
36+
local new="$2"
37+
local path="$3"
38+
git diff --name-only "$old..$new" -- "$path"
39+
}
40+
41+
mapfile -t changed_files < <(git_changed_files "$old_ref" "$new_ref" "$path" | filter_excluded)
42+
43+
if [[ ${#changed_files[@]} == 0 ]]; then
44+
echo "Nothing changed."
45+
exit 1
46+
else
47+
echo "Changes:"
48+
for file in "${changed_files[@]}"; do
49+
echo " $file"
50+
done
51+
exit 0
52+
fi

.ci/tags-to-json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
path="$1"
5+
tags_path="$path/tags"
6+
7+
if [[ ! -f "$tags_path" ]]; then
8+
echo "$tags_path does not exist"
9+
exit 1
10+
fi
11+
12+
output='"'
13+
first=true
14+
for tag in $(<"$tags_path"); do
15+
if [[ "$first" = true ]]; then
16+
first=false
17+
output="$output$tag"
18+
else
19+
output="$output\n$tag"
20+
fi
21+
done
22+
output="$output"'"'
23+
echo -n "$output"

.github/workflows/ci.yml

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,72 @@ on:
66
- staging
77
pull_request: {}
88
jobs:
9+
set-matrix:
10+
runs-on: ubuntu-latest
11+
outputs:
12+
matrix: ${{ steps.set-matrix.outputs.matrix }}
13+
steps:
14+
- uses: actions/checkout@v2
15+
with:
16+
# Fetch everything to be able to find changed images
17+
fetch-depth: 0
18+
- run: '.ci/changed-images'
19+
- id: set-matrix
20+
run: 'echo "::set-output name=matrix::$(.ci/changed-images --matrix)"'
921
build:
22+
if: github.ref != 'refs/heads/master'
23+
needs: set-matrix
24+
runs-on: ubuntu-latest
25+
strategy:
26+
matrix: ${{fromJson(needs.set-matrix.outputs.matrix)}}
27+
fail-fast: false
28+
steps:
29+
- uses: actions/checkout@v2
30+
- id: set-tags
31+
run: |
32+
set -e
33+
echo "::set-output name=tags::"
34+
cat "${{ matrix.image }}/tags"
35+
- uses: docker/setup-buildx-action@v1
36+
id: buildx
37+
with:
38+
version: latest
39+
install: true
40+
id: docker_build
41+
- uses: docker/build-push-action@v2
42+
id: docker_build
43+
with:
44+
push: false
45+
tags: ${{ steps.set-tags.outputs.tags }}
46+
context: ${{ matrix.image }}
47+
- name: Image digest
48+
run: echo ${{ steps.docker_build.outputs.digest }}
49+
build-and-push:
50+
if: github.ref == 'refs/heads/master'
51+
needs: set-matrix
1052
runs-on: ubuntu-latest
53+
strategy:
54+
matrix: ${{fromJson(needs.set-matrix.outputs.matrix)}}
55+
fail-fast: false
1156
steps:
12-
- run: echo "hello world"
57+
- uses: docker/login-action@v1
58+
with:
59+
username: ${{ secrets.DOCKERHUB_USERNAME }}
60+
password: ${{ secrets.DOCKERHUB_TOKEN }}
61+
- uses: actions/checkout@v2
62+
- id: set-tags
63+
run: 'echo "::set-output name=tags::$(.ci/tags-to-json "${{ matrix.image }}")"'
64+
- uses: docker/setup-buildx-action@v1
65+
id: buildx
66+
with:
67+
version: latest
68+
install: true
69+
id: docker_build
70+
- uses: docker/build-push-action@v2
71+
id: docker_build
72+
with:
73+
push: true
74+
tags: ${{ steps.set-tags.outputs.tags }}
75+
context: ${{ matrix.image }}
76+
- name: Image digest
77+
run: echo ${{ steps.docker_build.outputs.digest }}

clang-format/tags

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
hdivsecurity/clang-format:latest
2+
hdivsecurity/clang-format:10

0 commit comments

Comments
 (0)