Skip to content

Commit

Permalink
Merge pull request #53 from CasperWA/fix-52-respect-local-state
Browse files Browse the repository at this point in the history
Respect local state.

Remove the fast-forward merging steps and simply
resets the local state to be equal to the temporary branch
and pushes to the target branch.

Add extra CI job to test the new workflow.

Also implements:
- Don't run CI jobs in parallel.
- Actually use checkout@v1 for "v1" CI jobs.
- Use vMAJOR for all GitHub Actions.
- Move push activities into a bash function.
- Fix print messages.
  • Loading branch information
CasperWA authored Oct 13, 2021
2 parents 04693dc + 2fcb09d commit 4102799
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 39 deletions.
15 changes: 5 additions & 10 deletions .github/workflows/ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,9 @@
set -ex

TAG_NAME=$1
if [ -z "${TAG_NAME}" ]; then
if [ -z "${TEST_TAG}" ]; then
TAG_NAME=ci_test
else
TAG_NAME=${TEST_TAG}
fi
fi

git config user.email "[email protected]"
git config user.name "Casper Welzel Andersen"
git config --global user.email "[email protected]"
git config --global user.name "Casper Welzel Andersen"

echo "This is a test. ${RANDOM} ${RANDOM}" >> "ci_test_file.txt"
mv -f extra_data_more.md ../
Expand All @@ -20,4 +13,6 @@ mv -f ci_test_file.txt ../../
git add ../../ci_test_file.txt ../extra_data.log extra_data_more.md
git commit -m "CI tests"

git tag ci_test -a -m "This is a test tag"
if [ -n "${TAG_NAME}" ]; then
git tag ${TAG_NAME} -a -m "This is a test tag"
fi
107 changes: 96 additions & 11 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ jobs:
name: Reset test branches
steps:
- name: Checkout action repo
uses: actions/checkout@v2.3.4
uses: actions/checkout@v2
with:
token: ${{ secrets.CI_RESET_TEST_BRANCHES }}
fetch-depth: 0

- name: Set up Python 3.9
uses: actions/setup-python@v2.2.2
uses: actions/setup-python@v2
with:
python-version: 3.9

Expand Down Expand Up @@ -56,7 +56,7 @@ jobs:

steps:
- name: Use local action (checkout)
uses: actions/checkout@v2.3.4
uses: actions/checkout@v2

- name: Perform changes
run: ./ci.sh ${TEST_TAG}
Expand Down Expand Up @@ -96,12 +96,12 @@ jobs:
git tag -l | grep "${TEST_TAG}"
protected:
needs: reset_test_branches
needs: [reset_test_branches, not_protected]
runs-on: ubuntu-latest
name: Testing - protected
steps:
- name: Use local action (checkout)
uses: actions/checkout@v2.3.4
uses: actions/checkout@v2

- name: Perform changes
run: ./ci.sh
Expand All @@ -119,20 +119,63 @@ jobs:
with:
token: '${{ secrets.CI_PUSH_TO_PROTECTED_BRANCH }}'
branch: protected
unprotect_reviews: true

force-pushing:
needs: [protected]
runs-on: ubuntu-latest
name: Testing - protected (--force)
steps:
- name: Use local action (checkout)
uses: actions/checkout@v2

- name: Perform non-fast-forwardable changes
run: |
git config --global user.email "[email protected]"
git config --global user.name "Casper Welzel Andersen"
git fetch origin
git reset --hard ${{ github.sha }}
touch test.txt
git add test.txt
git commit -m "Diverged from remote branch"
- name: Push not using `--force` (should fail)
id: push_no_force
continue-on-error: true
uses: ./
with:
token: '${{ secrets.CI_PUSH_TO_PROTECTED_BRANCH }}'
branch: protected
unprotect_reviews: true

- name: This runs ONLY if the previous step doesn't fail
if: steps.push_no_force.outcome != 'failure' || steps.push_no_force.conclusion != 'success'
run: |
echo "Outcome: ${{ steps.push_no_force.outcome }} (not 'failure'), Conclusion: ${{ steps.push_no_force.conclusion }} (not 'success')"
exit 1
- name: Push using `--force` (should succeed)
uses: ./
with:
token: '${{ secrets.CI_PUSH_TO_PROTECTED_BRANCH }}'
branch: protected
unprotect_reviews: true
force: yes

reset_test_branches_v1:
needs: [not_protected, protected]
needs: [not_protected, protected, force-pushing]
runs-on: ubuntu-latest
name: Reset test branches - v1
steps:
- name: Checkout action repo
uses: actions/checkout@v2.3.4
uses: actions/checkout@v2
with:
token: ${{ secrets.CI_RESET_TEST_BRANCHES }}
fetch-depth: 0

- name: Set up Python 3.9
uses: actions/setup-python@v2.2.2
uses: actions/setup-python@v2
with:
python-version: 3.9

Expand Down Expand Up @@ -166,7 +209,7 @@ jobs:

steps:
- name: Use local action (checkout)
uses: actions/checkout@v2.3.4
uses: actions/checkout@v1

- name: Perform changes
run: ./ci.sh ${TEST_TAG}
Expand Down Expand Up @@ -206,12 +249,12 @@ jobs:
git tag -l | grep "${TEST_TAG}"
protected_v1:
needs: reset_test_branches_v1
needs: [reset_test_branches_v1, not_protected_v1]
runs-on: ubuntu-latest
name: Testing - protected - v1
steps:
- name: Use local action (checkout)
uses: actions/checkout@v2.3.4
uses: actions/checkout@v1

- name: Perform changes
run: ./ci.sh
Expand All @@ -229,3 +272,45 @@ jobs:
with:
token: '${{ secrets.CI_PUSH_TO_PROTECTED_BRANCH }}'
branch: protected

force-pushing_v1:
needs: [protected_v1]
runs-on: ubuntu-latest
name: Testing - protected (--force) - v1
steps:
- name: Use local action (checkout)
uses: actions/checkout@v1

- name: Perform non-fast-forwardable changes
run: |
git config --global user.email "[email protected]"
git config --global user.name "Casper Welzel Andersen"
git fetch origin
git reset --hard ${{ github.sha }}
touch test.txt
git add test.txt
git commit -m "Diverged from remote branch"
- name: Push not using `--force` (should fail)
id: push_no_force
continue-on-error: true
uses: ./
with:
token: '${{ secrets.CI_PUSH_TO_PROTECTED_BRANCH }}'
branch: protected
unprotect_reviews: true

- name: This runs ONLY if the previous step doesn't fail
if: steps.push_no_force.outcome != 'failure' || steps.push_no_force.conclusion != 'success'
run: |
echo "Outcome: ${{ steps.push_no_force.outcome }} (not 'failure'), Conclusion: ${{ steps.push_no_force.conclusion }} (not 'success')"
exit 1
- name: Push using `--force` (should succeed)
uses: ./
with:
token: '${{ secrets.CI_PUSH_TO_PROTECTED_BRANCH }}'
branch: protected
unprotect_reviews: true
force: yes
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v2.3.4
uses: actions/checkout@v2

- name: Set up Python 3.8
uses: actions/setup-python@v2.2.2
uses: actions/setup-python@v2
with:
python-version: 3.8

Expand Down
34 changes: 18 additions & 16 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,21 @@ remove_remote_temp_branch() {
push_tags() {
if [ -n "${PUSH_PROTECTED_PUSH_TAGS}" ]; then
echo -e "\nPushing tags ..."
git push ${PUSH_PROTECTED_FORCE_PUSH}--tags
git push --tags ${PUSH_PROTECTED_FORCE_PUSH}
echo "Pushing tags ... DONE!"
fi
}
push_to_target() {
git checkout -f ${INPUT_BRANCH}
git reset --hard ${PUSH_PROTECTED_TEMPORARY_BRANCH}
git push ${PUSH_PROTECTED_FORCE_PUSH}
}

# Retrieve target repository
echo -e "\nGetting latest commit of ${GITHUB_REPOSITORY}@${INPUT_BRANCH} ..."
git config --local --name-only --get-regexp "http\.https\:\/\/github\.com\/\.extraheader" && git config --local --unset-all "http.https://github.com/.extraheader" || :
git submodule foreach --recursive 'git config --local --name-only --get-regexp "http\.https\:\/\/github\.com\/\.extraheader" && git config --local --unset-all "http.https://github.com/.extraheader" || :'
git remote set-url origin https://${GITHUB_ACTOR}:${INPUT_TOKEN}@github.com/$GITHUB_REPOSITORY.git
git remote set-url origin https://${GITHUB_ACTOR}:${INPUT_TOKEN}@github.com/${GITHUB_REPOSITORY}.git
git fetch --unshallow -tp origin || :
echo "Getting latest commit of ${GITHUB_REPOSITORY}@${INPUT_BRANCH} ... DONE!"

Expand All @@ -75,20 +80,20 @@ fi
# This will only be non-empty if the branch IS protected
PUSH_PROTECTED_PROTECTED_BRANCH=$(push-action --token "${INPUT_TOKEN}" --ref "${INPUT_BRANCH}" --temp-branch "null" -- protected_branch)

# Create new temporary repository
# Create new temporary branch
PUSH_PROTECTED_TEMPORARY_BRANCH="push-action/${GITHUB_RUN_ID}/${RANDOM}-${RANDOM}-${RANDOM}"
echo -e "\nCreating temporary repository '${PUSH_PROTECTED_TEMPORARY_BRANCH}' ..."
git checkout -f -b ${PUSH_PROTECTED_TEMPORARY_BRANCH}
echo -e "\nCreating temporary branch '${PUSH_PROTECTED_TEMPORARY_BRANCH}' ..."
git checkout -f -b ${PUSH_PROTECTED_TEMPORARY_BRANCH} # throws away any local un-committed changes
if [ -n "${PUSH_PROTECTED_CHANGED_BRANCH}" ] && [ -n "${PUSH_PROTECTED_PROTECTED_BRANCH}" ]; then
git push -f origin ${PUSH_PROTECTED_TEMPORARY_BRANCH}
fi
echo "Creating temporary repository '${PUSH_PROTECTED_TEMPORARY_BRANCH}' ... DONE!"
echo "Creating temporary branch '${PUSH_PROTECTED_TEMPORARY_BRANCH}' ... DONE!"

# --force
case ${INPUT_FORCE} in
y | Y | yes | Yes | YES | true | True | TRUE | on | On | ON)
echo -e "\nWill force push!"
PUSH_PROTECTED_FORCE_PUSH="--force "
PUSH_PROTECTED_FORCE_PUSH="--force"
;;
n | N | no | No | NO | false | False | FALSE | off | Off | OFF)
;;
Expand Down Expand Up @@ -117,22 +122,19 @@ esac
# Unprotect target branch for pull request reviews (if desired)
unprotect &&

# Merge into target branch
echo -e "\nMerging (fast-forward) '${PUSH_PROTECTED_TEMPORARY_BRANCH}' -> '${INPUT_BRANCH}' ..." &&
git checkout ${INPUT_BRANCH} &&
git reset --hard origin/${INPUT_BRANCH} &&
git merge --ff-only ${PUSH_PROTECTED_TEMPORARY_BRANCH} &&
git push ${PUSH_PROTECTED_FORCE_PUSH} &&
# Push to target branch
echo -e "\nPushing '${PUSH_PROTECTED_TEMPORARY_BRANCH}' -> 'origin/${INPUT_BRANCH}' ..." &&
push_to_target &&
push_tags &&
echo "Merging (fast-forward) '${PUSH_PROTECTED_TEMPORARY_BRANCH}' -> '${INPUT_BRANCH}' ... DONE!" &&
echo "Pushing '${PUSH_PROTECTED_TEMPORARY_BRANCH}' -> 'origin/${INPUT_BRANCH}' ... DONE!" &&

# Re-protect target branch for pull request reviews (if desired)
protect &&

# Remove temporary repository
# Remove temporary branch
remove_remote_temp_branch
} || {
# Remove temporary repository
# Remove temporary branch
remove_remote_temp_branch &&
exit 1
}

0 comments on commit 4102799

Please sign in to comment.