-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·113 lines (93 loc) · 2.81 KB
/
build.sh
File metadata and controls
executable file
·113 lines (93 loc) · 2.81 KB
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
#! /bin/bash
usage="$(basename "$0") [-l | -a | -s] -- program to build docker images of terraform for multiple platforms.
where:
-l only build docker image of latest GitHub release
-a build docker image of all available GitHub releases
-s build docker image only on stable GitHub release"
# function joinByChar() {
# local IFS="$1"
# shift
# echo "$*"
# }
function invertArray() {
local array=("$@")
local tmpArray=()
## get length of array
len=${#array[@]}
## Use bash for loop
for (( i=0; i<$len; i++ )); do
tmpArray[$len - $i]=${array[$i]}
done
echo "${tmpArray[@]}"
}
function buildTagArgs() {
local tags=("$@")
local tagArgs=()
## Iterate over each item of the array
for t in "${tags[@]}"; do
tagArgs+=("--tag ${DOCKER_IMAGE}:${t}")
done
echo "${tagArgs[@]}"
}
if [[ -z ${DOCKER_IMAGE+set} ]]; then
echo "Environment variable DOCKER_IMAGE not set. Run \"export DOCKER_IMAGE=containous/whoami\""
exit 2
fi
if [[ -z ${PLATFORMS+set} ]]; then
echo "Environment variable PLATFORMS not set. Run \"export PLATFORMS=\"linux/amd64,linux/arm64\""
exit 2
fi
if [[ -z ${ORGANIZATION+set} ]]; then
echo "Environment variable ORGANIZATION not set. Run \"export ORGANIZATION=hashicorp\""
exit 2
fi
if [[ -z ${REPOSITORY+set} ]]; then
echo "Environment variable REPOSITORY not set. Run \"export REPOSITORY=terraform\""
exit 2
fi
while getopts :hlas flag
do
case "${flag}" in
h)
echo "$usage"
exit
;;
l)
## Only fetch latest releases
LATEST=true
VERSIONS=($(curl --silent https://api.github.com/repos/${ORGANIZATION}/${REPOSITORY}/releases/latest | jq --raw-output '.name' | tr -d 'v'))
set -e
;;
a)
## Get all GitHub releases
VERSIONS=($(curl --silent https://api.github.com/repos/${ORGANIZATION}/${REPOSITORY}/releases | jq --raw-output '.[].name' | tr -d 'v'))
;;
s)
## Filter out alpha and beta releases
VERSIONS=($(curl --silent https://api.github.com/repos/${ORGANIZATION}/${REPOSITORY}/releases | jq --raw-output '.[]|select(.name|test("^v\\d*\\.\\d*\\.\\d*$"))|.name' | tr -d 'v'))
;;
esac
done
## Invert array
INVERTED_VERSIONS=($(invertArray ${VERSIONS[@]}))
for VERSION in "${INVERTED_VERSIONS[@]}"
do
TAGS=($VERSION)
if [[ $LATEST ]]; then
TAGS+=("latest")
fi
TAG_ARGS=$(buildTagArgs ${TAGS[@]})
echo "Build Info:"
echo " VERSION: ${VERSION}"
echo " Platforms: ${PLATFORMS}"
for t in "${TAGS[@]}"; do
echo " Tag: $DOCKER_IMAGE:${t}"
done
docker buildx build \
--push \
--file ./docker/dockerfile.releases \
--build-arg TERRAFORM_VERSION=${VERSION} \
--platform ${PLATFORMS} \
${TAG_ARGS} \
.
done