From fddc37504527337a173487a0474862828f79a856 Mon Sep 17 00:00:00 2001 From: Chris Abraham Date: Tue, 21 May 2024 15:13:21 +0700 Subject: [PATCH] pull in set-environment script Signed-off-by: Chris Abraham --- .github/workflows/build_test_and_deploy.yml | 19 +-- .github/workflows/set-environment | 153 ++++++++++++++++++++ 2 files changed, 158 insertions(+), 14 deletions(-) create mode 100644 .github/workflows/set-environment diff --git a/.github/workflows/build_test_and_deploy.yml b/.github/workflows/build_test_and_deploy.yml index c637e14b..d2ff03da 100644 --- a/.github/workflows/build_test_and_deploy.yml +++ b/.github/workflows/build_test_and_deploy.yml @@ -49,10 +49,8 @@ jobs: # https://github.com/pantheon-systems/docker-build-tools-ci/blob/6.x/scripts/set-environment - name: setup-environment-vars run: | - echo '1' $GITHUB_REF if [[ "$GITHUB_REF" =~ ^refs/pull/([0-9]+)/merge$ ]]; then export PR_NUMBER=${BASH_REMATCH[1]} - echo '2' $PR_NUMBER export CI_BRANCH=`curl -u "${GITHUB_OWNER}:${GITHUB_TOKEN}" \ -H "Accept: application/vnd.github.groot-preview+json" \ @@ -65,7 +63,6 @@ jobs: python3 -c "import sys, json; print(json.load(sys.stdin)['html_url'])"` elif [ "$GITHUB_REF" != "refs/heads/main" ]; then - echo '3' export PR_NUMBER=`curl -u "${GITHUB_OWNER}:${GITHUB_TOKEN}" \ -H "Accept: application/vnd.github.groot-preview+json" \ "https://api.github.com/repos/${CI_PROJECT_NAME}/commits/${COMMIT_SHA}/pulls" | \ @@ -81,18 +78,12 @@ jobs: "https://api.github.com/repos/${CI_PROJECT_NAME}/commits/${COMMIT_SHA}/pulls" | \ python3 -c "import sys, json; print(json.load(sys.stdin)[0]['html_url'])"` else - echo '4' export CI_BRANCH="main" fi - echo '5' export CI_PROJECT_REPONAME="${CI_PROJECT_NAME#*/}" export CI_PROJECT_USERNAME=$GITHUB_REPOSITORY_OWNER - /build-tools-ci/scripts/set-environment - GITHUB_WORKFLOW_URL=https://github.com/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID - echo "export CI_BUILD_URL='${GITHUB_WORKFLOW_URL}'" >> $BASH_ENV - echo "export CI_NODE_INDEX=0" >> $BASH_ENV - echo "export CI_REPOSITORY_URL='https://github.com/${GITHUB_REPOSITORY}'" >> $BASH_ENV - echo "export ARTIFACTS_DIR_URL='${GITHUB_WORKFLOW_URL}/#artifacts'" >> $BASH_ENV + export GITHUB_WORKFLOW_URL=https://github.com/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID + ./.github/workflows/set-environment build_php: container: @@ -130,7 +121,7 @@ jobs: # Set TERMINUS_ENV and related environment variables. # https://github.com/pantheon-systems/docker-build-tools-ci/blob/6.x/scripts/set-environment - name: setup-environment-vars - run: /build-tools-ci/scripts/set-environment + run: ./.github/workflows/set-environment - name: run PHP build step run: composer -n build-assets @@ -198,7 +189,7 @@ jobs: # Set TERMINUS_ENV and related environment variables. # https://github.com/pantheon-systems/docker-build-tools-ci/blob/6.x/scripts/set-environment - name: setup-environment-vars - run: /build-tools-ci/scripts/set-environment + run: ./.github/workflows/set-environment # Rsync the build artifact pieces web directory - name: sync build artifact @@ -268,7 +259,7 @@ jobs: # Set TERMINUS_ENV and related environment variables. # https://github.com/pantheon-systems/docker-build-tools-ci/blob/6.x/scripts/set-environment - name: setup-environment-vars - run: /build-tools-ci/scripts/set-environment + run: ./.github/workflows/set-environment # Rsync the build artifact pieces web directory - name: sync build artifact diff --git a/.github/workflows/set-environment b/.github/workflows/set-environment new file mode 100644 index 00000000..7239a0a2 --- /dev/null +++ b/.github/workflows/set-environment @@ -0,0 +1,153 @@ +#!/bin/bash +set -eo pipefail + +# +# Before calling this script, set the following environent variables: +# +# - CI_BRANCH: the branch being tested +# - CI_BUILD_NUMBER: monotonically increasing build counter +# - PR_NUMBER: pull request number (if job is from a pull request) +# +# Optionally: +# +# - CI_PULL_REQUEST: URL to the current pull request; used to set PR_NUMBER +# - DEFAULT_SITE: name of the repository; used to set TERMINUS_SITE +# +# Note that any environment variable given above is not set, then +# it will be assigned its value from the corresponding CircleCI +# environment variable. +# +CI_BRANCH=${CI_BRANCH:-$CIRCLE_BRANCH} +CI_BUILD_NUMBER=${CI_BUILD_NUMBER:-$CIRCLE_BUILD_NUM} +CI_PROJECT_NAME=${CI_PROJECT_NAME:-$CIRCLE_PROJECT_REPONAME} +CI_PULL_REQUEST=${CI_PULL_REQUEST:-$CIRCLE_PULL_REQUEST} + +# Circle sets both $CIRCLE_PULL_REQUEST and $CI_PULL_REQUEST. +PR_NUMBER=${PR_NUMBER:-$CI_PULL_REQUEST} +PR_NUMBER=${PR_NUMBER##*/} + +# Set up BASH_ENV if it was not set for us. +BASH_ENV=${BASH_ENV:-$HOME/.bashrc} + +# Provide a default email address +GIT_EMAIL=${GIT_EMAIL:-ci-bot@pantheon.io} + +# We will also set the default site name to be the same as the repository name. +DEFAULT_SITE=${DEFAULT_SITE:-$CI_PROJECT_NAME} + +# By default, we will make the main branch master. +DEFAULT_BRANCH=${DEFAULT_BRANCH:-master} + +# If we are on the default branch. +if [[ ${CI_BRANCH} == ${DEFAULT_BRANCH} ]] ; then + # Use dev as the environment. + DEFAULT_ENV=${DEFAULT_ENV:-dev} +else + # Otherwise, name the environment after the CI build number. + DEFAULT_ENV=ci-$CI_BUILD_NUMBER +fi + +# If there is a PR number provided, though, then we will use it instead. +if [[ -n ${PR_NUMBER} ]] ; then + DEFAULT_ENV="pr-${PR_NUMBER}" +fi + +CI_PR_URL=${CI_PR_URL:-$CIRCLE_PULL_REQUEST} +CI_PROJECT_USERNAME=${CI_PROJECT_USERNAME:-$CIRCLE_PROJECT_USERNAME} +CI_PROJECT_REPONAME=${CI_PROJECT_REPONAME:-$CIRCLE_PROJECT_REPONAME} +TERMINUS_SITE=${TERMINUS_SITE:-$DEFAULT_SITE} +TERMINUS_ENV=${TERMINUS_ENV:-$DEFAULT_ENV} + +# If any Pantheon environments are locked, users may provide a URL-encoded +# 'username:password' string in an environment variable to include the +# HTTP Basic Authentication. +MULTIDEV_SITE_BASIC_AUTH=${MULTIDEV_SITE_BASIC_AUTH:-$SITE_BASIC_AUTH} +DEV_SITE_BASIC_AUTH=${DEV_SITE_BASIC_AUTH:-$SITE_BASIC_AUTH} +TEST_SITE_BASIC_AUTH=${TEST_SITE_BASIC_AUTH:-$SITE_BASIC_AUTH} +LIVE_SITE_BASIC_AUTH=${LIVE_SITE_BASIC_AUTH:-$SITE_BASIC_AUTH} + +# Prepare the Basic Authentication strings, appending the "@" sign if not empty. +MULTIDEV_SITE_BASIC_AUTH=${MULTIDEV_SITE_BASIC_AUTH:+"$MULTIDEV_SITE_BASIC_AUTH@"} +DEV_SITE_BASIC_AUTH=${DEV_SITE_BASIC_AUTH:+"$DEV_SITE_BASIC_AUTH@"} +TEST_SITE_BASIC_AUTH=${TEST_SITE_BASIC_AUTH:+"$TEST_SITE_BASIC_AUTH@"} +LIVE_SITE_BASIC_AUTH=${LIVE_SITE_BASIC_AUTH:+"$LIVE_SITE_BASIC_AUTH@"} + +#===================================================================================================================== +# EXPORT needed environment variables +# +# Circle CI 2.0 does not yet expand environment variables so they have to be manually EXPORTed +# Once environment variables can be expanded this section can be removed +# See: https://discuss.circleci.com/t/unclear-how-to-work-with-user-variables-circleci-provided-env-variables/12810/11 +# See: https://discuss.circleci.com/t/environment-variable-expansion-in-working-directory/11322 +# See: https://discuss.circleci.com/t/circle-2-0-global-environment-variables/8681 +# Bitbucket has similar issues: +# https://bitbucket.org/site/master/issues/18262/feature-request-pipeline-command-to-modify +#===================================================================================================================== +( + echo 'export PATH=$PATH:$HOME/bin' + echo "export PR_NUMBER=$PR_NUMBER" + echo "export CI_BRANCH=$(echo $CI_BRANCH | grep -v '"'^\(master\|[0-9]\+.x\)$'"')" + echo "export CI_BUILD_NUMBER=$CI_BUILD_NUMBER" + echo "export DEFAULT_SITE='$DEFAULT_SITE'" + echo "export CI_PR_URL='$CI_PR_URL'" + echo "export CI_PROJECT_USERNAME='$CI_PROJECT_USERNAME'" + echo "export CI_PROJECT_REPONAME='$CI_PROJECT_REPONAME'" + echo "export DEFAULT_ENV='$DEFAULT_ENV'" + echo 'export TERMINUS_HIDE_UPDATE_MESSAGE=1' + echo "export TERMINUS_SITE='$TERMINUS_SITE'" + echo "export TERMINUS_ENV='$TERMINUS_ENV'" + echo "export DEFAULT_BRANCH='$DEFAULT_BRANCH'" + echo "export MULTIDEV_SITE_URL='https://${MULTIDEV_SITE_BASIC_AUTH}$TERMINUS_ENV-$TERMINUS_SITE.pantheonsite.io/'" + echo "export DEV_SITE_URL='https://${DEV_SITE_BASIC_AUTH}dev-$TERMINUS_SITE.pantheonsite.io/'" + echo "export TEST_SITE_URL='https://${TEST_SITE_BASIC_AUTH}test-$TERMINUS_SITE.pantheonsite.io/'" + echo "export LIVE_SITE_URL='https://${LIVE_SITE_BASIC_AUTH}live-$TERMINUS_SITE.pantheonsite.io/'" + echo "export ARTIFACTS_DIR='artifacts'" + echo "export ARTIFACTS_FULL_DIR='/tmp/artifacts'" + echo "export CI_BUILD_URL='${GITHUB_WORKFLOW_URL}'" + echo "export CI_NODE_INDEX=0" + echo "export CI_REPOSITORY_URL='https://github.com/${GITHUB_REPOSITORY}'" + echo "export ARTIFACTS_DIR_URL='${GITHUB_WORKFLOW_URL}/#artifacts'" +) >> $BASH_ENV + +# If a Terminus machine token and site name are defined +if [[ -n "$TERMINUS_MACHINE_TOKEN" && -n "$TERMINUS_SITE" ]] +then + + # Authenticate with Terminus + terminus -n auth:login --machine-token="$TERMINUS_MACHINE_TOKEN" > /dev/null 2>&1 + + # Use Terminus to fetch variables + TERMINUS_SITE_UUID=$(terminus site:info $TERMINUS_SITE --field=id) + + # And add those variables to $BASH_ENV + ( + echo "export TERMINUS_SITE_UUID='$TERMINUS_SITE_UUID'" + ) >> $BASH_ENV +fi + +source $BASH_ENV + +echo 'Contents of BASH_ENV:' +cat $BASH_ENV +echo + +# Avoid ssh prompting when connecting to new ssh hosts +mkdir -p $HOME/.ssh && echo "StrictHostKeyChecking no" >> "$HOME/.ssh/config" + +# Configure the GitHub Oauth token if it is available +if [ -n "$GITHUB_TOKEN" ]; then + composer -n config --global github-oauth.github.com $GITHUB_TOKEN +fi + +# Set up our default git config settings if git is available. +git config --global user.email "${GIT_EMAIL:-no-reply+ci-$CI_BUILD_NUMBER@getpantheon.com}" +git config --global user.name "CI Bot" +git config --global core.fileMode false + +# Re-install the Terminus Build Tools plugin if requested +if [ -n $BUILD_TOOLS_VERSION ] && [ "$BUILD_TOOLS_VERSION" <> 'dev-master' ]; then + echo "Install Terminus Build Tools Plugin version $BUILD_TOOLS_VERSION." + echo "Note that it is NOT RECOMMENDED to define BUILD_TOOLS_VERSION, save in the Terminus Build Tools plugin tests themselves. All other tests should use the version bundled with the container." + rm -rf ${TERMINUS_PLUGINS_DIR:-~/.terminus/plugins}/terminus-build-tools-plugin + composer -n create-project --no-dev -d ${TERMINUS_PLUGINS_DIR:-~/.terminus/plugins} pantheon-systems/terminus-build-tools-plugin:$BUILD_TOOLS_VERSION +fi \ No newline at end of file