From b43b619240e653b67416afe566732cb4fffe51ba Mon Sep 17 00:00:00 2001 From: Leigh Oliver Date: Thu, 28 Apr 2022 11:51:18 +0100 Subject: [PATCH 1/4] Add test case for workflow failure on nothing to commit --- .github/workflows/test-tag.yml | 44 +++++++++++++++++++++++----------- VERSION | 2 +- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/.github/workflows/test-tag.yml b/.github/workflows/test-tag.yml index 86eb2bb..841f3dd 100644 --- a/.github/workflows/test-tag.yml +++ b/.github/workflows/test-tag.yml @@ -3,25 +3,11 @@ name: Run tests, Tag release on: push: branches: - # explicit deny all here to make sure there isnt a commit loop - - '!*' - main - fix/* jobs: - create_tag: - runs-on: ubuntu-latest - if: github.ref == 'refs/heads/main' - needs: run_tests - steps: - - name: Checkout - uses: actions/checkout@v2 - - - name: check if tag already exists - run: git fetch --depth=1 origin +refs/tags/*:refs/tags/* && git rev-parse "$(cat VERSION)" >/dev/null 2>&1 && exit 1 || exit 0 - - name: create tag - run: git config --global user.email "${{ github.actor }}" && git config --global user.name "${{ github.actor }}" && git tag -a -m "$(cat VERSION)" $(cat VERSION) && git push --follow-tags run_tests: runs-on: ubuntu-latest steps: @@ -98,3 +84,33 @@ jobs: destination_branch: commit_message_test deploy_key: ${{ secrets.DEPLOY_KEY }} commit_message: testing a custom commit message (${{ github.sha }}) + + + # Test that fails on nothing to commit - a copy of the previous test + - name: test issue where workflow fails when there is nothing to commit + uses: ./ + with: + source: tmp.txt + destination_repo: leigholiver/commit-with-deploy-key + destination_branch: commit_message_test + deploy_key: ${{ secrets.DEPLOY_KEY }} + commit_message: testing a custom commit message (${{ github.sha }}) + + # Test that nothing to commit is OK + + + + # If the tests are successful, and we've merged to main, create a tag + create_tag: + runs-on: ubuntu-latest + if: github.ref == 'refs/heads/main' + needs: run_tests + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: check if tag already exists + run: git fetch --depth=1 origin +refs/tags/*:refs/tags/* && git rev-parse "$(cat VERSION)" >/dev/null 2>&1 && exit 1 || exit 0 + + - name: create tag + run: git config --global user.email "${{ github.actor }}" && git config --global user.name "${{ github.actor }}" && git tag -a -m "$(cat VERSION)" $(cat VERSION) && git push --follow-tags diff --git a/VERSION b/VERSION index 570c796..e946d6b 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -v1.0.2 +v1.0.3 From a637ac7ba7a220cb8488a5b0e802642be343e1fb Mon Sep 17 00:00:00 2001 From: Leigh Oliver Date: Thu, 28 Apr 2022 12:04:45 +0100 Subject: [PATCH 2/4] Check for new commit before trying to push --- README.md | 2 +- VERSION | 2 +- entrypoint.sh | 12 ++++++++++-- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 6955fe1..a79292d 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Action to commit a file/directory to another repo using a deploy key. * Add the private key to your source repo as a secret ```yaml -uses: leigholiver/commit-with-deploy-key@v1.0.2 +uses: leigholiver/commit-with-deploy-key@v1.1.0 with: source: build_output destination_folder: dist diff --git a/VERSION b/VERSION index e946d6b..795460f 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -v1.0.3 +v1.1.0 diff --git a/entrypoint.sh b/entrypoint.sh index 6b9a686..1c068ea 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -42,10 +42,18 @@ elif [ -d $CALLING_DIR/$INPUT_SOURCE ]; then cp -a $CALLING_DIR/$INPUT_SOURCE/* . fi -# commit and push +# create the commit git add . git commit -m "${INPUT_COMMIT_MESSAGE}" -GIT_SSH_COMMAND=$GIT_SSH git push -u origin $INPUT_DESTINATION_BRANCH + +# if there is a new commit - push it to the destination +if [ ! -z "$(git cherry -v)" ]; then + GIT_SSH_COMMAND=$GIT_SSH git push -u origin $INPUT_DESTINATION_BRANCH + +# otherwise, don't try to push - this causes the workflow to fail +else + echo "Nothing waiting to be pushed" +fi # output the commit hash echo "::set-output name=commit_hash::$(git rev-parse HEAD)" From 3cef6ba75449633eab30cb6241dda84ed07e67c8 Mon Sep 17 00:00:00 2001 From: Leigh Oliver Date: Thu, 28 Apr 2022 13:02:27 +0100 Subject: [PATCH 3/4] Alternative fix with a better understanding of whats going on... --- entrypoint.sh | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index 1c068ea..486e2b9 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -44,16 +44,8 @@ fi # create the commit git add . -git commit -m "${INPUT_COMMIT_MESSAGE}" - -# if there is a new commit - push it to the destination -if [ ! -z "$(git cherry -v)" ]; then - GIT_SSH_COMMAND=$GIT_SSH git push -u origin $INPUT_DESTINATION_BRANCH - -# otherwise, don't try to push - this causes the workflow to fail -else - echo "Nothing waiting to be pushed" -fi +git commit -m "${INPUT_COMMIT_MESSAGE}" || echo +GIT_SSH_COMMAND=$GIT_SSH git push -u origin $INPUT_DESTINATION_BRANCH # output the commit hash echo "::set-output name=commit_hash::$(git rev-parse HEAD)" From 15d8bec8e695d0b96cb320b013969e6f381d0919 Mon Sep 17 00:00:00 2001 From: Leigh Oliver Date: Thu, 28 Apr 2022 14:57:32 +0100 Subject: [PATCH 4/4] tidy up --- .github/workflows/test-tag.yml | 7 +---- README.md | 49 +++++++++++++--------------------- VERSION | 2 +- 3 files changed, 21 insertions(+), 37 deletions(-) diff --git a/.github/workflows/test-tag.yml b/.github/workflows/test-tag.yml index 841f3dd..1cc9430 100644 --- a/.github/workflows/test-tag.yml +++ b/.github/workflows/test-tag.yml @@ -85,9 +85,7 @@ jobs: deploy_key: ${{ secrets.DEPLOY_KEY }} commit_message: testing a custom commit message (${{ github.sha }}) - - # Test that fails on nothing to commit - a copy of the previous test - - name: test issue where workflow fails when there is nothing to commit + - name: test workflow doesnt fail when there is nothing to commit uses: ./ with: source: tmp.txt @@ -96,9 +94,6 @@ jobs: deploy_key: ${{ secrets.DEPLOY_KEY }} commit_message: testing a custom commit message (${{ github.sha }}) - # Test that nothing to commit is OK - - # If the tests are successful, and we've merged to main, create a tag create_tag: diff --git a/README.md b/README.md index a79292d..55a49af 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,13 @@ # Commit with deploy key -Action to commit a file/directory to another repo using a deploy key. +Action to commit a file or directory to another repo using a deploy key. -## Example usage +## Usage * Create an SSH key pair to use for the commits -* Add the public key to your destination repo as a deploy key with push access +* Add the public key to your destination repo as a deploy key with write access * Add the private key to your source repo as a secret - +* Add this action to your workflow: ```yaml -uses: leigholiver/commit-with-deploy-key@v1.1.0 +uses: leigholiver/commit-with-deploy-key@v1.0.3 with: source: build_output destination_folder: dist @@ -16,30 +16,19 @@ with: ``` ## Inputs -### `source` -**Required** The file/directory to commit - -### `deploy_key` -**Required** Private SSH key to use for the commit. The public key must be added to the destination repostitory as a deploy key, with push access - -### `destination_repo` -**Required** Git repository to push changes to - -### `destination_folder` -Directory in the destination repo to push changes to (default `.`) - -### `destination_branch` -Branch in destination repo to push changes to (default `main`) - -### `delete_destination` -Delete destination directory contents before copy? (default `false`) - -### `git_username`/`git_email` -Github user to use for the commit (default: `${{ github.actor }}`) - -### `commit_message` -Commit message (default ` from `) +| Name | Required | Default | Description | +|--------------------- |--------- |----------------------------------- |---------------------------------------------------------| +| `source` | `true` | | The file/directory to commit | +| `deploy_key` | `true` | | Private SSH key to use for the commit. The public key must be added to the destination repostitory as a deploy key, with push access | +| `destination_repo` | `true` | | Git repository to push changes to | +| `destination_folder` | `false` | `.` | Directory in the destination repo to push changes to | +| `destination_branch` | `false` | `main` | Branch in destination repo to push changes to | +| `delete_destination` | `false` | `false` | Delete destination directory contents before copy? | +| `git_username` | `false` | `${{ github.actor }}` | Github user to use for the commit | +| `git_email` | `false` | `${{ github.actor }}` | Github user to use for the commit | +| `commit_message` | `false` | ` from ` | Commit message | ## Outputs -### `commit_hash` -SHA hash of the generated commit +| Name | Description | +|---------------|----------------------------------| +| `commit_hash` | SHA hash of the generated commit | diff --git a/VERSION b/VERSION index 795460f..e946d6b 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -v1.1.0 +v1.0.3