-
Notifications
You must be signed in to change notification settings - Fork 234
/
setup_env
executable file
·145 lines (128 loc) · 5.33 KB
/
setup_env
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
#!/usr/bin/env bash
# This script sets up the global build environment. This should be called before any other build scripts,
# as the other build scripts assume these global variables are set. The global variables are written to
# the file in $BASH_ENV, which means that any new bash shells launched within the lifetime of the machine
# context will have these variables set. This happens for example when CircleCI runs a new "step".
# The script should be sourced from the root of the repository, e.g:
# source ./build-system/scripts/setup_env
# This ensures the resultant variables are set in the calling shell.
# Save current options as we might be sourcing.
CURRENT_SHELL_OPTS=$(set +o)
trap 'eval "$CURRENT_SHELL_OPTS"' EXIT
[ -n "${BUILD_SYSTEM_DEBUG:-}" ] && set -x # conditionally trace
set -eu
COMMIT_HASH=$1
COMMIT_TAG=${2##*aztec-packages-}
JOB_NAME=$3
GIT_REPOSITORY_URL=${4:-}
BRANCH=${5:-}
PULL_REQUEST=${6:-}
BASH_ENV=${BASH_ENV:-}
ROOT_PATH=$(git rev-parse --show-toplevel)
BUILD_SYSTEM_PATH=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
PROJECT=$(cat $ROOT_PATH/PROJECT)
COMMIT_MESSAGE=$(git log -n 1 --pretty=format:"%s" $COMMIT_HASH)
PATH=$PATH:$BUILD_SYSTEM_PATH/scripts
check_env
export BRANCH
echo "COMMIT_HASH=$COMMIT_HASH"
echo "COMMIT_TAG=$COMMIT_TAG"
echo "JOB_NAME=$JOB_NAME"
echo "GIT_REPOSITORY_URL=$GIT_REPOSITORY_URL"
echo "BRANCH=$BRANCH"
echo "PULL_REQUEST=$PULL_REQUEST"
# If the user has requested to perform a "dry deploy", we set the commit tag to fake version, and set DRY_DEPLOY to 1.
if [[ "$COMMIT_MESSAGE" == *"[ci dry-deploy]"* ]]; then
COMMIT_TAG=v999.999.999
export DRY_DEPLOY=1
else
export DRY_DEPLOY=0
fi
if should_deploy; then
if [ -n "${COMMIT_TAG:-}" ]; then
# Extract the deploy env from the commit tag, if it has one, e.g. testnet.
# If we have one, we look something like v2.1.123-testnet.0. This is a "non production" release.
if [[ "$COMMIT_TAG" == *"-"* ]]; then
# Strips the trailing '.XX' from the end of the commit tag.
TEMP=${COMMIT_TAG%.*}
# Strips the 'vX.Y.ZZZ-' from the front of the commit tag, leaving the e.g. 'testnet'.
DEPLOY_ENV=${TEMP##*-}
elif [ ! "$DRY_DEPLOY" -eq 1 ]; then
DEPLOY_ENV=prod
else
DEPLOY_ENV=dev
fi
else
# If we're on master, this is our devnet.
DEPLOY_ENV="dev"
fi
fi
# - The deploy tag (used in api paths, subdomains), is a concatenation of the project name and deploy environment,
# e.g. aztec-dev, aztec-testnet, or aztec-prod
if [ -n "${DEPLOY_ENV:-}" ]; then
DEPLOY_TAG=$PROJECT-$DEPLOY_ENV
fi
if [ -z "$BASH_ENV" ]; then
BASH_ENV=$(mktemp)
fi
echo export ROOT_PATH=$ROOT_PATH >> $BASH_ENV
echo export BUILD_SYSTEM_PATH=$BUILD_SYSTEM_PATH >> $BASH_ENV
echo export DOCKER_BUILDKIT=${DOCKER_BUILDKIT:-1} >> $BASH_ENV
echo export BUILDKIT_PROGRESS=plain >> $BASH_ENV
echo export SSH_CONFIG_PATH=$BUILD_SYSTEM_PATH/remote/ssh_config >> $BASH_ENV
echo export PATH=\$PATH:$BUILD_SYSTEM_PATH/scripts >> $BASH_ENV
echo export AWS_DEFAULT_REGION=eu-west-2 >> $BASH_ENV
echo export ECR_REGION=us-east-2 >> $BASH_ENV
echo export AWS_ACCOUNT=278380418400 >> $BASH_ENV
echo export ECR_URL=278380418400.dkr.ecr.us-east-2.amazonaws.com >> $BASH_ENV
echo export ECR_DEPLOY_REGION=eu-west-2 >> $BASH_ENV
echo export DOCKERHUB_ACCOUNT=aztecprotocol >> $BASH_ENV
echo export DOCKERHUB_USERNAME=aztecprotocolci >> $BASH_ENV
echo export PROJECT=$PROJECT >> $BASH_ENV
echo export COMMIT_HASH=$COMMIT_HASH >> $BASH_ENV
echo export COMMIT_TAG=$COMMIT_TAG >> $BASH_ENV
echo "export COMMIT_MESSAGE='${COMMIT_MESSAGE//\'/\'\\\'\'}'" >> $BASH_ENV
echo export JOB_NAME=$JOB_NAME >> $BASH_ENV
echo export GIT_REPOSITORY_URL=$GIT_REPOSITORY_URL >> $BASH_ENV
echo export DEPLOY_ENV=${DEPLOY_ENV:-} >> $BASH_ENV
echo export DEPLOY_TAG=${DEPLOY_TAG:-} >> $BASH_ENV
echo export BRANCH=$BRANCH >> $BASH_ENV
echo export PULL_REQUEST=$PULL_REQUEST >> $BASH_ENV
echo export DRY_DEPLOY=${DRY_DEPLOY:-0} >> $BASH_ENV
echo export DOCKER_CLI_HINTS=false >> $BASH_ENV
# We want very strict failures on any failing command, undefined variable, or commands that pipe to other commands.
echo set -euo pipefail >> $BASH_ENV
# Enable logging if [ci debug] is in commit message.
if [[ "$COMMIT_MESSAGE" == *"[ci debug]"* ]]; then
echo export BUILD_SYSTEM_DEBUG=1 >> $BASH_ENV
echo set -x >> $BASH_ENV
fi
# Only run the following if we're the result of a commit (i.e. not being run manually).
if [ -n "$COMMIT_HASH" ]; then
# Our alpine build image has yq installed, ubuntu build instances install from gzips in ../bin.
if [ ! -f /etc/alpine-release ]; then
BIN_PATH=$BUILD_SYSTEM_PATH/bin
echo export PATH=$BIN_PATH:\$PATH >> $BASH_ENV
ARCH=$(uname -m)
if [ "$ARCH" == "x86_64" ]; then
gunzip -c $BIN_PATH/yq_linux_amd64 > $BIN_PATH/yq
elif [ "$ARCH" == "aarch64" ]; then
gunzip -c $BIN_PATH/yq_linux_arm64 > $BIN_PATH/yq
else
echo "Unknown arch: $ARCH"
exit 1
fi
chmod +x $BIN_PATH/yq
fi
# Install and ensure correct permissions on build instance key.
mkdir -p ~/.ssh
echo ${BUILD_INSTANCE_KEY:-} | base64 -d > ~/.ssh/build_instance_key
chmod 600 ~/.ssh/build_instance_key
if [[ "$COMMIT_MESSAGE" == *"[ci debug]"* ]]; then
echo export BUILD_SYSTEM_DEBUG=1 >> $BASH_ENV
fi
fi
# Print for visibility.
cat $BASH_ENV
# Having written the variables to $BASH_ENV, we now want to set them in this shell context.
source $BASH_ENV