Skip to content

Commit 12ca068

Browse files
committed
Add initial GitHub Actions CI
1 parent 8d03909 commit 12ca068

File tree

4 files changed

+183
-14
lines changed

4 files changed

+183
-14
lines changed

.github/workflows/generate.sh

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env bash
2+
set -Eeuo pipefail
3+
4+
bashbrewDir="$1"; shift
5+
6+
if [ "$#" -eq 0 ]; then
7+
changes="$(.github/workflows/pr-changed-files.sh library/)"
8+
if [ -z "$changes" ]; then
9+
echo '{}'
10+
exit
11+
fi
12+
repos="$(xargs -n1 basename <<<"$changes")"
13+
set -- $repos
14+
fi
15+
16+
strategy='{}'
17+
for repo; do
18+
newStrategy="$(GITHUB_REPOSITORY="$repo" GENERATE_STACKBREW_LIBRARY='cat "library/$GITHUB_REPOSITORY"' "$bashbrewDir/scripts/github-actions/generate.sh")"
19+
newStrategy="$(jq -c --arg repo "$repo" '.matrix.include = [
20+
.matrix.include[]
21+
| ([ .meta.entries[].tags[0] ]) as $tags
22+
| .name = ($tags | join(", "))
23+
| .runs.prepare += "\ngit clone --depth 1 https://github.com/docker-library/bashbrew.git ~/bashbrew\n~/bashbrew/bashbrew.sh --version"
24+
| .runs.build = (
25+
(if .os | startswith("windows-") then "export BASHBREW_ARCH=windows-amd64 BASHBREW_CONSTRAINTS=" + ([ .meta.entries[].constraints[] ] | join(", ") | @sh) + "\n" else "" end)
26+
+ "export BASHBREW_LIBRARY=\"$PWD/library\"\n"
27+
+ ([ $tags[] | "~/bashbrew/bashbrew.sh build " + @sh ] | join("\n"))
28+
)
29+
]' <<<"$newStrategy")"
30+
jq -c . <<<"$newStrategy" > /dev/null # sanity check
31+
strategy="$(jq -c --argjson strategy "$strategy" '.matrix.include = ($strategy.matrix.include // []) + .matrix.include' <<<"$newStrategy")"
32+
done
33+
jq -c . <<<"$strategy" > /dev/null # sanity check
34+
35+
if [ -t 1 ]; then
36+
jq <<<"$strategy"
37+
else
38+
cat <<<"$strategy"
39+
fi
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env bash
2+
set -Eeuo pipefail
3+
4+
git fetch --quiet https://github.com/docker-library/official-images.git master
5+
git diff --numstat FETCH_HEAD...HEAD -- "$@" | cut -d$'\t' -f3-

.github/workflows/test-pr.yml

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
name: Test PR
2+
3+
on:
4+
pull_request:
5+
6+
defaults:
7+
run:
8+
shell: 'bash -Eeuo pipefail -x {0}'
9+
10+
jobs:
11+
12+
label:
13+
name: Label
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/checkout@v2
17+
- id: labels
18+
name: Generate List
19+
run: |
20+
labels="$(.github/workflows/pr-changed-files.sh library/)"
21+
labels="$(jq -Rsc 'rtrimstr("\n") | split("\n") as $labels | { labels: $labels, count: ($labels | length) }' <<<"$labels")"
22+
jq . <<<"$labels"
23+
echo "::set-output name=labels::$labels"
24+
# TODO add the "new-image" label as appropriate
25+
- name: Apply Labels
26+
uses: actions/github-script@0.9.0
27+
with:
28+
script: |
29+
const data = ${{ steps.labels.outputs.labels }}
30+
github.issues.addLabels({
31+
owner: context.repo.owner,
32+
repo: context.repo.repo,
33+
issue_number: context.payload.pull_request.number,
34+
labels: data.labels,
35+
})
36+
if: fromJSON(steps.labels.outputs.labels).count > 0
37+
38+
diff:
39+
name: Diff
40+
runs-on: ubuntu-latest
41+
steps:
42+
- uses: actions/checkout@v2
43+
- id: diff
44+
name: Run ./diff-pr.sh
45+
run: |
46+
git clone --depth 1 https://github.com/docker-library/bashbrew.git -b master ~/bashbrew
47+
~/bashbrew/bashbrew.sh --version > /dev/null
48+
export PATH="$HOME/bashbrew/bin:$PATH"
49+
bashbrew --version
50+
diff="$(./diff-pr.sh 0)"
51+
# "Body is too long (maximum is 65536 characters)" (so we'll check for some fudge room and pre-filter the diff)
52+
diff="$(jq -Rcs 'rtrimstr("\n") | { diff: (if length < 65000 then . else "TODO diff too large for GitHub comment!" end), length: length }' <<<"$diff")"
53+
echo "::set-output name=diff::$diff"
54+
- name: Comment
55+
run: |
56+
const data = ${{ steps.diff.outputs.diff }}
57+
const body = "<details>\n<summary>Diff:</summary>\n\n```diff\n" + data.diff + "\n```\n\n</details>"
58+
const { data: comments } = await github.issues.listComments({
59+
owner: context.repo.owner,
60+
repo: context.repo.repo,
61+
issue_number: context.payload.pull_request.number,
62+
})
63+
comments.forEach((comment) => {
64+
if (comment.user.login === 'github-actions') {
65+
github.issues.deleteComment({
66+
owner: context.repo.owner,
67+
repo: context.repo.repo,
68+
comment_id: comment.id,
69+
})
70+
}
71+
})
72+
github.issues.createComment({
73+
owner: context.repo.owner,
74+
repo: context.repo.repo,
75+
issue_number: context.payload.pull_request.number,
76+
body: body,
77+
})
78+
79+
generate-jobs:
80+
name: Generate Jobs
81+
runs-on: ubuntu-latest
82+
outputs:
83+
strategy: ${{ steps.generate-jobs.outputs.strategy }}
84+
steps:
85+
- uses: actions/checkout@v2
86+
- id: generate-jobs
87+
name: Generate Jobs
88+
run: |
89+
git clone --depth 1 https://github.com/docker-library/bashbrew.git -b master ~/bashbrew
90+
strategy="$(.github/workflows/generate.sh ~/bashbrew)"
91+
jq . <<<"$strategy" # sanity check / debugging aid
92+
echo "::set-output name=strategy::$strategy"
93+
94+
test:
95+
needs: generate-jobs
96+
strategy: ${{ fromJson(needs.generate-jobs.outputs.strategy) }}
97+
name: ${{ matrix.name }}
98+
runs-on: ${{ matrix.os }}
99+
steps:
100+
- uses: actions/checkout@v2
101+
- name: Prepare Environment
102+
run: ${{ matrix.runs.prepare }}
103+
- name: Pull Dependencies
104+
run: ${{ matrix.runs.pull }}
105+
- name: Build ${{ matrix.name }}
106+
run: ${{ matrix.runs.build }}
107+
- name: History ${{ matrix.name }}
108+
run: ${{ matrix.runs.history }}
109+
- name: Test ${{ matrix.name }}
110+
run: ${{ matrix.runs.test }}
111+
- name: '"docker images"'
112+
run: ${{ matrix.runs.images }}

diff-pr.sh

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ shopt -s dotglob
55
# make sure we can GTFO
66
trap 'echo >&2 Ctrl+C captured, exiting; exit 1' SIGINT
77

8+
# if bashbrew is missing, bail early with a sane error
9+
bashbrew --version > /dev/null
10+
811
usage() {
912
cat <<-EOUSAGE
1013
usage: $0 [PR number] [repo[:tag]]
@@ -57,7 +60,8 @@ fi
5760
pull="$1" # PR number
5861
shift
5962

60-
#dir="$(dirname "$(readlink -f "$BASH_SOURCE")")"
63+
diffDir="$(readlink -f "$BASH_SOURCE")"
64+
diffDir="$(dirname "$diffDir")"
6165

6266
tempDir="$(mktemp -d)"
6367
trap "rm -rf '$tempDir'" EXIT
@@ -67,12 +71,19 @@ git clone --quiet \
6771
https://github.com/docker-library/official-images.git \
6872
oi
6973

70-
git -C oi fetch --quiet \
71-
origin "pull/$pull/merge":pull
74+
if [ "$pull" != '0' ]; then
75+
git -C oi fetch --quiet \
76+
origin "pull/$pull/merge":refs/heads/pull
77+
else
78+
git -C oi fetch --quiet --update-shallow \
79+
"$diffDir" HEAD:refs/heads/pull
80+
fi
7281

73-
images=( "$@" )
74-
if [ "${#images[@]}" -eq 0 ]; then
75-
images=( $(git -C oi/library diff --name-only HEAD...pull -- . | xargs -n1 basename) )
82+
if [ "$#" -eq 0 ]; then
83+
images="$(git -C oi/library diff --name-only HEAD...pull -- .)"
84+
[ -n "$images" ] || exit 0
85+
images="$(xargs -n1 basename <<<"$images")"
86+
set -- $images
7687
fi
7788

7889
export BASHBREW_CACHE="${BASHBREW_CACHE:-${XDG_CACHE_HOME:-$HOME/.cache}/bashbrew}"
@@ -146,7 +157,8 @@ copy-tar() {
146157

147158
local d dockerfiles=()
148159
for d in "$src"/*/.bashbrew-dockerfile-name; do
149-
local bf="$(< "$d")" dDir="$(dirname "$d")"
160+
local bf; bf="$(< "$d")"
161+
local dDir; dDir="$(dirname "$d")"
150162
dockerfiles+=( "$dDir/$bf" )
151163
if [ "$bf" = 'Dockerfile' ]; then
152164
# if "Dockerfile.builder" exists, let's check that too (busybox, hello-world)
@@ -205,7 +217,8 @@ copy-tar() {
205217
# "find: warning: -path ./xxx/ will not match anything because it ends with /."
206218
local findGlobbedPath="${f%/}"
207219
findGlobbedPath="${findGlobbedPath#./}"
208-
globbed=( $(cd "$dDir" && find -path "./$findGlobbedPath") )
220+
local globbedStr; globbedStr="$(cd "$dDir" && find -path "./$findGlobbedPath")"
221+
local -a globbed=( $globbedStr )
209222
if [ "${#globbed[@]}" -eq 0 ]; then
210223
globbed=( "$f" )
211224
fi
@@ -243,9 +256,9 @@ git -C temp init --quiet
243256
git -C temp config user.name 'Bogus'
244257
git -C temp config user.email 'bogus@bogus'
245258

246-
bashbrew list "${images[@]}" | sort -uV > temp/_bashbrew-list || :
247-
bashbrew cat --format "$archesListTemplate" "${images[@]}" | sort -V > temp/_bashbrew-arches || :
248-
bashbrew cat --format "$sharedTagsListTemplate" "${images[@]}" | grep -vE '^$' | sort -V > temp/_bashbrew-shared-tags || :
259+
bashbrew list "${images[@]}" 2>>temp/_bashbrew.err | sort -uV > temp/_bashbrew-list || :
260+
bashbrew cat --format "$archesListTemplate" "${images[@]}" 2>>temp/_bashbrew.err | sort -V > temp/_bashbrew-arches || :
261+
bashbrew cat --format "$sharedTagsListTemplate" "${images[@]}" 2>>temp/_bashbrew.err | grep -vE '^$' | sort -V > temp/_bashbrew-shared-tags || :
249262
for image in "${images[@]}"; do
250263
if script="$(bashbrew cat -f "$template" "$image")"; then
251264
mkdir tar
@@ -260,9 +273,9 @@ git -C temp commit --quiet --allow-empty -m 'initial' || :
260273
git -C oi checkout --quiet pull
261274

262275
git -C temp rm --quiet -rf . || :
263-
bashbrew list "${images[@]}" | sort -uV > temp/_bashbrew-list || :
264-
bashbrew cat --format "$archesListTemplate" "${images[@]}" | sort -V > temp/_bashbrew-arches || :
265-
bashbrew cat --format "$sharedTagsListTemplate" "${images[@]}" | grep -vE '^$' | sort -V > temp/_bashbrew-shared-tags || :
276+
bashbrew list "${images[@]}" 2>>temp/_bashbrew.err | sort -uV > temp/_bashbrew-list || :
277+
bashbrew cat --format "$archesListTemplate" "${images[@]}" 2>>temp/_bashbrew.err | sort -V > temp/_bashbrew-arches || :
278+
bashbrew cat --format "$sharedTagsListTemplate" "${images[@]}" 2>>temp/_bashbrew.err | grep -vE '^$' | sort -V > temp/_bashbrew-shared-tags || :
266279
script="$(bashbrew cat -f "$template" "${images[@]}")"
267280
mkdir tar
268281
( eval "$script" | tar -xiC tar )

0 commit comments

Comments
 (0)