Skip to content

Commit

Permalink
chore: remove redundant safe-directory configuration
Browse files Browse the repository at this point in the history
This has been resolved in the [checkout](actions/checkout#770) action and would already be set by default.

Update README.md

Fix removed code lines

* Add force with lease support
* Update the documentation

fix: github_token and github_url optional

Add atomic push

Since time elapses between the checkout the github workflow performs and the eventual push this action invokes the remote HEAD may have changed. If this is the case the HEAD update will be rejected but any tag (and their commits) will be pushed.

In general I think this operation should be atomic, either we push everything or we push nothing.

Force pushes still work the way you would expect (i.e. if we force the HEAD update with --atomic everything is still pushed)

This also protects from the situation where someone else has seized your tag name in the meantime but not updated your HEAD.

See git docs for more information - https://git-scm.com/docs/git-push#Documentation/git-push.txt---no-atomic

Add option to control atomic switch

Use node16 to run action

Node.js 12 actions are deprecated. For more information see: https://github.blog/changelog/2022-09-22-github-actions-all-actions-will-begin-running-on-node16-instead-of-node12/.

Update the documentation and the push process

feat: Restructure the code

Update the documentation and the push process
  • Loading branch information
jackton1 authored and ZPascal committed Oct 23, 2022
1 parent e69c0c0 commit 8b7f16d
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 13 deletions.
77 changes: 68 additions & 9 deletions README.md
Expand Up @@ -39,6 +39,53 @@ jobs:
branch: ${{ github.ref }}
```

An example workflow to use the force-with-lease parameter to force push to a repository:

```yaml
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
ref: ${{ github.head_ref }}
fetch-depth: 0
- name: Commit files
run: |
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git commit -m "Add changes" -a
- name: Push changes
uses: ad-m/github-push-action@master
with:
force_with_lease: true
```

An example workflow to update/ overwrite an existing tag:

```yaml
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
ref: ${{ github.head_ref }}
fetch-depth: 0
- name: Commit files
run: |
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git tag -d $GITHUB_REF_NAME
git tag $GITHUB_REF_NAME
git commit -m "Add changes" -a
- name: Push changes
uses: ad-m/github-push-action@master
with:
force: true
tags: true
```

An example workflow to authenticate with GitHub Platform via Deploy Keys or in general SSH:

```yaml
Expand Down Expand Up @@ -67,18 +114,21 @@ jobs:

### Inputs

| name | value | default | description |
| ---- | ----- | ------- | ----------- |
| github_token | string | `${{ github.token }}` | [GITHUB_TOKEN](https://docs.github.com/en/actions/security-guides/automatic-token-authentication#using-the-github_token-in-a-workflow) <br /> or a repo scoped <br /> [Personal Access Token](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token). |
| branch | string | (default) | Destination branch to push changes. <br /> Can be passed in using `${{ github.ref }}`. |
| force | boolean | false | Determines if force push is used. |
| tags | boolean | false | Determines if `--tags` is used. |
| directory | string | '.' | Directory to change to before pushing. |
| repository | string | '' | Repository name. <br /> Default or empty repository name represents <br /> current github repository. <br /> If you want to push to other repository, <br /> you should make a [personal access token](https://github.com/settings/tokens) <br /> and use it as the `github_token` input. |
| name | value | default | description |
|------------------| ----- | ------- |-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| github_token | string | `${{ github.token }}` | [GITHUB_TOKEN](https://docs.github.com/en/actions/security-guides/automatic-token-authentication#using-the-github_token-in-a-workflow) <br /> or a repo scoped <br /> [Personal Access Token](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token). |
| ssh | boolean | false | Determines if ssh/ Deploy Keys is used. |
| branch | string | (default) | Destination branch to push changes. <br /> Can be passed in using `${{ github.ref }}`. |
| force | boolean | false | Determines if force push is used. |
| force_with_lease | boolean | false | Determines if force-with-lease push is used. Please specify the corresponding branch inside `ref` section of the checkout action e.g. `ref: ${{ github.head_ref }}`. |
| atomic | boolean | true | Determines if [atomic](https://git-scm.com/docs/git-push#Documentation/git-push.txt---no-atomic) push is used. |
| tags | boolean | false | Determines if `--tags` is used. |
| directory | string | '.' | Directory to change to before pushing. |
| repository | string | '' | Repository name. <br /> Default or empty repository name represents <br /> current github repository. <br /> If you want to push to other repository, <br /> you should make a [personal access token](https://github.com/settings/tokens) <br /> and use it as the `github_token` input. |

## Troubeshooting

Please be aware, if your job fails and the corresponding output log looks like the following error, update your used verson of the action to `ad-m/github-push-action@master`:
Please be aware, if your job fails and the corresponding output log looks like the following error, update your used version of the action to `ad-m/github-push-action@master`:
```log
Push to branch ***************
fatal: unsafe repository ('/github/workspace' is owned by someone else)
Expand All @@ -87,6 +137,15 @@ To add an exception for this directory, call:
git config --global --add safe.directory /github/workspace
```

If you see the following error inside the output of the job, and you want to update an existing Tag:
```log
To https://github.com/Test/test_repository
! [rejected] 0.0.9 -> 0.0.9 (stale info)
error: failed to push some refs to 'https://github.com/Test/test_repository'
```

Please use the `force` instead the `force_with_lease` parameter. The update of the tag is with the `--force-with-lease` parameter not possible.

## License

The Dockerfile and associated scripts and documentation in this project are released under the [MIT License](LICENSE).
Expand Down
12 changes: 9 additions & 3 deletions action.yml
Expand Up @@ -7,11 +7,11 @@ branding:
inputs:
github_token:
description: 'GitHub token or PAT token'
required: true
required: false
default: ${{ github.token }}
github_url:
description: 'GitHub url or GitHub Enterprise url'
required: true
required: false
default: ${{ github.server_url }}
ssh:
description: 'Specify if ssh should be used'
Expand All @@ -26,6 +26,12 @@ inputs:
force:
description: 'Determines if force push is used'
required: false
force_with_lease:
description: 'Determines if force-with-lease push is used'
required: false
atomic:
description: 'Determines if atomic push is used, default true'
required: false
tags:
description: 'Determines if --tags is used'
required: false
Expand All @@ -34,5 +40,5 @@ inputs:
required: false
default: '.'
runs:
using: 'node12'
using: 'node16'
main: 'start.js'
20 changes: 19 additions & 1 deletion start.sh
Expand Up @@ -2,6 +2,7 @@
set -e

INPUT_FORCE=${INPUT_FORCE:-false}
INPUT_FORCE_WITH_LEASE=${INPUT_FORCE_WITH_LEASE:-false}
INPUT_SSH=${INPUT_SSH:-false}
INPUT_TAGS=${INPUT_TAGS:-false}
INPUT_DIRECTORY=${INPUT_DIRECTORY:-'.'}
Expand All @@ -14,6 +15,19 @@ echo "Push to branch $INPUT_BRANCH";
exit 1;
};

if ${INPUT_FORCE} && ${INPUT_FORCE_WITH_LEASE}; then
echo 'Please, specify only force or force_with_lease and not both.';
exit 1;
fi

if ${INPUT_FORCE}; then
_FORCE_OPTION='--force'
fi

if ${INPUT_FORCE_WITH_LEASE}; then
_FORCE_OPTION='--force-with-lease'
fi

if ${INPUT_TAGS}; then
_TAGS='--tags'
fi
Expand All @@ -28,4 +42,8 @@ fi

git config --local --add safe.directory ${INPUT_DIRECTORY}

git push "${remote_repo}" HEAD:${INPUT_BRANCH} --follow-tags $_FORCE_OPTION $_TAGS;
if ! ${INPUT_FORCE_WITH_LEASE}; then
ADDITIONAL_PARAMETERS="${remote_repo} HEAD:${INPUT_BRANCH}"
fi

git push $ADDITIONAL_PARAMETERS --follow-tags $_FORCE_OPTION $_TAGS;

0 comments on commit 8b7f16d

Please sign in to comment.