Skip to content

Commit

Permalink
Don't overwrite updates made on staging
Browse files Browse the repository at this point in the history
  • Loading branch information
andriokha committed Jun 25, 2024
1 parent 0952c29 commit 4e077f4
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 25 deletions.
16 changes: 12 additions & 4 deletions .github/actions/test-setup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -61,6 +65,7 @@ runs:
git config --global user.name 'Test User'
git config --global user.email '[email protected]'
# 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 .
Expand All @@ -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 '[email protected]'
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 }}
Expand Down
14 changes: 10 additions & 4 deletions .github/actions/test-teardown/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
42 changes: 26 additions & 16 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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 }}

Expand All @@ -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 }}

Expand Down Expand Up @@ -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
Expand All @@ -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 }}

Expand All @@ -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:
Expand All @@ -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 }}

Expand All @@ -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"
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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 }}

Expand Down Expand Up @@ -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 }}

Expand Down Expand Up @@ -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
Expand All @@ -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 }}

Expand All @@ -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 }}

Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 6 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 }}
Expand Down
3 changes: 3 additions & 0 deletions workflow-templates/update-config-branch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'.

Expand All @@ -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' }}

0 comments on commit 4e077f4

Please sign in to comment.