2019-04-19 00:32:19 +02:00
|
|
|
#!/bin/bash
|
|
|
|
# This is an entrypoint for our Docker image that does some minimal bootstrapping before executing.
|
|
|
|
|
|
|
|
set -e
|
|
|
|
|
2020-03-31 03:25:41 +02:00
|
|
|
# The default backend URL is the Pulumi Managed Service.
|
|
|
|
# To use one of the alternate supported backends, set the
|
|
|
|
# PULUMI_BACKEND_URL env var according to:
|
|
|
|
# https://www.pulumi.com/docs/intro/concepts/state/#to-a-self-managed-backend.
|
|
|
|
pulumi login $PULUMI_BACKEND_URL
|
|
|
|
|
2019-04-19 00:32:19 +02:00
|
|
|
# If the PULUMI_CI variable is set, we'll do some extra things to make common tasks easier.
|
|
|
|
if [ ! -z "$PULUMI_CI" ]; then
|
|
|
|
# Capture the PWD before we go and potentially change it.
|
|
|
|
ROOT=$(pwd)
|
|
|
|
|
|
|
|
# If the root of the Pulumi project isn't the root of the repo, CD into it.
|
|
|
|
if [ ! -z "$PULUMI_ROOT" ]; then
|
|
|
|
cd $PULUMI_ROOT
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Detect the CI system and configure variables so that we get good Pulumi workflow and GitHub App support.
|
|
|
|
if [ ! -z "$GITHUB_WORKFLOW" ]; then
|
|
|
|
export PULUMI_CI_SYSTEM="GitHub"
|
|
|
|
export PULUMI_CI_BUILD_ID=
|
|
|
|
export PULUMI_CI_BUILD_TYPE=
|
|
|
|
export PULUMI_CI_BUILD_URL=
|
|
|
|
export PULUMI_CI_PULL_REQUEST_SHA="$GITHUB_SHA"
|
|
|
|
|
|
|
|
# For PR events, we want to take the ref of the target branch, not the current. This ensures, for
|
|
|
|
# instance, that a PR for a topic branch merging into `master` will use the `master` branch as the
|
|
|
|
# target for a preview. Note that for push events, we of course want to use the actual branch.
|
|
|
|
if [ "$PULUMI_CI" = "pr" ]; then
|
|
|
|
# Not all PR events warrant running a preview. Many of them pertain to changes in assignments and
|
|
|
|
# ownership, but we only want to run the preview if the action is "opened", "edited", or "synchronize".
|
|
|
|
PR_ACTION=$(jq -r ".action" < $GITHUB_EVENT_PATH)
|
|
|
|
if [ "$PR_ACTION" != "opened" ] && [ "$PR_ACTION" != "edited" ] && [ "$PR_ACTION" != "synchronize" ]; then
|
|
|
|
echo -e "PR event ($PR_ACTION) contains no changes and does not warrant a Pulumi Preview"
|
|
|
|
echo -e "Skipping Pulumi action altogether..."
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
BRANCH=$(jq -r ".pull_request.base.ref" < $GITHUB_EVENT_PATH)
|
|
|
|
else
|
|
|
|
BRANCH="$GITHUB_REF"
|
|
|
|
fi
|
|
|
|
BRANCH=$(echo $BRANCH | sed "s/refs\/heads\///g")
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Respect the branch mappings file for stack selection. Note that this is *not* required, but if the file
|
|
|
|
# is missing, the caller of this script will need to pass `-s <stack-name>` to specify the stack explicitly.
|
|
|
|
if [ ! -z "$BRANCH" ]; then
|
2020-11-07 19:08:01 +01:00
|
|
|
if [ -z "$PULUMI_STACK_NAME" ]; then
|
|
|
|
if [ -e $ROOT/.pulumi/ci.json ]; then
|
|
|
|
PULUMI_STACK_NAME=$(cat $ROOT/.pulumi/ci.json | jq -r ".\"$BRANCH\"")
|
|
|
|
else
|
|
|
|
# If there's no stack mapping file, we are on master, and there's a single stack, use it.
|
|
|
|
PULUMI_STACK_NAME=$(pulumi stack ls | awk 'FNR == 2 {print $1}' | sed 's/\*//g')
|
|
|
|
fi
|
2019-04-19 00:32:19 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ ! -z "$PULUMI_STACK_NAME" ] && [ "$PULUMI_STACK_NAME" != "null" ]; then
|
|
|
|
pulumi stack select $PULUMI_STACK_NAME
|
|
|
|
else
|
|
|
|
echo -e "No stack configured for branch '$BRANCH'"
|
|
|
|
echo -e ""
|
|
|
|
echo -e "To configure this branch, please"
|
|
|
|
echo -e "\t1) Run 'pulumi stack init <stack-name>'"
|
|
|
|
echo -e "\t2) Associated the stack with the branch by adding"
|
|
|
|
echo -e "\t\t{"
|
|
|
|
echo -e "\t\t\t\"$BRANCH\": \"<stack-name>\""
|
|
|
|
echo -e "\t\t}"
|
|
|
|
echo -e "\tto your .pulumi/ci.json file"
|
|
|
|
echo -e ""
|
|
|
|
echo -e "For now, exiting cleanly without doing anything..."
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
# For Google, we need to authenticate with a service principal for certain authentication operations.
|
|
|
|
if [ ! -z "$GOOGLE_CREDENTIALS" ]; then
|
2019-10-23 22:38:00 +02:00
|
|
|
export GOOGLE_APPLICATION_CREDENTIALS="$(mktemp).json"
|
2020-07-14 14:04:01 +02:00
|
|
|
# Check if GOOGLE_CREDENTIALS is base64 encoded
|
2020-06-08 18:36:28 +02:00
|
|
|
if [[ $GOOGLE_CREDENTIALS =~ ^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$ ]]; then
|
|
|
|
echo "$GOOGLE_CREDENTIALS"|base64 -d > $GOOGLE_APPLICATION_CREDENTIALS
|
2020-07-07 17:35:17 +02:00
|
|
|
# unset for other gcloud commands using this variable.
|
|
|
|
unset GOOGLE_CREDENTIALS
|
2020-06-08 18:36:28 +02:00
|
|
|
else
|
|
|
|
echo "$GOOGLE_CREDENTIALS" > $GOOGLE_APPLICATION_CREDENTIALS
|
|
|
|
fi
|
2019-10-23 22:38:00 +02:00
|
|
|
gcloud auth activate-service-account --key-file=$GOOGLE_APPLICATION_CREDENTIALS
|
2020-08-19 09:29:22 +02:00
|
|
|
gcloud --quiet auth configure-docker $GOOGLE_DOCKER_HOSTNAME_LIST
|
2019-04-19 00:32:19 +02:00
|
|
|
fi
|
|
|
|
|
2019-10-22 17:11:37 +02:00
|
|
|
# Next, run npm install. We always call this, as
|
|
|
|
# previous calls (stack select, login, preview) will run the
|
|
|
|
# npm scripts that install plugins; plugin installation directory
|
|
|
|
# is outside of the persisted filesystem (GitHub Actions).
|
|
|
|
# So we need to run this everytime to ensure the plugins are
|
|
|
|
# always available. This shouldn't cause a performance hit,
|
|
|
|
# as the node_modules/lock file will be persisted across runs.
|
|
|
|
#
|
|
|
|
# Similarly, run yarn install as applicable for the same reasons.
|
|
|
|
if [ -e package.json ]; then
|
|
|
|
if [ -f yarn.lock ] || [ ! -z $USE_YARN ]; then
|
|
|
|
yarn install
|
|
|
|
else
|
2020-01-06 17:23:15 +01:00
|
|
|
# Set npm auth token if one is provided.
|
|
|
|
if [ ! -z "$NPM_AUTH_TOKEN" ]; then
|
|
|
|
echo "//registry.npmjs.org/:_authToken=$NPM_AUTH_TOKEN" > ~/.npmrc
|
|
|
|
fi
|
2020-05-25 15:36:20 +02:00
|
|
|
if [ -f package-lock.json ] || [ -f npm-shrinkwrap.json ]; then
|
2020-05-13 20:32:36 +02:00
|
|
|
npm ci
|
|
|
|
else
|
|
|
|
npm install
|
|
|
|
fi
|
2019-10-22 17:11:37 +02:00
|
|
|
fi
|
2019-04-19 00:32:19 +02:00
|
|
|
fi
|
|
|
|
|
2019-11-25 17:37:33 +01:00
|
|
|
# If the user is running the Python SDK, we will need to install their requirements as well.
|
|
|
|
if [ -e requirements.txt ]; then
|
2020-07-14 14:04:01 +02:00
|
|
|
# Check if should use venv
|
|
|
|
PULUMI_VENV=$(cat Pulumi.yaml | grep "virtualenv:" | cut -d':' -f2)
|
|
|
|
if [ -z $PULUMI_VENV ]; then
|
2020-07-22 22:07:26 +02:00
|
|
|
pip3 install -r requirements.txt
|
|
|
|
else
|
2020-07-14 14:04:01 +02:00
|
|
|
python3 -m venv $PULUMI_VENV
|
2020-08-29 01:08:32 +02:00
|
|
|
$PULUMI_VENV/bin/python -m pip install --upgrade pip setuptools wheel
|
|
|
|
$PULUMI_VENV/bin/python -m pip install -r requirements.txt
|
2020-07-14 14:04:01 +02:00
|
|
|
fi
|
2019-11-25 17:37:33 +01:00
|
|
|
fi
|
|
|
|
|
2019-08-06 20:57:56 +02:00
|
|
|
# Now just pass along all arguments to the Pulumi CLI, sending the output to a file for
|
|
|
|
# later use. Note that we exit immediately on failure (under set -e), so we `tee` stdout, but
|
|
|
|
# allow errors to be surfaced in the Actions log.
|
|
|
|
PULUMI_COMMAND="pulumi $*"
|
2019-08-07 01:33:51 +02:00
|
|
|
OUTPUT_FILE=$(mktemp)
|
2019-08-06 20:57:56 +02:00
|
|
|
echo "#### :tropical_drink: \`$PULUMI_COMMAND\`"
|
|
|
|
bash -c "$PULUMI_COMMAND" | tee $OUTPUT_FILE
|
2019-08-06 22:58:28 +02:00
|
|
|
EXIT_CODE=${PIPESTATUS[0]}
|
2019-04-19 00:32:19 +02:00
|
|
|
|
|
|
|
# If the GitHub action stems from a Pull Request event, we may optionally leave a comment if the
|
|
|
|
# COMMENT_ON_PR is set.
|
|
|
|
COMMENTS_URL=$(cat $GITHUB_EVENT_PATH | jq -r .pull_request.comments_url)
|
|
|
|
if [ ! -z $COMMENTS_URL ] && [ ! -z $COMMENT_ON_PR ]; then
|
|
|
|
if [ -z $GITHUB_TOKEN ]; then
|
|
|
|
echo "ERROR: COMMENT_ON_PR was set, but GITHUB_TOKEN is not set."
|
|
|
|
else
|
2019-08-06 20:57:56 +02:00
|
|
|
COMMENT="#### :tropical_drink: \`$PULUMI_COMMAND\`
|
2019-04-19 00:32:19 +02:00
|
|
|
\`\`\`
|
2019-08-06 20:57:56 +02:00
|
|
|
$(cat $OUTPUT_FILE)
|
2019-04-19 00:32:19 +02:00
|
|
|
\`\`\`"
|
|
|
|
PAYLOAD=$(echo '{}' | jq --arg body "$COMMENT" '.body = $body')
|
|
|
|
echo "Commenting on PR $COMMENTS_URL"
|
|
|
|
curl -s -S -H "Authorization: token $GITHUB_TOKEN" -H "Content-Type: application/json" --data "$PAYLOAD" "$COMMENTS_URL"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
exit $EXIT_CODE
|