Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating a separate base branch in test Workflow #543

Merged
merged 6 commits into from
Aug 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/test-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ on:
jobs:
test_in_pull_request:
name: Test in Pull Request
if: ${{ startsWith(github.head_ref, 'workflows/tests/') }}
if: ${{ startsWith(github.head_ref, 'workflows/tests/head') }}
runs-on: ubuntu-latest
steps:
- name: Sanity check
if: ${{ github.base_ref == 'refs/heads/main' || github.base_ref == 'main' }}
if: ${{ !startsWith(github.base_ref, 'workflows/tests/base') }}
run: exit 1

- uses: actions/checkout@v3
Expand Down
149 changes: 113 additions & 36 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:

steps:
- name: Sanity check
if: ${{ startsWith(github.head_ref, 'workflows/tests/') || github.head_ref == 'refs/heads/main' }}
if: ${{ startsWith(github.head_ref, 'workflows/tests/') }}
run: exit 1

- uses: actions/checkout@v3
Expand All @@ -40,9 +40,8 @@ jobs:
BASE_BRANCH_NAME: ${{ github.head_ref }}
run: |
git fetch --unshallow origin
git checkout $BASE_BRANCH_NAME

- name: Git checkout
- name: Git checkout base branch
id: git-checkout
env:
PULL_REQUEST_BRANCH_NAME: ${{ github.event.pull_request.head.ref }}
Expand All @@ -58,51 +57,93 @@ jobs:
echo "::error::Fail to identify Pull Request branch"
exit 1
fi
echo "::set-output name=base-branch-name::$BASE_BRANCH_NAME"
echo "::notice::Base branch is $BASE_BRANCH_NAME"
git checkout $BASE_BRANCH_NAME
echo "::set-output name=base-branch-name::$BASE_BRANCH_NAME"
echo "::notice::Base commit is `git rev-parse --verify HEAD`"

- name: Git checkout test base branch
env:
TEST_BASE_BRANCH_NAME: ${{ format('workflows/tests/base/{0}-{1}-on-{2}', github.run_number, github.run_attempt, github.sha) }}
run: |
echo "::notice::Base branch is $TEST_BASE_BRANCH_NAME"
git checkout -b "$TEST_BASE_BRANCH_NAME"

- name: Make commit
- name: Make test base commit
env:
SHA: ${{ github.sha }}
RUN_NUMBER: ${{ github.run_number }}
RUN_ATTEMPT: ${{ github.run_attempt }}
BASE_BRANCH_NAME: ${{ steps.git-checkout.outputs.base-branch-name }}
TEST_BRANCH_NAME: ${{ format('workflows/tests/{0}-{1}-on-{2}', github.run_number, github.run_attempt, github.sha) }}
BASE_PR_NUMBER: ${{ github.event.pull_request.number }}
run: |
git commit --allow-empty -m "Test base commit for #$BASE_PR_NUMBER ($SHA) by $RUN_NUMBER-$RUN_ATTEMPT"
echo "::notice::Test base commit is `git rev-parse --verify HEAD`"

- name: Git push test base branch
env:
TEST_BASE_BRANCH_NAME: ${{ format('workflows/tests/base/{0}-{1}-on-{2}', github.run_number, github.run_attempt, github.sha) }}
run: |
git push -f --set-upstream origin "$TEST_BASE_BRANCH_NAME"
echo "::notice::Remote test branch is origin/$TEST_BASE_BRANCH_NAME"

- name: Open base PR
id: open-base-pr
env:
GH_TOKEN: ${{ secrets.CREATE_PULL_REQUEST_ACTION_TOKEN }}
SHA: ${{ github.sha }}
RUN_NUMBER: ${{ github.run_number }}
RUN_ATTEMPT: ${{ github.run_attempt }}
BASE_PR_NUMBER: ${{ github.event.pull_request.number }}
run: |
echo "::notice::Run number is $RUN_NUMBER"
echo "::notice::Run attempt is $RUN_ATTEMPT"
echo "::notice::Base branch is $BASE_BRANCH_NAME"
echo "::notice::Base commit is `git rev-parse --verify HEAD`"
echo "::notice::Test branch is $TEST_BRANCH_NAME"
git checkout -b "$TEST_BRANCH_NAME"
git commit --allow-empty -m "Test commit for #$BASE_PR_NUMBER ($SHA) by $RUN_NUMBER-$RUN_ATTEMPT"
echo "::notice::Test commit is `git rev-parse --verify HEAD`"
echo "::notice::Base PR number is $BASE_PR_NUMBER"
gh pr create --base "main" --title "Test base PR for #$BASE_PR_NUMBER ($SHA)" --body "Generated from #$BASE_PR_NUMBER. Run $RUN_NUMBER. Attempt $RUN_ATTEMPT."
TEST_BASE_PR_NUMBER=`gh pr view | grep 'number:' | cut -f 2`
echo "::set-output name=test-base-pr-number::$TEST_BASE_PR_NUMBER"
echo "::notice::Test base PR number is $TEST_BASE_PR_NUMBER"

- name: Git push
- name: Git checkout test head branch
env:
TEST_BRANCH_NAME: ${{ format('workflows/tests/{0}-{1}-on-{2}', github.run_number, github.run_attempt, github.sha) }}
TEST_HEAD_BRANCH_NAME: ${{ format('workflows/tests/head/{0}-{1}-on-{2}', github.run_number, github.run_attempt, github.sha) }}
run: |
git push -f --set-upstream origin "$TEST_BRANCH_NAME"
echo "::notice::Remote test branch is origin/$TEST_BRANCH_NAME"
echo "::notice::Test head branch is $TEST_HEAD_BRANCH_NAME"
git checkout -b "$TEST_HEAD_BRANCH_NAME"

- name: Open PR
id: open-pr
- name: Make test head commit
env:
SHA: ${{ github.sha }}
RUN_NUMBER: ${{ github.run_number }}
RUN_ATTEMPT: ${{ github.run_attempt }}
BASE_PR_NUMBER: ${{ github.event.pull_request.number }}
run: |
git commit --allow-empty -m "Test head commit for #$BASE_PR_NUMBER ($SHA) by $RUN_NUMBER-$RUN_ATTEMPT"
echo "::notice::Test head commit is `git rev-parse --verify HEAD`"

- name: Git push test head branch
env:
TEST_HEAD_BRANCH_NAME: ${{ format('workflows/tests/head/{0}-{1}-on-{2}', github.run_number, github.run_attempt, github.sha) }}
run: |
git push -f --set-upstream origin "$TEST_HEAD_BRANCH_NAME"
echo "::notice::Remote test branch is origin/$TEST_HEAD_BRANCH_NAME"

- name: Open head PR
id: open-head-pr
env:
GH_TOKEN: ${{ secrets.CREATE_PULL_REQUEST_ACTION_TOKEN }}
SHA: ${{ github.sha }}
RUN_NUMBER: ${{ github.run_number }}
RUN_ATTEMPT: ${{ github.run_attempt }}
BASE_BRANCH_NAME: ${{ steps.git-checkout.outputs.base-branch-name }}
TEST_BASE_BRANCH_NAME: ${{ format('workflows/tests/base/{0}-{1}-on-{2}', github.run_number, github.run_attempt, github.sha) }}
BASE_PR_NUMBER: ${{ github.event.pull_request.number }}
run: |
echo "::notice::Base PR number is $BASE_PR_NUMBER"
gh pr create --base "$BASE_BRANCH_NAME" --title "Test PR for #$BASE_PR_NUMBER ($SHA)" --body "Generated from #$BASE_PR_NUMBER. Run $RUN_NUMBER. Attempt $RUN_ATTEMPT.
gh pr create --base "$TEST_BASE_BRANCH_NAME" --title "Test head PR for #$BASE_PR_NUMBER ($SHA)" --body "Generated from #$BASE_PR_NUMBER. Run $RUN_NUMBER. Attempt $RUN_ATTEMPT.

#accept2ship"
TEST_PR_NUMBER=`gh pr view | grep 'number:' | cut -f 2`
echo "::set-output name=test-pr-number::$TEST_PR_NUMBER"
echo "::notice::Test PR number is $TEST_PR_NUMBER"
TEST_HEAD_PR_NUMBER=`gh pr view | grep 'number:' | cut -f 2`
echo "::set-output name=test-head-pr-number::$TEST_HEAD_PR_NUMBER"
echo "::notice::Test PR number is $TEST_HEAD_PR_NUMBER"

- name: Approve PR
env:
Expand Down Expand Up @@ -130,10 +171,10 @@ jobs:
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
BASE_PR_NUMBER: ${{ github.event.pull_request.number }}
TEST_PR_NUMBER: ${{ steps.open-pr.outputs.test-pr-number }}
TEST_HEAD_PR_NUMBER: ${{ steps.open-head-pr.outputs.test-head-pr-number }}
CHECKS_TEMP_FILE: ${{ steps.check-pr.outputs.checks-temp-file }}
run: |
gh pr comment "$BASE_PR_NUMBER" --body "**Checks from #$TEST_PR_NUMBER**
gh pr comment "$BASE_PR_NUMBER" --body "**Checks from #$TEST_HEAD_PR_NUMBER**

| Check | Status | URL |
| --- | --- | --- |
Expand All @@ -143,44 +184,80 @@ jobs:
- name: Verify PR
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
TEST_PR_NUMBER: ${{ steps.open-pr.outputs.test-pr-number }}
run: |
TEST_PR_STATE=`gh pr view --json 'state' -q '.state'`
echo "::set-output name=test_pr_state::$TEST_PR_STATE"
echo "::notice::Test PR state is $TEST_PR_STATE"
if [ $TEST_PR_STATE != 'MERGED' ]
if [ "$TEST_PR_STATE" != 'MERGED' ]
then
exit 1
fi

- name: Close PR
if: ${{ always() }}
- name: Close head PR
if: ${{ failure() || cancelled() }}
continue-on-error: true
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SHA: ${{ github.sha }}
RUN_NUMBER: ${{ github.run_number }}
RUN_ATTEMPT: ${{ github.run_attempt }}
BASE_BRANCH_NAME: ${{ steps.git-checkout.outputs.base-branch-name }}
TEST_PR_NUMBER: ${{ steps.open-pr.outputs.test-pr-number }}
TEST_HEAD_PR_NUMBER: ${{ steps.open-head-pr.outputs.test-head-pr-number }}
run: |
if [ $BASE_BRANCH_NAME != '' ]
TEST_PR_STATE=`gh pr view --json 'state' -q '.state'`
if [ "$TEST_PR_STATE" = 'MERGED' ]
then
exit
fi
if [ -n "$BASE_BRANCH_NAME" ]
then
git checkout "$BASE_BRANCH_NAME"
echo "::notice::Base branch is `git branch --show-current`"
else
git checkout "$SHA"
echo "::notice::Base commit is `git rev-parse --verify HEAD`"
fi
gh pr close $TEST_PR_NUMBER -d -c "Finished testing $SHA by $RUN_NUMBER-$RUN_ATTEMPT. Closing PR."
gh pr close $TEST_HEAD_PR_NUMBER -d -c "Finished testing $SHA by $RUN_NUMBER-$RUN_ATTEMPT. Closing PR."

- name: Delete remote branch
- name: Delete remote test head branch
if: ${{ always() }}
continue-on-error: true
env:
TEST_HEAD_BRANCH_NAME: ${{ format('workflows/tests/head/{0}-{1}-on-{2}', github.run_number, github.run_attempt, github.sha) }}
run: |
if test $(git ls-remote origin --heads "$TEST_HEAD_BRANCH_NAME" | wc -l ) != 0
then
git push -d origin "$TEST_HEAD_BRANCH_NAME"
fi

- name: Close base PR
if: ${{ always() }}
continue-on-error: true
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SHA: ${{ github.sha }}
RUN_NUMBER: ${{ github.run_number }}
RUN_ATTEMPT: ${{ github.run_attempt }}
TEST_BRANCH_NAME: ${{ format('workflows/tests/{0}-{1}-on-{2}', github.run_number, github.run_attempt, github.sha) }}
BASE_BRANCH_NAME: ${{ steps.git-checkout.outputs.base-branch-name }}
TEST_BASE_PR_NUMBER: ${{ steps.open-base-pr.outputs.test-base-pr-number }}
run: |
git push -d origin "$TEST_BRANCH_NAME"
if [ -n "$BASE_BRANCH_NAME" ]
then
git checkout "$BASE_BRANCH_NAME"
echo "::notice::Base branch is `git branch --show-current`"
else
git checkout "$SHA"
echo "::notice::Base commit is `git rev-parse --verify HEAD`"
fi
gh pr close $TEST_BASE_PR_NUMBER -d -c "Finished testing $SHA by $RUN_NUMBER-$RUN_ATTEMPT. Closing PR."

- name: Delete remote test base branch
if: ${{ always() }}
continue-on-error: true
env:
TEST_BASE_BRANCH_NAME: ${{ format('workflows/tests/base/{0}-{1}-on-{2}', github.run_number, github.run_attempt, github.sha) }}
run: |
if test $(git ls-remote origin --heads "$TEST_BASE_BRANCH_NAME" | wc -l ) != 0
then
git push -d origin "$TEST_BASE_BRANCH_NAME"
fi