2015-10-16 01:21:55 +02:00
#!/bin/bash
set -eo pipefail
2016-07-27 19:06:59 +02:00
# TODO something clever with this pattern to get the exact list of _tags_ which have changed, not just repos:
#format='{{ range .Entries }}{{ join " " (join ":" $.RepoName (.Tags | first)) .GitRepo .GitFetch .GitCommit .Directory }}{{ "\n" }}{{ end }}'
#comm -13 \
# <(bashbrew cat -f "$format" https://github.com/docker-library/official-images/raw/master/library/docker | sort) \
# <(bashbrew cat -f "$format" https://raw.githubusercontent.com/infosiftr/stackbrew/d92ffa4b5f8a558c22c5d0a7e0f33bff8fae990b/library/docker | sort) \
# | cut -d' ' -f1
2016-06-06 23:17:32 +02:00
# make sure we can GTFO
trap 'echo >&2 Ctrl+C captured, exiting; exit 1' SIGINT
# start with an error if Docker isn't working...
docker version > /dev/null
2015-11-10 02:26:04 +01:00
dir = " $( dirname " $( readlink -f " $BASH_SOURCE " ) " ) "
2015-10-16 01:21:55 +02:00
usage( ) {
cat <<-EOUSAGE
usage: $0 [ PR number] [ repo[ :tag] ]
ie: $0 1024
$0 9001 debian php django
2015-11-10 02:26:04 +01:00
$0 0 hylang # special case that runs against local directory
2015-10-16 01:21:55 +02:00
This script builds and tests the specified pull request to official-images and
provides ouput in markdown for commenting on the pull request.
EOUSAGE
}
pull = " $1 "
shift || { usage >& 2 && exit 1; }
2015-11-10 02:26:04 +01:00
if [ -z " $BASHBREW_SECOND_STAGE " ] ; then
2020-04-24 23:51:33 +02:00
dockerRepo = 'oisupport/bashbrew'
dockerBase = " $dockerRepo :base "
dockerImage = " $dockerRepo :test-pr "
2016-05-25 18:54:33 +02:00
2020-04-24 23:51:33 +02:00
bashbrewVersion = " $( < " $dir /bashbrew-version " ) "
docker build -t " $dockerBase " --pull " https://github.com/docker-library/bashbrew.git#v $bashbrewVersion " > /dev/null
docker build -t " $dockerImage " " $dir " > /dev/null
2016-06-06 23:17:32 +02:00
2020-04-23 20:35:41 +02:00
args = ( --init )
2016-06-06 19:41:10 +02:00
2015-11-10 02:26:04 +01:00
if [ " $pull " = '0' ] ; then
2016-06-06 19:41:10 +02:00
args += ( --name " bashbrew-test-local- $RANDOM " )
2015-11-10 02:26:04 +01:00
else
2016-06-06 19:41:10 +02:00
args += ( --name " bashbrew-test-pr- $pull " )
fi
args += (
-v /var/run/docker.sock:/var/run/docker.sock
--group-add 0
2016-06-06 23:17:32 +02:00
-v /etc/passwd:/etc/passwd:ro
-v /etc/group:/etc/group:ro
2016-06-06 19:41:10 +02:00
)
if getent group docker & > /dev/null; then
args += ( --group-add " $( getent group docker | cut -d: -f3) " )
fi
# if we don't have DOCKER_HOST set, let's bind-mount cache for speed!
if [ -z " $DOCKER_HOST " ] ; then
export BASHBREW_CACHE = " ${ BASHBREW_CACHE :- ${ XDG_CACHE_HOME :- $HOME /.cache } /bashbrew } " # resolve path to current "host-side" cache directory
mkdir -p " $BASHBREW_CACHE " # ensure it's created by our user, not root
export BASHBREW_CACHE = " $( cd " $BASHBREW_CACHE " && pwd -P) " # readlink -f
args += (
-v " $BASHBREW_CACHE " :/bashbrew-cache
-e BASHBREW_CACHE = /bashbrew-cache
# make sure our user in the container can read it
--group-add " $( stat -c '%g' " $BASHBREW_CACHE " ) "
)
2016-06-06 21:58:27 +02:00
else
2016-06-06 23:17:32 +02:00
dockerGid = " $(
docker run -i --rm " ${ args [@] } " " $dockerImage " sh -e <<-'EOSH'
exec 2>/dev/null
stat -c '%g' /var/run/docker.sock \
|| getent group docker | cut -d: -f3
EOSH
) " || true
if [ " $dockerGid " ] ; then
args += ( --group-add " $dockerGid " )
fi
2015-11-10 02:26:04 +01:00
fi
2016-05-25 18:54:33 +02:00
2016-06-06 19:41:10 +02:00
args += (
--user " $( id -u) " :" $( id -g) "
$( id -G | xargs -n1 echo --group-add)
-e BASHBREW_SECOND_STAGE = 1
)
2017-11-01 21:34:01 +01:00
for e in " ${ !BASHBREW_@ } " ; do
2017-11-01 20:25:07 +01:00
case " $e " in
BASHBREW_SECOND_STAGE| BASHBREW_CACHE| BASHBREW_LIBRARY) ; ;
*)
args += ( -e " $e " )
; ;
esac
2017-11-01 19:51:28 +01:00
done
2016-06-06 23:17:32 +02:00
cmd = ( ./test-pr.sh " $pull " " $@ " )
2016-06-06 19:41:10 +02:00
2020-05-20 02:33:52 +02:00
if [ -t 0 ] && [ -t 1 ] ; then
2016-09-13 23:16:20 +02:00
# only add "-t" if we have a TTY
args += ( -t )
fi
exec docker run -i --rm " ${ args [@] } " " $dockerImage " " ${ cmd [@] } "
2015-11-10 02:26:04 +01:00
fi
if [ -d .git ] ; then
echo >& 2 'error: something has gone horribly wrong; .git already exists'
echo >& 2 ' why do you have BASHBREW_SECOND_STAGE set?'
exit 1
fi
if [ " $pull " = '0' ] ; then
commit = 'FAKE'
else
2016-06-06 21:58:27 +02:00
dir = " $( mktemp -d) "
trap " rm -rf ' $dir ' " EXIT
cd " $dir "
2015-11-10 02:26:04 +01:00
# TODO we only have "git version 2.4.1" which doesn't support "clone -q" :(
git init -q .
git remote add origin https://github.com/docker-library/official-images.git
git fetch -q origin
git reset -q --hard origin/master
git config user.name 'nobody'
git config user.email 'nobody@nowhere.noplace'
git fetch -q origin " pull/ $pull /head:pr- $pull "
2016-06-03 19:52:03 +02:00
git merge -q --no-edit " pr- $pull " > /dev/null
2015-11-10 02:26:04 +01:00
commit = " $( git log -1 --format= format:%h " pr- $pull " ) "
fi
2015-10-16 01:21:55 +02:00
2016-06-07 18:01:25 +02:00
export BASHBREW_LIBRARY = " $PWD /library "
2015-10-16 01:21:55 +02:00
if [ " $# " -eq 0 ] ; then
IFS = $'\n'
2015-11-10 02:26:04 +01:00
files = ( $( git diff --name-only origin/master...HEAD -- library | xargs -n1 basename) )
2015-10-16 01:21:55 +02:00
unset IFS
2015-11-10 02:26:04 +01:00
# TODO narrow this down into groups of the exact tags for each image that changed >:)
2015-10-16 01:21:55 +02:00
else
files = ( " $@ " )
fi
if [ ${# files [@] } -eq 0 ] ; then
echo >& 2 'no files in library/ changed in PR #' " $pull "
exit 0
fi
join( ) {
sep = " $1 "
arg1 = " $2 "
shift 2
echo -n " $arg1 "
[ $# -gt 0 ] && printf " ${ sep } %s " " $@ "
}
2016-06-06 19:41:10 +02:00
IFS = $'\n'
files = ( $( bashbrew list --repos --uniq --build-order " ${ files [@] } " ) )
unset IFS
2016-05-25 18:54:33 +02:00
2017-11-03 15:52:15 +01:00
echo 'Build test of' '#' " $pull " ';' " $commit " ';' '`' " ${ BASHBREW_ARCH :- amd64 } " '`' '(`' " $( join '`, `' " ${ files [@] } " ) " '`):'
2016-12-22 21:21:14 +01:00
declare -A failedBuild = ( ) failedTests = ( )
2015-11-10 02:26:04 +01:00
for img in " ${ files [@] } " ; do
2016-06-06 19:41:10 +02:00
IFS = $'\n'
2017-02-06 18:59:00 +01:00
uniqImgs = ( $( bashbrew list --uniq --build-order " $img " ) )
2020-04-25 01:43:14 +02:00
uniqImgs = ( $( bashbrew cat --format '{{ if .TagEntry.HasArchitecture arch }}{{ $.RepoName }}:{{ .TagEntry.Tags | first }}{{ end }}' " ${ uniqImgs [@] } " ) ) # filter to just the set supported by the current BASHBREW_ARCH
2016-06-06 19:41:10 +02:00
unset IFS
2015-10-16 01:21:55 +02:00
echo
echo '```console'
2016-06-06 19:41:10 +02:00
for uniqImg in " ${ uniqImgs [@] } " ; do
2016-12-22 21:21:14 +01:00
imgRepo = " ${ uniqImg %% : * } "
2016-06-06 19:41:10 +02:00
echo
echo '$ bashbrew build' " $uniqImg "
2016-06-08 19:55:01 +02:00
if bashbrew build --pull= missing " $uniqImg " ; then
2016-06-06 19:41:10 +02:00
echo
echo '$ test/run.sh' " $uniqImg "
if ! ./test/run.sh " $uniqImg " ; then
2016-12-22 21:21:14 +01:00
failedTests[ $imgRepo ] += " $uniqImg "
2016-06-06 19:41:10 +02:00
fi
else
2016-12-22 21:21:14 +01:00
failedBuild[ $imgRepo ] += " $uniqImg "
2015-10-16 01:21:55 +02:00
fi
2016-06-06 19:41:10 +02:00
echo
done
2015-10-16 01:21:55 +02:00
echo '```'
done
2016-12-22 21:21:14 +01:00
echo
2016-06-06 19:41:10 +02:00
if [ " ${# failedBuild [@] } " -gt 0 ] ; then
2016-12-22 21:21:14 +01:00
echo 'The following images failed to build:'
echo
for repo in " ${ !failedBuild[@] } " ; do
echo '- `' " $repo " '`:'
for img in ${ failedBuild [ $repo ] } ; do
echo ' - `' " $img " '`'
done
done
2016-06-06 19:41:10 +02:00
echo
fi
if [ " ${# failedTests [@] } " -gt 0 ] ; then
2015-10-16 01:21:55 +02:00
echo
2016-12-22 21:21:14 +01:00
echo 'The following images failed at least one test:'
echo
for repo in " ${ !failedTests[@] } " ; do
echo '- `' " $repo " '`:'
for img in ${ failedTests [ $repo ] } ; do
echo ' - `' " $img " '`'
done
done
echo
2015-10-16 01:21:55 +02:00
fi