github.com/discordapp/buildkite-agent@v2.6.6+incompatible/templates/1.0-beta.1-6/bootstrap.sh (about) 1 #!/bin/bash 2 3 # 4 # _ _ _ _ _ _ _ _ _ _ 5 # | | (_) | | | | (_) | | | | | | | 6 # | |__ _ _ _| | __| | | ___| |_ ___ ______| |__ ___ ___ | |_ ___| |_ _ __ __ _ _ __ 7 # | '_ \| | | | | |/ _` | |/ / | __/ _ \______| '_ \ / _ \ / _ \| __/ __| __| '__/ _` | '_ \ 8 # | |_) | |_| | | | (_| | <| | || __/ | |_) | (_) | (_) | |_\__ \ |_| | | (_| | |_) | 9 # |_.__/ \__,_|_|_|\__,_|_|\_\_|\__\___| |_.__/ \___/ \___/ \__|___/\__|_| \__,_| .__/ 10 # | | 11 # |_| 12 # https://github.com/buildbox/agent/blob/master/templates/bootstrap.sh 13 14 BUILDBOX_BOOTSTRAP_VERSION="buildkite-bootstrap version 1.0-beta.1" 15 16 ############################################################## 17 # 18 # BOOTSTRAP FUNCTIONS 19 # These functions are used throughout the bootstrap.sh file 20 # 21 ############################################################## 22 23 BUILDBOX_PROMPT="\033[90m$\033[0m" 24 25 function buildbox-prompt-and-run { 26 echo -e "$BUILDBOX_PROMPT $1" 27 eval $1 28 } 29 30 function buildbox-run { 31 echo -e "$BUILDBOX_PROMPT $1" 32 eval $1 33 EVAL_EXIT_STATUS=$? 34 35 if [[ $EVAL_EXIT_STATUS -ne 0 ]]; then 36 exit $EVAL_EXIT_STATUS 37 fi 38 } 39 40 ############################################################## 41 # 42 # PATH DEFAULTS 43 # Come up with the paths used throughout the bootstrap.sh file 44 # 45 ############################################################## 46 47 : ${BUILDBOX_PATH:="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"} 48 : ${BUILDBOX_BIN_PATH:="$BUILDBOX_PATH/bin"} 49 : ${BUILDBOX_BUILD_PATH:="$BUILDBOX_PATH/builds"} 50 51 # Add the $BUILDBOX_BIN to the $PATH 52 export PATH="$BUILDBOX_BIN_PATH:$PATH" 53 54 # Send the bootstrap version back to Buildbox 55 buildbox-agent build-data set "buildkite:bootstrap:version" $BUILDBOX_BOOTSTRAP_VERSION 56 57 # Come up with the place that the repository will be checked out to 58 SANITIZED_AGENT_NAME=$(echo $BUILDBOX_AGENT_NAME | tr -d '"') 59 PROJECT_FOLDER_NAME="$SANITIZED_AGENT_NAME/$BUILDBOX_PROJECT_SLUG" 60 BUILDBOX_BUILD_CHECKOUT_PATH="$BUILDBOX_BUILD_PATH/$PROJECT_FOLDER_NAME" 61 62 ############################################################## 63 # 64 # REPOSITORY HANDLING 65 # Creates the build folder and makes sure we're running the 66 # build at the right commit. 67 # 68 ############################################################## 69 70 # Remove the checkout folder if BUILDBOX_CLEAN_CHECKOUT is present 71 if [[ "$BUILDBOX_CLEAN_CHECKOUT" == "true" ]]; then 72 echo '--- Cleaning project checkout' 73 74 buildbox-run "rm -rf \"$BUILDBOX_BUILD_CHECKOUT_PATH\"" 75 fi 76 77 echo '--- Preparing build folder' 78 79 buildbox-run "mkdir -p \"$BUILDBOX_BUILD_CHECKOUT_PATH\"" 80 buildbox-run "cd \"$BUILDBOX_BUILD_CHECKOUT_PATH\"" 81 82 # Do we need to do a git checkout? 83 if [[ ! -d ".git" ]]; then 84 # If it's a first time SSH git clone it will prompt to accept the host's 85 # fingerprint. To avoid this add the host's key to ~/.ssh/known_hosts ahead 86 # of time: 87 # ssh-keyscan -H host.com >> ~/.ssh/known_hosts 88 buildbox-run "git clone \"$BUILDBOX_REPO\" . -qv" 89 fi 90 91 buildbox-run "git clean -fdq" 92 buildbox-run "git fetch -q" 93 94 # Allow checkouts of forked pull requests on GitHub only. See: 95 # https://help.github.com/articles/checking-out-pull-requests-locally/#modifying-an-inactive-pull-request-locally 96 if [[ "$BUILDBOX_PULL_REQUEST" != "false" ]] && [[ "$BUILDBOX_PROJECT_PROVIDER" == *"github"* ]]; then 97 buildbox-run "git fetch origin \"+refs/pull/$BUILDBOX_PULL_REQUEST/head:\"" 98 elif [[ "$BUILDBOX_TAG" == "" ]]; then 99 # Default empty branch names 100 : ${BUILDBOX_BRANCH:=master} 101 102 buildbox-run "git reset --hard origin/$BUILDBOX_BRANCH" 103 fi 104 105 buildbox-run "git checkout -qf \"$BUILDBOX_COMMIT\"" 106 107 # Grab author and commit information and send it back to Buildbox 108 buildbox-agent build-data set "buildkite:git:commit" "`git show "$BUILDBOX_COMMIT" -s --format=fuller --no-color`" 109 buildbox-agent build-data set "buildkite:git:branch" "`git branch --contains "$BUILDBOX_COMMIT" --no-color`" 110 111 ############################################################## 112 # 113 # RUN THE BUILD 114 # Determines how to run the build, and then runs it 115 # 116 ############################################################## 117 118 if [[ "$BUILDBOX_SCRIPT_PATH" == "" ]]; then 119 echo "ERROR: No script to run. Please go to \"Project Settings\" and configure your build step's \"Script to Run\"" 120 exit 1 121 fi 122 123 ## Docker 124 if [[ "$BUILDBOX_DOCKER" != "" ]]; then 125 DOCKER_CONTAINER="buildbox_"$BUILDBOX_JOB_ID"_container" 126 DOCKER_IMAGE="buildbox_"$BUILDBOX_JOB_ID"_image" 127 128 function docker-cleanup { 129 docker rm -f $DOCKER_CONTAINER 130 131 # Enabling the following line will prevent your build server from filling up, 132 # but will slow down your builds because it'll be built from scratch each time. 133 # 134 # docker rmi -f $DOCKER_IMAGE 135 } 136 137 trap docker-cleanup EXIT 138 139 # Build the Docker image, namespaced to the job 140 buildbox-run "docker build -t $DOCKER_IMAGE ." 141 142 echo "+++ Running $BUILDBOX_SCRIPT_PATH (in Docker container $DOCKER_IMAGE)" 143 144 # Run the build script command in a one-off container 145 buildbox-prompt-and-run "docker run --name $DOCKER_CONTAINER $DOCKER_IMAGE ./$BUILDBOX_SCRIPT_PATH" 146 147 ## Fig 148 elif [[ "$BUILDBOX_FIG_CONTAINER" != "" ]]; then 149 # Fig strips dashes and underscores, so we'll remove them to match the docker container names 150 FIG_PROJ_NAME="buildbox"${BUILDBOX_JOB_ID//-} 151 # The name of the docker container fig creates when it creates the adhoc run 152 FIG_CONTAINER_NAME=$FIG_PROJ_NAME"_"$BUILDBOX_FIG_CONTAINER 153 154 function fig-cleanup { 155 fig -p $FIG_PROJ_NAME kill 156 fig -p $FIG_PROJ_NAME rm --force 157 158 # The adhoc run container isn't cleaned up by fig, so we have to do it ourselves 159 echo "Killing "$FIG_CONTAINER_NAME"_run_1..." 160 docker rm -f $FIG_CONTAINER_NAME"_run_1" 161 162 # Enabling the following line will prevent your build server from filling up, 163 # but will slow down your builds because it'll be built from scratch each time. 164 # 165 # docker rmi -f $FIG_CONTAINER_NAME 166 } 167 168 trap fig-cleanup EXIT 169 170 # Build the Docker images using Fig, namespaced to the job 171 buildbox-run "fig -p $FIG_PROJ_NAME build" 172 173 echo "+++ Running $BUILDBOX_SCRIPT_PATH (in Fig container '$BUILDBOX_FIG_CONTAINER')" 174 175 # Run the build script command in the service specified in BUILDBOX_FIG_CONTAINER 176 buildbox-prompt-and-run "fig -p $FIG_PROJ_NAME run $BUILDBOX_FIG_CONTAINER ./$BUILDBOX_SCRIPT_PATH" 177 178 ## Standard 179 else 180 echo "+++ Running build script" 181 echo -e "$BUILDBOX_PROMPT ./$BUILDBOX_SCRIPT_PATH" 182 183 ."/$BUILDBOX_SCRIPT_PATH" 184 fi 185 186 # Capture the exit status for the end 187 EXIT_STATUS=$? 188 189 ############################################################## 190 # 191 # ARTIFACTS 192 # Uploads and build artifacts associated with this build 193 # 194 ############################################################## 195 196 if [[ "$BUILDBOX_ARTIFACT_PATHS" != "" ]]; then 197 # If you want to upload artifacts to your own server, uncomment the lines below 198 # and replace the AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY with keys to your 199 # own bucket. 200 # 201 # export AWS_SECRET_ACCESS_KEY=yyy 202 # export AWS_ACCESS_KEY_ID=xxx 203 # export AWS_S3_ACL=private 204 # buildbox-run "buildbox-agent build-artifact upload \"$BUILDBOX_ARTIFACT_PATHS\" \"s3://name-of-your-s3-bucket/$BUILDBOX_JOB_ID\"" 205 206 # Show the output of the artifact uploder when in debug mode 207 if [[ "$BUILDBOX_AGENT_DEBUG" == "true" ]]; then 208 echo '--- Uploading artifacts' 209 buildbox-run "buildbox-agent build-artifact upload \"$BUILDBOX_ARTIFACT_PATHS\"" 210 else 211 buildbox-run "buildbox-agent build-artifact upload \"$BUILDBOX_ARTIFACT_PATHS\" > /dev/null 2>&1" 212 fi 213 fi 214 215 # Be sure to exit this script with the same exit status that the users build 216 # script exited with. 217 exit $EXIT_STATUS