Skip to content

Commit f27ed76

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

File tree

5 files changed

+176
-1
lines changed

5 files changed

+176
-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: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,66 @@ 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+
- run: '.ci/changed-images'
16+
- id: set-matrix
17+
run: 'echo "::set-output name=matrix::$(.ci/changed-images --matrix)"'
918
build:
19+
if: github.ref != 'refs/heads/master'
20+
needs: set-matrix
21+
runs-on: ubuntu-latest
22+
strategy:
23+
matrix: ${{fromJson(needs.set-matrix.outputs.matrix)}}
24+
fail-fast: false
25+
steps:
26+
- uses: actions/checkout@v2
27+
- id: set-tags
28+
run: 'echo "::set-output name=tags::$(.ci/tags-to-json "${{ matrix.image }}")"'
29+
- uses: docker/setup-buildx-action@v1
30+
id: buildx
31+
with:
32+
version: latest
33+
install: true
34+
id: docker_build
35+
- uses: docker/build-push-action@v2
36+
id: docker_build
37+
with:
38+
push: false
39+
tags: ${{ steps.set-tags.outputs.tags }}
40+
context: ${{ matrix.image }}
41+
- name: Image digest
42+
run: echo ${{ steps.docker_build.outputs.digest }}
43+
build-and-push:
44+
if: github.ref == 'refs/heads/master'
45+
needs: set-matrix
1046
runs-on: ubuntu-latest
47+
strategy:
48+
matrix: ${{fromJson(needs.set-matrix.outputs.matrix)}}
49+
fail-fast: false
1150
steps:
12-
- run: echo "hello world"
51+
- uses: docker/login-action@v1
52+
with:
53+
username: ${{ secrets.DOCKERHUB_USERNAME }}
54+
password: ${{ secrets.DOCKERHUB_TOKEN }}
55+
- uses: actions/checkout@v2
56+
- id: set-tags
57+
run: 'echo "::set-output name=tags::$(.ci/tags-to-json "${{ matrix.image }}")"'
58+
- uses: docker/setup-buildx-action@v1
59+
id: buildx
60+
with:
61+
version: latest
62+
install: true
63+
id: docker_build
64+
- uses: docker/build-push-action@v2
65+
id: docker_build
66+
with:
67+
push: true
68+
tags: ${{ steps.set-tags.outputs.tags }}
69+
context: ${{ matrix.image }}
70+
- name: Image digest
71+
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)