-
Notifications
You must be signed in to change notification settings - Fork 28
/
build.sh
executable file
·73 lines (59 loc) · 1.9 KB
/
build.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
#!/usr/bin/env bash
set -e
if [ $# != 2 ]; then
echo "Usage: $0 <app> <arch>"
echo "* app: subfolder in apps/hetzner/"
echo "* arch: either amd64 or arm64"
echo ""
exit 1
fi
APP=$1
ARCH=$2
if [ -z "$HCLOUD_TOKEN" ]; then
echo "No HCLOUD_TOKEN set"
exit 1
fi
GIT_REVISION=`git symbolic-ref HEAD 2> /dev/null | cut -b 12-`-`git log --pretty=format:\"%h\" -1 | xargs`
SNAPSHOT_NAME=${APP}-${ARCH}-${GIT_REVISION}-${CI_PIPELINE_ID}
export HCLOUD_SERVER_LOCATION="fsn1"
# Target architecture for the new VM created by Packer
if [ "$ARCH" == "amd64" ]; then
export HCLOUD_SERVER_TYPE_BEFORE_UPSCALE="cx11"
export HCLOUD_SERVER_TYPE="cpx21"
fi
if [ "$ARCH" == "arm64" ]; then
export HCLOUD_SERVER_TYPE_BEFORE_UPSCALE="cax11"
export HCLOUD_SERVER_TYPE="cax21"
fi
# Local architecture (used to execute hcloud and packer)
LOCAL_ARCH='amd64'
if [ $(uname -i) == 'aarch64' ]; then
LOCAL_ARCH='arm64'
fi
if [ -z "$CI_JOB_ID" ]; then
export CI_JOB_ID=`date +%s`
fi
# Download dependencies
rm -rf hcloud || true
mkdir hcloud
cd hcloud
wget -q "https://cs1.hetzner.de/cloud/hcloud-linux-${LOCAL_ARCH}.tar.gz"
tar xzf hcloud-linux-${LOCAL_ARCH}.tar.gz
export PATH=$PATH:$(pwd)
cd ..
rm -rf packer || true
mkdir packer
cd packer
wget -q "https://releases.hashicorp.com/packer/1.10.3/packer_1.10.3_linux_${LOCAL_ARCH}.zip"
unzip packer_1.10.3_linux_${LOCAL_ARCH}.zip
export PATH=$PATH:$(pwd)
cd ..
# Check syntax
packer validate -syntax-only apps/hetzner/${APP}/
# Create VMs, install one-click-apps, create snapshot
packer build -color=false -on-error=abort -var snapshot_name=${SNAPSHOT_NAME} apps/hetzner/${APP}/
# Tag the resulting image with the application it contains
snapshot_id=$(hcloud image list -o noheader -o "columns=id,description" | grep "${SNAPSHOT_NAME}" | awk '{print $1}')
hcloud image add-label $snapshot_id app=${APP}
# Cleanup any VMs, etc. that might be remaining
./cleanup.sh ${APP}