Skip to content

Automated update of regions #6018

Automated update of regions

Automated update of regions #6018

name: Record information on PR and (maybe) Build from fork
on:
pull_request_target:
branches: [main]
types: [labeled,opened,reopened,synchronize]
env:
PLATFORMSH_CLI_NO_INTERACTION: 1
PLATFORM_PROJECT: ${{ vars.PROJECT_ID }}
PLATFORMSH_CLI_DEFAULT_TIMEOUT: 60 # Increase timeout for CLI commands
SLEEP_TIME: 5
NUM_TRIES: 30
BRANCH_TITLE: ${{ vars.BFF_PREFIX }}-${{ github.event.number }}
jobs:
build:
# we only want to run this job if the pull request is from a fork AND it has been given the proper label
if: >-
(
github.event.label.name == 'build-fork' ||
contains(github.event.pull_request.labels.*.name, 'build-fork')
) &&
github.event.pull_request.head.repo.full_name != github.repository
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}
token: ${{ secrets.WORKFLOW_TOKEN }}
- uses: upsun/action-cli@v1
with:
cli_provider: 'platform'
# Create an environment and activate it
- name: 'Build a branch from the fork'
env:
PLATFORMSH_CLI_TOKEN: ${{ secrets.PLATFORMSH_CLI_TOKEN }}
# @see activate_environment:Set environment title:env in manage-environment.yaml
PR_TITLE: ${{ github.event.pull_request.title }}
run: |
echo "Adding changes from forked PR to local branch"
# Get most recent changes
git checkout ${{ github.event.pull_request.head.sha }}
# Put most recent changes on the branch
echo "Switching to branch ${{ env.BRANCH_TITLE }}"
# try to switch to the branch, and if it doesn't exist, create a new branch
git switch ${{ env.BRANCH_TITLE }} 2>/dev/null || git switch -C ${{ env.BRANCH_TITLE }}
echo "Pushing most recent changes"
git push --force origin ${{ env.BRANCH_TITLE }}
# has platform.sh received our new branch?
counter=0
branchReceived=""
echo "::notice::Checking if Platform.sh has received our new branch..."
while (( counter < ${{ env.NUM_TRIES }} )) && [[ -z "${branchReceived}" ]]; do
sleep ${{ env.SLEEP_TIME }}
echo "::notice::Attempt ${counter} of ${{ env.NUM_TRIES }}"
branchReceived=$(platform project:curl /environments | jq --arg branch "${{ env.BRANCH_TITLE }}" -r '.[] | select(.name | contains($branch))');
counter=$((++counter))
done
if [[ -z "${branchReceived}" ]]; then
echo "::warning::It appears that Platform.sh did not receive our branch ${{ env.BRANCH_TITLE }}. Please look at the logs and try again."
exit 24;
else
echo "::notice::Branch ${{ env.BRANCH_TITLE }} was successfully pushed to Platform.sh. Continuing..."
fi
# if this is an existing PR then we've already changed the title to be more descriptive, so we need id as well
# to match the original branch name. In addition, if it is an existing PR, then the environment will be in the
# process of being built and therefore will have a status of 'In progress'
# If environment not active, activate it
if ! $(platform environments --format plain --columns id,title,status | grep '${{ env.BRANCH_TITLE }}' | grep -Eq '(In progress|Active)'); then
echo "::notice::Updating Platform.sh environment title for better tracking..."
# in this case we DO want to statically include `pr-##` since we're referring to a pull request number
newTitle="(pr-${{ github.event.number }}) ${PR_TITLE}"
platform environment:info title "${newTitle}" -e ${{ env.BRANCH_TITLE }}
echo "::notice::Activating environment ${{ env.BRANCH_TITLE }}"
platform environment:activate -e ${{ env.BRANCH_TITLE }}
fi
# records all the info needed about the PR for the downstream workflows
record-pr-info:
runs-on: ubuntu-latest
needs:
- build
if: ${{ always() }}
steps:
- name: "Save PR info for downstream workflows"
if: ${{ always() }}
run: |
touch pr_number.txt pr_head_sha.txt pr_base_sha.txt branch.txt continuetests.txt forkorsource.txt
# set default
echo "false" > continuetests.txt
echo "${{ github.event.number }}" > pr_number.txt
echo "${{ github.event.pull_request.head.sha }}" > pr_head_sha.txt
echo "${{ github.event.pull_request.base.sha }}" > pr_base_sha.txt
- name: "Get branch name"
id: branch_name
env:
# since this is user supplied, force the runner to make it an env so it escapes characters
BRANCH_NAME: ${{ github.event.pull_request.head.ref }}
run: |
# For PRs from non-forked repos
if [ ${{ github.event.pull_request.head.repo.id }} == ${{ vars.REPO_ID }} ]; then
branch_name="${{ env.BRANCH_NAME }}"
# For PRs from forks
else
branch_name="${{ env.BRANCH_TITLE }}"
fi
echo "::notice::Setting branch name to ${branch_name}."
echo "${branch_name}" > branch.txt
- name: "PR is from source repo or from fork but properly labeled"
if: github.event.pull_request.head.repo.id == vars.REPO_ID || contains(github.event.pull_request.labels.*.name, 'build-fork')
run: |
echo "true" > continuetests.txt
- name: "We are building from the source"
if: github.event.pull_request.head.repo.id == vars.REPO_ID
run:
echo "source" > forkorsource.txt
- name: "We are building from a fork"
if: github.event.pull_request.head.repo.id != vars.REPO_ID && contains(github.event.pull_request.labels.*.name, 'build-fork')
run: |
echo "fork" > forkorsource.txt
- name: "Save PR info"
uses: actions/upload-artifact@v4
with:
name: pr-info
# @todo is there a better way to do this?
path: |
pr_number.txt
pr_head_sha.txt
pr_base_sha.txt
branch.txt
continuetests.txt
forkorsource.txt
retention-days: 1