-
Notifications
You must be signed in to change notification settings - Fork 20
/
release.sh
executable file
·156 lines (124 loc) · 4.77 KB
/
release.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/bin/bash
# Copyright 2020 Adevinta
# shellcheck disable=SC1091
set -e
trap "exit" INT
# Load Libraries
. _scripts/libgit.sh
. _scripts/libdocker.sh
get_tag_check() {
local -r object_path="${1:?path to object argument required}"
echo "$(git_commit_id go.mod)-$(git_commit_id "$object_path")"
}
# Load required env vars
eval "$(git_env)"
eval "$(dkr_env)"
LOG_TIME=$(date +"%s")
log_msg() {
local previous=$LOG_TIME
LOG_TIME=$(date +"%s")
echo "$(date -u +'%Y-%m-%dT%H:%M:%SZ') [$((LOG_TIME-previous))s] -- $1"
}
PLATFORMS=${PLATFORMS:-"linux/arm64 linux/amd64"}
BRANCH=${TRAVIS_BRANCH:-$(git_branch .)}
BRANCH=${BRANCH//\//-} # Replace / with - for branch names such as dependabot generated ones
IMAGE_TAGS=()
CACHE_TAGS=(edge)
if [[ $BRANCH == "master" ]]; then
IMAGE_TAGS+=(latest edge)
FORCE_BUILD="${FORCE_BUILD:-true}"
ADD_TAG_CHECK=true
elif [[ $TRAVIS_TAG != "" ]]; then
IMAGE_TAGS+=("$TRAVIS_TAG")
FORCE_BUILD="${FORCE_BUILD:-true}"
ADD_TAG_CHECK=false
else
IMAGE_TAGS+=("$BRANCH" "$BRANCH-$(git rev-parse --short HEAD)")
FORCE_BUILD="${FORCE_BUILD:-false}"
ADD_TAG_CHECK=true
CACHE_TAGS+=("$BRANCH") # First time will print a message => ERROR importing cache manifest from XXXX
fi
log_msg "Starting FORCE_BUILD=$FORCE_BUILD"
CHECKS=()
for cf in cmd/*; do
check=$(basename "$cf")
if [[ $FORCE_BUILD == "false" ]]; then
TAG_CHECK="$(get_tag_check "cmd/$check")"
# Check if check version (code+dep) has been already pushed to Registry
if [[ $(dkr_image_exists "$check" "$TAG_CHECK") == true ]]; then
echo "Skipping $DKR_USERNAME/$check:$TAG_CHECK exists"
continue
fi
fi
CHECKS+=("$check")
done
log_msg "Computed list of checks to build: [${CHECKS[*]}]"
if [ ${#CHECKS[@]} -eq 0 ]; then
exit
fi
# Download go dependencies
go mod download
log_msg "Downloaded go mod"
# Login into registry (authenticated pulls)
dkr_login > /dev/null
if ! docker buildx inspect multiarch; then
# see https://github.com/docker/buildx/issues/495#issuecomment-761562905
docker run --rm -it --privileged multiarch/qemu-user-static --reset -p yes
docker buildx create --name multiarch --driver docker-container --use --bootstrap
log_msg "Created buildx"
fi
# Generate a checktypes for the current tag.
TEST_TAG="${IMAGE_TAGS[0]}"
# Generate a checktypes.json that could be used later on for testint with vulcan-local.
vulcan-check-catalog -registry-url "$DKR_USERNAME" -tag "$TEST_TAG" -output checktypes.json cmd/
log_msg "Generated checktypes.json file with tags $TEST_TAG"
export VULCAN_CHECKTYPES=./checktypes.json
BUILDX_ARGS=()
BUILDX_ARGS+=("--label" "org.opencontainers.image.revision=$(git rev-parse --short HEAD)")
BUILDX_ARGS+=("--label" "org.opencontainers.image.ref=https://github.com/adevinta/vulcan-checks")
# See https://stackoverflow.com/questions/76499510/why-i-can-not-pull-the-docker-image-that-is-public-in-dockerhub
BUILDX_ARGS+=("--provenance=false")
# Iterate over all checks
for check in "${CHECKS[@]}"; do
CHECK_PLATFORMS=$PLATFORMS
if [[ $check =~ $ARM64_EXCLUDE ]]; then
CHECK_PLATFORMS=${PLATFORMS// linux\/arm64/}
else
CHECK_PLATFORMS=$PLATFORMS
fi
# Build the go app
for PLATFORM in $CHECK_PLATFORMS; do
OS=$(echo "$PLATFORM" | cut -f1 -d/)
ARCH=$(echo "$PLATFORM" | cut -f2 -d/)
CGO_ENABLED=0 GOOS=$OS GOARCH=$ARCH go build -ldflags="-s -w" -o "cmd/$check/$OS/$ARCH/$check" "$PWD/cmd/$check"
log_msg "Builded go $check:$PLATFORM"
done
BUILDX_CHECK_ARGS=()
for tag in "${CACHE_TAGS[@]}"; do
BUILDX_CHECK_ARGS+=("--cache-from" "type=registry,ref=$DKR_USERNAME/$check:$tag")
done
if [ -x "cmd/$check/test.sh" ]; then
log_msg "Builded test image $DKR_USERNAME/$check:$TEST_TAG"
# Build the image without pushing
docker buildx build "${BUILDX_CHECK_ARGS[@]}" \
--tag "$DKR_USERNAME/$check:$TEST_TAG" \
--platform="linux/amd64" \
"cmd/$check" --load
log_msg "Testing image $DKR_USERNAME/$check:$TEST_TAG"
. "cmd/$check/test.sh" "$check"
fi
for tag in "${IMAGE_TAGS[@]}"; do
BUILDX_CHECK_ARGS+=("--tag" "$DKR_USERNAME/$check:$tag")
done
if [[ $ADD_TAG_CHECK == "true" ]]; then
BUILDX_CHECK_ARGS+=("--tag" "$DKR_USERNAME/$check:$(get_tag_check "cmd/$check")")
fi
docker buildx build "${BUILDX_ARGS[@]}" "${BUILDX_CHECK_ARGS[@]}" \
--cache-to "type=inline" \
--label "org.opencontainers.image.title=$check" \
--label "org.opencontainers.image.created=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" \
--platform="${CHECK_PLATFORMS// /,}" \
"cmd/$check" --push
log_msg "Pushed image $check:[${IMAGE_TAGS[*]}]"
done
docker buildx rm multiarch