github.com/discordapp/buildkite-agent@v2.6.6+incompatible/templates/0.2/bootstrap.sh (about) 1 #!/bin/bash 2 3 BUILDBOX_PROMPT="\033[90m$\033[0m" 4 5 function buildbox-prompt-and-run { 6 echo -e "$BUILDBOX_PROMPT $1" 7 eval $1 8 } 9 10 function buildbox-run { 11 echo -e "$BUILDBOX_PROMPT $1" 12 13 eval $1 14 EVAL_EXIT_STATUS=$? 15 16 if [ $EVAL_EXIT_STATUS -ne 0 ] 17 then 18 exit $EVAL_EXIT_STATUS 19 fi 20 } 21 22 if [ "$BUILDBOX_BIN_DIR" != "" ] 23 then 24 echo "Deprecation warning: BUILDBOX_BIN_DIR has been renamed to BUILDBOX_BIN_PATH" 25 BUILDBOX_BIN_PATH=$BUILDBOX_BIN_DIR 26 fi 27 28 if [ "$BUILDBOX_DIR" != "" ] 29 then 30 echo "Deprecation warning: BUILDBOX_DIR has been renamed to BUILDBOX_PATH" 31 BUILDBOX_PATH=$BUILDBOX_DIR 32 fi 33 34 echo '--- setup environment' 35 36 # Provide a default BUILDBOX_PATH 37 if [ -z "$BUILDBOX_PATH" ]; then 38 # This will return the location of this file. We assume that the buildbox-artifact 39 # tool is in the same folder. You can of course customize the locations 40 # and edit this file. 41 BUILDBOX_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 42 fi 43 44 # If no BUILDBOX_BIN_PATH has been provided, make one up 45 if [ -z "$BUILDBOX_BIN_PATH" ]; then 46 if [ -d "$BUILDBOX_PATH/bin" ]; then 47 BUILDBOX_BIN_PATH="$BUILDBOX_PATH/bin" 48 else 49 BUILDBOX_BIN_PATH="$BUILDBOX_PATH" 50 fi 51 fi 52 53 # Add the $BUILDBOX_BIN to the $PATH 54 export PATH="$BUILDBOX_BIN_PATH:$PATH" 55 56 # Create the build directory 57 SANITIZED_AGENT_NAME=$(echo $BUILDBOX_AGENT_NAME | tr -d '"') 58 PROJECT_FOLDER_NAME="$SANITIZED_AGENT_NAME/$BUILDBOX_PROJECT_SLUG" 59 60 # Allow overiding the location that builds get run in 61 if [ -z "$BUILDBOX_BUILD_PATH" ]; then 62 BUILDBOX_PROJECT_PATH="$BUILDBOX_PATH/builds/$PROJECT_FOLDER_NAME" 63 else 64 BUILDBOX_PROJECT_PATH="$BUILDBOX_BUILD_PATH/$PROJECT_FOLDER_NAME" 65 fi 66 67 # Show the ENV variables if DEBUG is on 68 if [ "$BUILDBOX_AGENT_DEBUG" == "true" ] 69 then 70 buildbox-run "env | grep BUILDBOX" 71 fi 72 73 buildbox-run "mkdir -p \"$BUILDBOX_PROJECT_PATH\"" 74 buildbox-run "cd \"$BUILDBOX_PROJECT_PATH\"" 75 76 # Do we need to do a git checkout? 77 if [ ! -d ".git" ] 78 then 79 # If it's a first time SSH git clone it will prompt to accept the host's 80 # fingerprint. To avoid this add the host's key to ~/.ssh/known_hosts ahead 81 # of time: 82 # ssh-keyscan -H host.com >> ~/.ssh/known_hosts 83 buildbox-run "git clone "$BUILDBOX_REPO" . -qv" 84 fi 85 86 # Default empty branch names 87 if [ "$BUILDBOX_BRANCH" == "" ] 88 then 89 BUILDBOX_BRANCH="master" 90 fi 91 92 buildbox-run "git clean -fdq" 93 buildbox-run "git fetch -q" 94 95 # Only reset to the branch if we're not on a tag 96 if [ "$BUILDBOX_TAG" == "" ] 97 then 98 buildbox-run "git reset --hard origin/$BUILDBOX_BRANCH" 99 fi 100 101 buildbox-run "git checkout -qf \"$BUILDBOX_COMMIT\"" 102 103 if [ "$BUILDBOX_SCRIPT_PATH" == "" ] 104 then 105 echo "ERROR: No script to run. Please go to \"Project Settings\" and configure your build step's \"Script to Run\"" 106 exit 1 107 fi 108 109 ## Docker 110 if [ "$BUILDBOX_DOCKER" != "" ] 111 then 112 DOCKER_CONTAINER="buildbox_"$BUILDBOX_JOB_ID"_container" 113 DOCKER_IMAGE="buildbox_"$BUILDBOX_JOB_ID"_image" 114 115 function docker-cleanup { 116 docker rm -f $DOCKER_CONTAINER 117 118 # Enabling the following line will prevent your build server from filling up, 119 # but will slow down your builds because it'll be built from scratch each time. 120 # 121 # docker rmi -f $DOCKER_IMAGE 122 } 123 124 trap docker-cleanup EXIT 125 126 # Build the Docker image, namespaced to the job 127 buildbox-run "docker build -t $DOCKER_IMAGE ." 128 129 echo "--- running $BUILDBOX_SCRIPT_PATH (in Docker container $DOCKER_IMAGE)" 130 131 # Run the build script command in a one-off container 132 buildbox-prompt-and-run "docker run --name $DOCKER_CONTAINER $DOCKER_IMAGE ./$BUILDBOX_SCRIPT_PATH" 133 134 ## Fig 135 elif [ "$BUILDBOX_FIG_CONTAINER" != "" ] 136 then 137 # Fig strips dashes and underscores, so we'll remove them to match the docker container names 138 FIG_PROJ_NAME="buildbox"${BUILDBOX_JOB_ID//-} 139 # The name of the docker container fig creates when it creates the adhoc run 140 FIG_CONTAINER_NAME=$FIG_PROJ_NAME"_"$BUILDBOX_FIG_CONTAINER 141 142 function fig-cleanup { 143 fig -p $FIG_PROJ_NAME kill 144 fig -p $FIG_PROJ_NAME rm --force 145 146 # The adhoc run container isn't cleaned up by fig, so we have to do it ourselves 147 echo "Killing "$FIG_CONTAINER_NAME"_run_1..." 148 docker rm -f $FIG_CONTAINER_NAME"_run_1" 149 150 # Enabling the following line will prevent your build server from filling up, 151 # but will slow down your builds because it'll be built from scratch each time. 152 # 153 # docker rmi -f $FIG_CONTAINER_NAME 154 } 155 156 trap fig-cleanup EXIT 157 158 # Build the Docker images using Fig, namespaced to the job 159 buildbox-run "fig -p $FIG_PROJ_NAME build" 160 161 echo "--- running $BUILDBOX_SCRIPT_PATH (in Fig container '$BUILDBOX_FIG_CONTAINER')" 162 163 # Run the build script command in the service specified in BUILDBOX_FIG_CONTAINER 164 buildbox-prompt-and-run "fig -p $FIG_PROJ_NAME run $BUILDBOX_FIG_CONTAINER ./$BUILDBOX_SCRIPT_PATH" 165 166 ## Standard 167 else 168 echo "--- running $BUILDBOX_SCRIPT_PATH" 169 170 # Run the step's build script 171 ."/$BUILDBOX_SCRIPT_PATH" 172 fi 173 174 # Capture the exit status for the end 175 EXIT_STATUS=$? 176 177 if [ "$BUILDBOX_ARTIFACT_PATHS" != "" ] 178 then 179 # NOTE: In agent version 1.0 and above, the location and the name of the 180 # buildbox artifact binary changed. As of this verison, builbdox-artifact has 181 # been rolled into buildbox-agent. 182 if buildbox-agent --help | grep build-artifact 183 then 184 # If you want to upload artifacts to your own server, uncomment the lines below 185 # and replace the AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY with keys to your 186 # own bucket. 187 # 188 # export AWS_SECRET_ACCESS_KEY=yyy 189 # export AWS_ACCESS_KEY_ID=xxx 190 # export AWS_S3_ACL=private 191 # buildbox-run "buildbox-agent build-artifact upload \"$BUILDBOX_ARTIFACT_PATHS\" \"s3://name-of-your-s3-bucket/$BUILDBOX_JOB_ID\"" 192 193 # Show the output of the artifact uploder when in debug mode 194 if [ "$BUILDBOX_AGENT_DEBUG" == "true" ] 195 then 196 echo '--- uploading artifacts' 197 buildbox-run "buildbox-agent build-artifact upload \"$BUILDBOX_ARTIFACT_PATHS\"" 198 else 199 buildbox-run "buildbox-agent build-artifact upload \"$BUILDBOX_ARTIFACT_PATHS\" > /dev/null 2>&1" 200 fi 201 elif [[ -e $BUILDBOX_PATH/buildbox-artifact ]] 202 then 203 # If you want to upload artifacts to your own server, uncomment the lines below 204 # and replace the AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY with keys to your 205 # own bucket. 206 # export AWS_SECRET_ACCESS_KEY=yyy 207 # export AWS_ACCESS_KEY_ID=xxx 208 # buildbox-run "buildbox-artifact upload \"$BUILDBOX_ARTIFACT_PATHS\" "s3://name-of-your-s3-bucket/$BUILDBOX_JOB_ID" --url $BUILDBOX_AGENT_API_URL > /dev/null 2>&1" 209 210 # By default we silence the buildbox-artifact build output. However, if you'd like to see 211 # it in your logs, remove the: > /dev/null 2>&1 from the end of the line. 212 buildbox-run "buildbox-artifact upload \"$BUILDBOX_ARTIFACT_PATHS\" --url $BUILDBOX_AGENT_API_URL > /dev/null 2>&1" 213 else 214 echo >&2 "ERROR: buildbox-artifact could not be found in $BUILDBOX_PATH" 215 exit 1 216 fi 217 fi 218 219 exit $EXIT_STATUS