Skip to content

Commit

Permalink
Merge pull request #3404 from vegaprotocol/3400-ci-docker-image-build
Browse files Browse the repository at this point in the history
Fix CI docker image build (#3400)
  • Loading branch information
jeremyletang committed Apr 22, 2021
1 parent 150398c commit e75064c
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 50 deletions.
31 changes: 30 additions & 1 deletion .drone-github.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ steps:
./script/build-multiarch-no-cgo.sh default || exit 1 ;
else
echo "No Go files changed. Skipping building of binary." ;
rm -f cmd/vega/vega-* ;
rm -f cmd/vega/vega-* cmd/vegabenchmark/vegabenchmark-* ;
fi
depends_on:
- fetch
Expand All @@ -73,6 +73,35 @@ steps:
event:
- pull_request

- name: build_docker_image-PR
image: docker.pkg.github.com/vegaprotocol/devops-infra/cipipeline-docker:18.09.9
volumes:
- name: dockersock
path: /run/docker.sock
environment:
DOCKER_HOST: unix:///run/docker.sock
DOCKER_CONFIG_JSON:
from_secret: dockerconfig
commands:
- mkdir -p docker/bin
- find cmd -maxdepth 1 -and -not -name cmd | sed -e 's#^cmd/##' | while read -r app ; do
if test -f "cmd/$$app/$$app-linux-amd64" ; then
cp -a "cmd/$$app/$$app-linux-amd64" "docker/bin/$$app" || exit 1 ;
else
echo "dummy_noop" >"docker/bin/$$app" ;
fi ;
done
- tmptag="$$(openssl rand -hex 10)"
- docker build -t "docker.pkg.github.com/vegaprotocol/vega/vega:$$tmptag" docker/
- rm -rf docker/bin
- mkdir -p "$$HOME/.docker" ; echo "$$DOCKER_CONFIG_JSON" >"$$HOME/.docker/config.json" ; unset DOCKER_CONFIG_JSON
- docker rmi "docker.pkg.github.com/vegaprotocol/vega/vega:$$tmptag"
depends_on:
- build-PR
when:
event:
- pull_request

- name: test-PR
image: docker.pkg.github.com/vegaprotocol/devops-infra/cipipeline:1.16.2
volumes:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Vega specific entries
cmd/*/*-dbg
cmd/vega/vega*
cmd/vegabenchmark/vegabenchmark*
data/
.DS_Store
.envrc
Expand Down
135 changes: 86 additions & 49 deletions script/build-multiarch-no-cgo.sh
Original file line number Diff line number Diff line change
@@ -1,59 +1,96 @@
#!/usr/bin/env bash

default_combinations="linux,amd64
darwin,amd64
windows,amd64"

all_combinations="$default_combinations
linux,386
linux,arm64
linux,mips64
linux,mips64le
linux,mips
linux,mipsle
windows,386"
# Get a list of apps to build by looking at the directories in cmd.
mapfile -t apps < <(find cmd -maxdepth 1 -and -not -name cmd | sed -e 's#^cmd/##' | sort)

default_combinations=(
linux-amd64
darwin-amd64
windows-amd64
)

all_combinations=("${default_combinations[@]}")
all_combinations+=(
linux-386
linux-arm64
linux-mips64
linux-mips64le
linux-mips
linux-mipsle
windows-386
)

case "${1:-}" in
"") combinations="$default_combinations" ;;
default) combinations="$default_combinations" ;;
all) combinations="$all_combinations" ;;
*) echo "Invalid: $1" ; exit 1 ;;
"")
combinations=("${default_combinations[@]}")
;;
default)
combinations=("${default_combinations[@]}")
;;
all)
combinations=("${all_combinations[@]}")
;;
*)
echo "Invalid: $1"
exit 1
;;
esac

version="${DRONE_TAG:-$(git describe --tags 2>/dev/null)}"
version_hash="$(echo "${CI_COMMIT_SHA:-$(git rev-parse HEAD)}" | cut -b1-8)"
ldflags="-X main.CLIVersion=$version -X main.CLIVersionHash=$version_hash"

pidsfile="$(mktemp)"
for combo in $combinations ; do
goos="$(echo "$combo" | cut -f1 -d,)"
goarch="$(echo "$combo" | cut -f2 -d,)"
case "$goos" in
windows) o="cmd/vega/vega-$goos-$goarch.exe" ;;
*) o="cmd/vega/vega-$goos-$goarch" ;;
build_app() {
local app
app="${1:?}"

local combo goarch goos ldflags o pidsfile

case "$app" in
vega)
version="${DRONE_TAG:-$(git describe --tags 2>/dev/null)}"
version_hash="$(echo "${CI_COMMIT_SHA:-$(git rev-parse HEAD)}" | cut -b1-8)"
ldflags="-X main.CLIVersion=$version -X main.CLIVersionHash=$version_hash"
;;
*)
ldflags=""
;;
esac
env \
CGO_ENABLED=0 \
GOOS="$goos" \
GOARCH="$goarch" \
go build -o "$o" -ldflags "$ldflags" ./cmd/vega &
pid="$!"
echo "Building for OS=$goos arch=$goarch in subprocess $pid"
echo "$pid" >>"$pidsfile"
done

final=0
while read -r pid ; do
echo -n "Waiting for subprocess $pid ..."
wait "$pid"
code="$?"
if test "$code" = 0 ; then
echo "OK"
else
echo "code $code"
final="$((final+1))"
pidsfile="$(mktemp)"
for combo in "${combinations[@]}" ; do
goos="$(echo "$combo" | cut -f1 -d-)"
goarch="$(echo "$combo" | cut -f2 -d-)"
case "$goos" in
windows) o="cmd/$app/$app-$goos-$goarch.exe" ;;
*) o="cmd/$app/$app-$goos-$goarch" ;;
esac
env \
CGO_ENABLED=0 \
GOOS="$goos" \
GOARCH="$goarch" \
go build -o "$o" -ldflags "$ldflags" "./cmd/$app" &
pid="$!"
echo "Building $app for OS=$goos arch=$goarch in subprocess $pid"
echo "$pid" >>"$pidsfile"
done

final=0
while read -r pid ; do
echo -n "Waiting for subprocess $pid ..."
wait "$pid"
code="$?"
if test "$code" = 0 ; then
echo "OK"
else
echo "code $code"
final="$((final+1))"
fi
done <"$pidsfile"
rm -f "$pidsfile"

if test "$final" -gt 0 ; then
echo "Failed to build $app"
exit "$final"
fi
done <"$pidsfile"
rm -f "$pidsfile"
}

exit "$final"
for app in "${apps[@]}" ; do
build_app "$app"
done

0 comments on commit e75064c

Please sign in to comment.