From 4e077f4fd89d81c11fad36d328027448dfc2640d Mon Sep 17 00:00:00 2001 From: AndyF Date: Mon, 24 Jun 2024 19:31:56 +0100 Subject: [PATCH] Don't overwrite updates made on staging --- .github/actions/test-setup/action.yml | 16 ++++++-- .github/actions/test-teardown/action.yml | 14 +++++-- .github/workflows/test.yml | 42 +++++++++++++-------- README.md | 4 ++ action.yml | 7 +++- workflow-templates/update-config-branch.yml | 3 ++ 6 files changed, 61 insertions(+), 25 deletions(-) diff --git a/.github/actions/test-setup/action.yml b/.github/actions/test-setup/action.yml index 9bddcc3..866f3da 100644 --- a/.github/actions/test-setup/action.yml +++ b/.github/actions/test-setup/action.yml @@ -26,6 +26,10 @@ inputs: description: The branch on the site repository that mirrors the config repository. type: string required: true + test_site_repo_live_branch: + description: The name of the live branch (ie the one that's currently deployed). + type: string + required: true test_site_repo_pr_branch: description: The name of the topic branch to create on the site repository with config changes. type: string @@ -61,6 +65,7 @@ runs: git config --global user.name 'Test User' git config --global user.email 'test@example.com' + # Create the main branch on the config repo with two commits. pushd remote git checkout --orphan ${{ inputs.test_config_repo_branch }}-new git rm -rf . @@ -72,26 +77,29 @@ runs: git add c e git rm d git commit -m "Updated" - git push origin HEAD:${{ inputs.test_config_repo_branch }} --force + git push origin HEAD:${{ inputs.test_config_repo_branch }} git checkout ${{ inputs.test_config_repo_branch }} git reset --hard origin/${{ inputs.test_config_repo_branch }} popd + # Add the mock site with current config to staging and main. pushd site git checkout --orphan ${{ inputs.test_site_repo_pr_branch_base }}-new git rm -rf . mkdir -p a z config/sync cp -r ../remote/* config/sync touch {a,z}/.gitkeep b - git config user.name 'Test User' - git config user.email 'test@example.com' git add . git commit -m "Add mock Drupal site" + git branch ${{ inputs.test_site_repo_live_branch }} + git push origin ${{ inputs.test_site_repo_live_branch }} + + # Add an extra commit to staging only with an additional config change. echo modified > config/sync/e git add config/sync git commit -m "Simulate updating config during development" - git push origin HEAD:${{ inputs.test_site_repo_pr_branch_base }} --force + git push origin HEAD:${{ inputs.test_site_repo_pr_branch_base }} git checkout ${{ inputs.test_site_repo_pr_branch_base }} git reset --hard origin/${{ inputs.test_site_repo_pr_branch_base }} diff --git a/.github/actions/test-teardown/action.yml b/.github/actions/test-teardown/action.yml index b56ab1a..195d832 100644 --- a/.github/actions/test-teardown/action.yml +++ b/.github/actions/test-teardown/action.yml @@ -12,10 +12,16 @@ runs: steps: - name: Tear down for test shell: bash - working-directory: test/site + working-directory: test run: | - # Tear down for test set -eu - git for-each-ref "refs/remotes/origin/${{ inputs.branch_prefix }}*" --format '%(refname:short)' | while read refname - do git push -d origin ${refname//'origin/'/} + # Tear down for test + for directory in remote site; do + pushd "$directory" + git for-each-ref "refs/remotes/origin/${{ inputs.branch_prefix }}*" --format '%(refname:short)' | while read refname; do + branch=${refname//'origin/'/} + git push -d origin $branch + echo "Deleted $directory branch '$branch'." + done + popd done diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 4f8a189..0065305 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -37,6 +37,7 @@ jobs: env: test_config_repo_branch: "${{ github.job }}-main" test_site_repo_config_branch: "${{ github.job }}-config-only" + test_site_repo_live_branch: "${{ github.job }}-main" test_site_repo_pr_branch: "${{ github.job }}-automatic-config-export" test_site_repo_pr_branch_base: "${{ github.job }}-staging" steps: @@ -54,6 +55,7 @@ jobs: test_site_repo: ${{ vars.TEST_SITE_REPO }} test_site_repo_token: ${{ secrets.TEST_SITE_REPO_TOKEN }} test_site_repo_config_branch: ${{ env.test_site_repo_config_branch }} + test_site_repo_live_branch: ${{ env.test_site_repo_live_branch }} test_site_repo_pr_branch: ${{ env.test_site_repo_pr_branch }} test_site_repo_pr_branch_base: ${{ env.test_site_repo_pr_branch_base }} @@ -65,6 +67,7 @@ jobs: site_repo: ${{ vars.TEST_SITE_REPO }} site_repo_token: ${{ secrets.TEST_SITE_REPO_TOKEN }} site_repo_config_branch: ${{ env.test_site_repo_config_branch }} + site_repo_live_branch: ${{ env.test_site_repo_live_branch }} site_repo_pr_branch: ${{ env.test_site_repo_pr_branch }} site_repo_pr_branch_base: ${{ env.test_site_repo_pr_branch_base }} @@ -104,6 +107,7 @@ jobs: test_site_repo_config_branch: "${{ github.job }}-config-only" test_site_repo_pr_branch: "${{ github.job }}-automatic-config-export" test_site_repo_pr_branch_base: "${{ github.job }}-staging" + test_site_repo_live_branch: "${{ github.job }}-main" steps: - uses: actions/checkout@v4 @@ -119,6 +123,7 @@ jobs: test_site_repo: ${{ vars.TEST_SITE_REPO }} test_site_repo_token: ${{ secrets.TEST_SITE_REPO_TOKEN }} test_site_repo_config_branch: ${{ env.test_site_repo_config_branch }} + test_site_repo_live_branch: ${{ env.test_site_repo_live_branch }} test_site_repo_pr_branch: ${{ env.test_site_repo_pr_branch }} test_site_repo_pr_branch_base: ${{ env.test_site_repo_pr_branch_base }} @@ -129,17 +134,27 @@ jobs: cd site git remote add remote-config ../remote git fetch remote-config + # Create the site repo config-only branch 1 commit behind the config repo. git checkout -b ${{ env.test_site_repo_config_branch }} remote-config/${{ env.test_config_repo_branch }}^ tmp_dir=$(mktemp -d) cp * "$tmp_dir" git push origin HEAD - git checkout ${{ env.test_site_repo_pr_branch_base }} + # Ensure the live branch matches the new site repo config branch. + git checkout ${{ env.test_site_repo_live_branch }} rm config/sync/* - cp -r "$tmp_dir"/* config/sync - git add . + mv "$tmp_dir"/* config/sync + git add config/sync git commit --amend -m "Update" - git push origin HEAD --force + git push --force origin HEAD + + # Update the staging branch with a fresh config change. + git checkout ${{ env.test_site_repo_pr_branch_base }} + git reset --hard ${{ env.test_site_repo_live_branch }} + echo modified > config/sync/e + git add config/sync/e + git commit -m "Simulate updating config during development" + git push --force - uses: ./test/update-config with: @@ -149,6 +164,7 @@ jobs: site_repo: ${{ vars.TEST_SITE_REPO }} site_repo_token: ${{ secrets.TEST_SITE_REPO_TOKEN }} site_repo_config_branch: ${{ env.test_site_repo_config_branch }} + site_repo_live_branch: ${{ env.test_site_repo_live_branch }} site_repo_pr_branch: ${{ env.test_site_repo_pr_branch }} site_repo_pr_branch_base: ${{ env.test_site_repo_pr_branch_base }} @@ -168,9 +184,6 @@ jobs: run: | set -eu exit=0 - #tmp_dir=$(mktemp -d) - #git checkout ${{ env.test_site_repo_config_branch }} - #cp * "$tmp_dir" if [[ "$(gh pr list --head ${{ env.test_site_repo_pr_branch }} --json id)" == "[]" ]]; then echo "**TEST FAILURE:** There should be a PR created." >> "$GITHUB_STEP_SUMMARY" @@ -189,15 +202,6 @@ jobs: exit=1 fi - #rm config/sync/* - #mv "$tmp_dir"/* config/sync - #if [[ -n "$(git status --porcelain)" ]]; then - # echo "**TEST FAILURE:** The PR branch config should match the config repo branch." >> "$GITHUB_STEP_SUMMARY" - # exit=1 - #fi - - #rm -rf "$tmp_dir" - exit $exit - uses: ./test/update-config/.github/actions/test-teardown @@ -212,6 +216,7 @@ jobs: env: test_config_repo_branch: "${{ github.job }}-main" test_site_repo_config_branch: "${{ github.job }}-config-only" + test_site_repo_live_branch: "${{ github.job }}-main" test_site_repo_pr_branch: "${{ github.job }}-automatic-config-export" test_site_repo_pr_branch_base: "${{ github.job }}-staging" steps: @@ -229,6 +234,7 @@ jobs: test_site_repo: ${{ vars.TEST_SITE_REPO }} test_site_repo_token: ${{ secrets.TEST_SITE_REPO_TOKEN }} test_site_repo_config_branch: ${{ env.test_site_repo_config_branch }} + test_site_repo_live_branch: ${{ env.test_site_repo_live_branch }} test_site_repo_pr_branch: ${{ env.test_site_repo_pr_branch }} test_site_repo_pr_branch_base: ${{ env.test_site_repo_pr_branch_base }} @@ -273,6 +279,7 @@ jobs: site_repo: ${{ vars.TEST_SITE_REPO }} site_repo_token: ${{ secrets.TEST_SITE_REPO_TOKEN }} site_repo_config_branch: ${{ env.test_site_repo_config_branch }} + site_repo_live_branch: ${{ env.test_site_repo_live_branch }} site_repo_pr_branch: ${{ env.test_site_repo_pr_branch }} site_repo_pr_branch_base: ${{ env.test_site_repo_pr_branch_base }} @@ -311,6 +318,7 @@ jobs: test_site_repo_config_branch: "${{ github.job }}-config-only" test_site_repo_pr_branch: "${{ github.job }}-automatic-config-export" test_site_repo_pr_branch_base: "${{ github.job }}-staging" + test_site_repo_live_branch: "${{ github.job }}-main" steps: - uses: actions/checkout@v4 @@ -326,6 +334,7 @@ jobs: test_site_repo: ${{ vars.TEST_SITE_REPO }} test_site_repo_token: ${{ secrets.TEST_SITE_REPO_TOKEN }} test_site_repo_config_branch: ${{ env.test_site_repo_config_branch }} + test_site_repo_live_branch: "${{ github.job }}-main" test_site_repo_pr_branch: ${{ env.test_site_repo_pr_branch }} test_site_repo_pr_branch_base: ${{ env.test_site_repo_pr_branch_base }} @@ -348,6 +357,7 @@ jobs: site_repo: ${{ vars.TEST_SITE_REPO }} site_repo_token: ${{ secrets.TEST_SITE_REPO_TOKEN }} site_repo_config_branch: ${{ env.test_site_repo_config_branch }} + site_repo_live_branch: ${{ env.test_site_repo_live_branch }} site_repo_pr_branch: ${{ env.test_site_repo_pr_branch }} site_repo_pr_branch_base: ${{ env.test_site_repo_pr_branch_base }} diff --git a/README.md b/README.md index e3c286d..aca52fd 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,10 @@ with: # The branch on the site repository that mirrors the config repository. # Default: config-only site_repo_config_branch: '' + + # The name of the live branch (ie the one that's currently deployed). + # Default: main + site_repo_live_branch: '' # The name of the topic branch to create on the site repository with config # changes. diff --git a/action.yml b/action.yml index d2631c7..5404839 100644 --- a/action.yml +++ b/action.yml @@ -40,6 +40,11 @@ inputs: type: string required: true default: config-only + site_repo_live_branch: + description: The name of the live branch (ie the one that's currently deployed). + type: string + default: main + required: true site_repo_pr_branch: description: The name of the topic branch to create on the site repository with config changes. type: string @@ -167,7 +172,7 @@ runs: set -eu source_branch=${{ inputs.site_repo_pr_branch }} if ! git show-branch remotes/origin/${{ inputs.site_repo_pr_branch }} &> /dev/null; then - git branch ${{ inputs.site_repo_pr_branch }} origin/${{ inputs.site_repo_pr_branch_base }} + git branch ${{ inputs.site_repo_pr_branch }} origin/${{ inputs.site_repo_live_branch }} source_branch=${{ inputs.site_repo_pr_branch_base }} fi git checkout ${{ inputs.site_repo_pr_branch }} diff --git a/workflow-templates/update-config-branch.yml b/workflow-templates/update-config-branch.yml index 4045c6c..185004f 100644 --- a/workflow-templates/update-config-branch.yml +++ b/workflow-templates/update-config-branch.yml @@ -13,6 +13,8 @@ name: Check for config updates # - CONFIG_REPO: The GitHub config repo, eg. MyOrg/MySiteConfig. # - SITE_REPO_PR_BRANCH_BASE: (optional) The branch from which the config topic # branch will be made for the PR; defaults to 'staging'. +# - SITE_REPO_LIVE_BRANCH: (optional) The name of the live branch (ie the one +# that's currently deployed). # - GITHUB_NOTIFY: (optional) A space-separated list of github users including # '@' to ping on a new PR, eg. '@andy @becca'. @@ -38,3 +40,4 @@ jobs: config_repo_token: ${{ secrets.CONFIG_REPO_TOKEN }} github_notify: ${{ vars.GITHUB_NOTIFY }} site_repo_pr_branch_base: ${{ vars.SITE_REPO_PR_BRANCH_BASE || 'staging' }} + site_repo_live_branch: ${{ vars.SITE_REPO_LIVE_BRANCH || 'main' }}