Get PR URL, start dependent jobs #1724
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Get PR URL, start dependent jobs | |
#on: | |
# pull_request_target: | |
# branches: [main] | |
# types: [labeled,opened,reopened,synchronize] | |
on: | |
workflow_run: | |
workflows: [Record information on PR and (maybe) Build from fork] | |
types: | |
- completed | |
env: | |
PLATFORMSH_CLI_NO_INTERACTION: 1 | |
PLATFORM_PROJECT: ${{ vars.PROJECT_ID }} | |
PLATFORMSH_CLI_DEFAULT_TIMEOUT: 60 # Increase timeout for CLI commands | |
UPSUN_DOCS_PREFIX: "https://docs.upsun.com" | |
PRURLJOBNAME: 'Get PR URL' | |
jobs: | |
get_info_on_pr: | |
runs-on: ubuntu-latest | |
name: "Retrieves information on the PR" | |
outputs: | |
prnum: ${{ steps.set_pr_number.outputs.prnum }} | |
headsha: ${{ steps.set_pr_head_sha.outputs.headsha }} | |
basesha: ${{ steps.set_pr_base_sha.outputs.basesha }} | |
prbranch: ${{ steps.set_pr_branch.outputs.branch }} | |
continuetests: ${{ steps.set_continue_tests.outputs.continue }} | |
reposource: ${{ steps.set_repo_source.outputs.reposource }} | |
steps: | |
- name: Download info on PR | |
uses: dawidd6/action-download-artifact@v6 | |
with: | |
workflow: build-from-fork.yaml | |
workflow_conclusion: completed | |
name: pr-info | |
run_id: ${{ github.event.workflow_run.id }} | |
- id: set_pr_number | |
run: | | |
PR_NUM=$(cat pr_number.txt) | |
echo "prnum=${PR_NUM}" >> $GITHUB_OUTPUT | |
- id: set_pr_head_sha | |
run: | | |
HEAD_SHA=$(cat pr_head_sha.txt) | |
echo "headsha=${HEAD_SHA}" >> $GITHUB_OUTPUT | |
- id: set_pr_base_sha | |
run: | | |
BASE_SHA=$(cat pr_base_sha.txt) | |
echo "basesha=${BASE_SHA}" >> $GITHUB_OUTPUT | |
- id: set_pr_branch | |
run: | | |
BRANCH=$(cat branch.txt) | |
echo "branch=${BRANCH}" >> $GITHUB_OUTPUT | |
- id: set_continue_tests | |
run: | | |
CONTINUE=$(cat continuetests.txt) | |
echo "continue=${CONTINUE}" >> $GITHUB_OUTPUT | |
- id: set_repo_source | |
run: | | |
SOURCE=$(cat forkorsource.txt) | |
echo "reposource=${SOURCE}" >> $GITHUB_OUTPUT | |
get_pr_url: | |
name: "Get PR URL" | |
needs: | |
- get_info_on_pr | |
runs-on: ubuntu-latest | |
env: | |
CONTINUE: ${{ needs.get_info_on_pr.outputs.continuetests }} | |
BRANCH: ${{ needs.get_info_on_pr.outputs.prbranch }} | |
BASESHA: ${{ needs.get_info_on_pr.outputs.basesha }} | |
HEADSHA: ${{ needs.get_info_on_pr.outputs.headsha }} | |
PRNUM: ${{ needs.get_info_on_pr.outputs.prnum }} | |
REPOSOURCE: ${{ needs.get_info_on_pr.outputs.reposource }} | |
# Run only for PRs from default repo and approved PRs from forks | |
if: needs.get_info_on_pr.outputs.continuetests == 'true' | |
outputs: | |
pr_url: ${{ steps.get_env_url.outputs.pr_url }} | |
pr_url_upsun: ${{ steps.get_env_url.outputs.pr_url_upsun }} | |
pr_url_status: ${{ steps.get_env_url.outputs.env_status }} | |
steps: | |
# at this point we want to checkout the repository but at the latest commit of the default branch | |
- uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- name: retrieve PR url | |
id: get_env_url | |
uses: ./.github/actions/get-pr-info | |
with: | |
platformsh_token: ${{ secrets.PLATFORMSH_CLI_TOKEN }} | |
branch: ${{ env.BRANCH }} | |
project: ${{ vars.PROJECT_ID }} | |
dont_run_nonlabeled_forks: | |
name: Warn about non-labeled PRs from forks | |
needs: | |
- get_info_on_pr | |
runs-on: ubuntu-latest | |
if: needs.get_info_on_pr.outputs.continuetests == 'false' | |
steps: | |
- name: Warn the environment will not be built | |
run: | | |
echo "::warning::Pull Requests from forks will not have an environment built until they are given the appropriate label." | |
- uses: actions/checkout@v4 | |
- uses: ./.github/actions/report-status | |
with: | |
job_name: '${{ env.PRURLJOBNAME }}' | |
workflow_name: '${{ github.workflow }}' | |
gh_token: '${{ github.token }}' | |
run_id: '${{ github.run_id }}' | |
head_sha: '${{ needs.get_info_on_pr.outputs.headsha }}' | |
status_description: 'Pull Requests from forks will not have an environment built until they are given the appropriate label.' | |
report_pr_url_status: | |
name: 'report PR URL status' | |
needs: | |
- get_info_on_pr | |
- get_pr_url | |
runs-on: ubuntu-latest | |
env: | |
BRANCH: ${{ needs.get_info_on_pr.outputs.prbranch }} | |
PRNUM: ${{ needs.get_info_on_pr.outputs.prnum }} | |
steps: | |
- name: 'Set status message' | |
id: set-status-message | |
run: | | |
if [ 'success' = '${{ needs.get_pr_url.outputs.pr_url_status }}' ]; then | |
status_message="PR environment successfully deployed." | |
status_state="${{ needs.get_pr_url.outputs.pr_url_status }}" | |
else | |
status_message="Unable to retrieve PR URL. See log output in Actions run or visit https://console.platform.sh/projects/${{ env.PLATFORM_PROJECT }}/${{ env.BRANCH }}" | |
# the status reported by our action most likely wont align with what the gh api is expecting, so if the | |
# status reported by our action isn't `success` we'll manually set it to `failure` | |
status_state="failure" | |
fi | |
echo "status_message=${status_message}" >> $GITHUB_OUTPUT | |
echo "status_state=${status_state}" >> $GITHUB_OUTPUT | |
- name: 'Warn and provide log' | |
if: needs.get_pr_url.outputs.pr_url_status != 'success' | |
env: | |
PLATFORMSH_CLI_TOKEN: ${{ secrets.PLATFORMSH_CLI_TOKEN }} | |
run: | | |
echo "::error::The environment for pull request ${{ env.PRNUM }} failed to deploy. Please rerun this workflow." | |
echo "::warning::Check the logs: https://console.platform.sh/projects/${{ env.PLATFORM_PROJECT }}/${{ env.BRANCH }}" | |
echo "The last status we received was ${{ needs.get_pr_url.outputs.pr_url_status }}" | |
echo "List of activities that we ask for, but not limited to the last one:" | |
platform activities --type "environment.push environment.activate environment.redeploy environment.branch" --environment ${{ env.BRANCH }} | |
echo "Now a list of the (up to) last 20 activities for branch ${{ env.BRANCH }}:" | |
platform activities --environment ${{ env.BRANCH }} --limit 20 | |
# Get the ID of failed activity and output its log: | |
failedID=$(platform activities --format plain --type "environment.push environment.activate environment.redeploy environment.branch" --no-header --columns "ID" --limit 1 --environment ${{ env.BRANCH }} --result=failure) | |
echo "Log for the failed activity, ID ${failedID}" | |
platform activity:log ${failedID} --environment ${{ env.BRANCH }} | |
- uses: actions/checkout@v4 | |
- uses: ./.github/actions/report-status | |
with: | |
job_name: '${{ env.PRURLJOBNAME }}' | |
workflow_name: '${{ github.workflow }}' | |
gh_token: '${{ github.token }}' | |
run_id: '${{ github.run_id }}' | |
head_sha: '${{ needs.get_info_on_pr.outputs.headsha }}' | |
status_description: '${{ steps.set-status-message.outputs.status_message }}' | |
state: '${{ steps.set-status-message.outputs.status_state }}' | |
report_changed_files: | |
name: Collects a list of changed files, reports their URL as a comment | |
needs: | |
- get_info_on_pr | |
- get_pr_url | |
runs-on: ubuntu-latest | |
if: needs.get_pr_url.outputs.pr_url_status == 'success' | |
env: | |
BRANCH: ${{ needs.get_info_on_pr.outputs.prbranch }} | |
BASESHA: ${{ needs.get_info_on_pr.outputs.basesha }} | |
HEADSHA: ${{ needs.get_info_on_pr.outputs.headsha }} | |
PRNUM: ${{ needs.get_info_on_pr.outputs.prnum }} | |
ENV_URL: ${{ needs.get_pr_url.outputs.pr_url }} | |
UPSUN_ENV_URL: ${{ needs.get_pr_url.outputs.pr_url_upsun }} | |
steps: | |
- uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- name: Make pr_comment.txt file | |
run: | | |
# we need this for the later action | |
touch pr_comment.txt | |
- name: Get changed files | |
id: changed-files | |
uses: tj-actions/changed-files@v46 | |
with: | |
sha: ${{ env.HEADSHA }} | |
base_sha: ${{ env.BASESHA }} | |
# Create a list of relevant changed pages | |
- name: Get list of changed files | |
id: get-files | |
run: | | |
files_platform=() | |
files_upsun=() | |
for file in ${{ steps.changed-files.outputs.all_changed_files }}; do | |
# Rewrite all Markdown files in the source to be links in the final environment | |
if [[ $file = "sites/platform/src/"*".md" ]]; then | |
# Remove file extension | |
page=${file/.md/.html} | |
# Remove initial directory | |
page=${page/"sites/platform/src/"/} | |
# Shift index pages up a level | |
page=${page/"/_index.html"/".html"} | |
files_platform+=("$ENV_URL$page") | |
fi | |
if [[ $file = "sites/upsun/src/"*".md" ]]; then | |
# Remove file extension | |
page=${file/.md/.html} | |
# Remove initial directory | |
page=${page/"sites/upsun/src/"/} | |
# Shift index pages up a level | |
page=${page/"/_index.html"/".html"} | |
files_upsun+=("$UPSUN_ENV_URL$page") | |
fi | |
done | |
# If there are changed files for Platform.sh, return a list | |
# Using "\n" here creates problems in passing data to the next step, so replace there instead | |
if (( ${#files_platform[@]} != 0 )); then | |
echo $(printf "||%s" "${files_platform[@]}") | |
echo changed_files_platform=$(printf "||%s" "${files_platform[@]}") >> $GITHUB_OUTPUT | |
else | |
echo changed_files_platform="" >> $GITHUB_OUTPUT | |
fi | |
# If there are changed files for Upsun, return a list | |
# Using "\n" here creates problems in passing data to the next step, so replace there instead | |
if (( ${#files_upsun[@]} != 0 )); then | |
echo $(printf "||%s" "${files_upsun[@]}") | |
echo changed_files_upsun=$(printf "||%s" "${files_upsun[@]}") >> $GITHUB_OUTPUT | |
else | |
echo changed_files_upsun="" >> $GITHUB_OUTPUT | |
fi | |
# If there are changed pages, create a comment with a checklist | |
- name: Comment with links | |
if: steps.get-files.outputs.changed_files_platform != '' || steps.get-files.outputs.changed_files_upsun != '' | |
env: | |
PAGES_PLATFORM: ${{ steps.get-files.outputs.changed_files_platform }} | |
PAGES_UPSUN: ${{ steps.get-files.outputs.changed_files_upsun }} | |
run: | | |
echo -e "Your Platform.sh environment has successfully deployed. :rocket:\n\nSee the changed pages:\n\n<details>\n<summary>Platform.sh docs</summary><br/>\n\n${PAGES_PLATFORM//||/'\n- [ ] '}\n\n</details>\n\n<details>\n<summary>Upsun docs</summary><br/>\n\n${PAGES_UPSUN//||/'\n- [ ] '}" > pr_comment.txt | |
# If there are no changed pages, create a comment | |
# with a link to general URL | |
- name: Comment without links | |
if: steps.get-files.outputs.changed_files_platform == '' && steps.get-files.outputs.changed_files_upsun == '' | |
env: | |
ENV_URL: ${{ needs.get_pr_url.outputs.pr_url }} | |
UPSUN_ENV_URL: ${{ needs.get_pr_url.outputs.pr_url_upsun }} | |
run: | | |
echo -e "Your Platform.sh environment has successfully deployed. :rocket:\n\nSee the site:\n\n- [Platform.sh docs]($ENV_URL)\n- [Upsun docs]($UPSUN_ENV_URL)" > pr_comment.txt | |
- name: Post PR comment | |
uses: marocchino/sticky-pull-request-comment@v2 | |
with: | |
number: ${{ env.PRNUM }} | |
path: pr_comment.txt | |
run-redirection-tests: | |
name: Verify contracted redirections | |
needs: | |
- get_info_on_pr | |
- get_pr_url | |
runs-on: ubuntu-latest | |
env: | |
CONTINUE: ${{ needs.get_info_on_pr.outputs.continuetests }} | |
HEADSHA: ${{ needs.get_info_on_pr.outputs.headsha }} | |
# this isn't REALLY needed since we're depending on get_pr_info and it only completes successfully | |
if: needs.get_info_on_pr.outputs.continuetests == 'true' | |
steps: | |
- uses: actions/checkout@v4 | |
- id: redirection-tests | |
uses: ./.github/actions/redirection-verification | |
with: | |
environment_url: ${{ needs.get_pr_url.outputs.pr_url_upsun }} | |
base_environment_url: ${{ needs.get_pr_url.outputs.pr_url }} | |
redirection_project_id: ${{ secrets.REDIRECTIONIO_PROJECT_ID }} | |
redirection_token: ${{ secrets.REDIRECTIONIO_API_KEY }} | |
report_redirection_test: | |
name: "Report redirection test results" | |
needs: | |
- get_info_on_pr | |
- get_pr_url | |
- run-redirection-tests | |
runs-on: ubuntu-latest | |
env: | |
HEADSHA: ${{ needs.get_info_on_pr.outputs.headsha }} | |
if: ${{ always() }} | |
steps: | |
# we want the default branch | |
- uses: actions/checkout@v4 | |
- name: "Determine status and message" | |
id: determine-status-message | |
run: | | |
if [ 'success' = ${{ needs.run-redirection-tests.result }} ]; then | |
state="success" | |
message="Redirection verifications passed." | |
else | |
# if it isn't `success` then no matter what it was it's a failure | |
state="failure" | |
message="Redirection verifications failed. Please see run summary." | |
if [ 'skipped' = ${{ needs.run-redirection-tests.result }} ] && [ 'false' = '${{ needs.get_info_on_pr.outputs.continuetests }}' ]; then | |
message="Redirection tests can not run on forks until approved." | |
fi | |
fi | |
echo "::warning::${message}" | |
echo "message=${message}" >> $GITHUB_OUTPUT | |
echo "state=${state}" >> $GITHUB_OUTPUT | |
- uses: ./.github/actions/report-status | |
with: | |
# @todo i wish there was a way to go get this info instead of hard-coding it | |
job_name: 'Verify contracted redirections' | |
workflow_name: '${{ github.workflow }}' | |
gh_token: '${{ github.token }}' | |
run_id: '${{ github.run_id }}' | |
head_sha: '${{ env.HEADSHA }}' | |
status_description: '${{ steps.determine-status-message.outputs.message }}' | |
state: '${{ steps.determine-status-message.outputs.state }}' | |
run-e2e-test: | |
runs-on: ubuntu-latest | |
name: E2E tests | |
needs: | |
- get_info_on_pr | |
- get_pr_url | |
env: | |
CONTINUE: ${{ needs.get_info_on_pr.outputs.continuetests }} | |
REPOSOURCE: ${{ needs.get_info_on_pr.outputs.reposource }} | |
HEADSHA: ${{ needs.get_info_on_pr.outputs.headsha }} | |
# this isn't REALLY needed since we're depending on get_pr_info and it only completes successfully | |
if: needs.get_info_on_pr.outputs.continuetests == 'true' | |
strategy: | |
matrix: | |
include: | |
- site: platformsh | |
url: ${{ needs.get_pr_url.outputs.pr_url }} | |
- site: upsun | |
url: ${{ needs.get_pr_url.outputs.pr_url_upsun }} | |
steps: | |
- uses: actions/checkout@v4 | |
if: env.REPOSOURCE == 'source' | |
with: | |
# If this is the base repo, then we want to check out the PR branch so we can potentially run new E2E | |
# tests/changes that are included in the PR. | |
fetch-depth: 0 | |
ref: ${{ env.HEADSHA }} | |
- uses: actions/checkout@v4 | |
# if it isn't the base repo and we're in a fork then we'll checkout the base repo | |
if: env.REPOSOURCE == 'fork' | |
- uses: cypress-io/github-action@v6 | |
with: | |
wait-on: ${{ matrix.url }} | |
record: true | |
env: | |
CYPRESS_baseUrl: ${{ matrix.url }} | |
CYPRESS_environment: github | |
CYPRESS_site: ${{ matrix.site }} | |
CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }} | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
report_e2e_tests: | |
name: Report all E2E tests status | |
runs-on: ubuntu-latest | |
needs: | |
- run-e2e-test | |
- get_info_on_pr | |
- get_pr_url | |
env: | |
HEADSHA: ${{ needs.get_info_on_pr.outputs.headsha }} | |
if: ${{ always() }} | |
steps: | |
- uses: actions/checkout@v4 | |
- name: 'Set status and messages' | |
id: set-status-and-messages | |
run: | | |
if [ 'success' = '${{ needs.run-e2e-test.result }}' ]; then | |
message="All E2E tests passed." | |
state="success" | |
else | |
state="failure" | |
message="One or more E2E tests failed. See run summary." | |
if [ 'skipped' = '${{ needs.run-e2e-test.result }}' ] && [ 'false' = '${{ needs.get_info_on_pr.outputs.continuetests }}' ]; then | |
message="E2E tests can not be run on forks until approved." | |
fi | |
fi | |
echo "::warning::${message}" | |
echo "message=${message}" >> $GITHUB_OUTPUT | |
echo "state=${state}" >> $GITHUB_OUTPUT | |
- name: Report matrix status | |
uses: ./.github/actions/report-status | |
if: always() | |
with: | |
job_name: 'Report all E2E tests status' | |
workflow_name: '${{ github.workflow }}' | |
gh_token: '${{ github.token }}' | |
run_id: '${{ github.run_id }}' | |
head_sha: '${{ needs.get_info_on_pr.outputs.headsha }}' | |
status_description: '${{ steps.set-status-and-messages.outputs.message }}' | |
state: '${{ steps.set-status-and-messages.outputs.state }}' | |