Skip to content

Commit bf3adea

Browse files
authored
feat: add support for SSH deploy keys (#64)
1 parent e65baf0 commit bf3adea

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

README.md

+34-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ jobs:
4949
name: "Bump version and create changelog with commitizen"
5050
steps:
5151
- name: Check out
52-
uses: actions/checkout@v2
52+
uses: actions/checkout@v3
5353
with:
5454
fetch-depth: 0
5555
token: "${{ secrets.GITHUB_TOKEN }}"
@@ -66,7 +66,8 @@ jobs:
6666
6767
| Name | Description | Default |
6868
| ------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------- |
69-
| `github_token` | Token for the repo. Can be passed in using `${{ secrets.GITHUB_TOKEN }}` **required** | - |
69+
| `github_token` | Token for the repo. Can be passed in using `${{ secrets.GITHUB_TOKEN }}`. Required if `use_ssh: false` | - |
70+
| `use_ssh` | Set to true if ssh-key has been configured for the `actions/checkout` | `false` |
7071
| `dry_run` | Run without creating commit, output to stdout | false |
7172
| `repository` | Repository name to push. Default or empty value represents current github repository | current one |
7273
| `branch` | Destination branch to push changes | Same as the one executing the action by default |
@@ -94,6 +95,37 @@ jobs:
9495

9596
The new version is also available as an environment variable under `REVISION` or you can access using `${{ steps.cz.outputs.version }}`
9697

98+
## Using SSH with deploy keys
99+
100+
1. Create a [deploy key](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/managing-deploy-keys#deploy-keys) (which is the SSH **public key**)
101+
2. Add the **private key** as a [Secret](https://docs.github.com/en/actions/security-guides/encrypted-secrets#creating-encrypted-secrets-for-a-repository) in your repository, e.g: `COMMIT_KEY`
102+
3. Set up your action
103+
104+
```yaml
105+
name: Bump version
106+
107+
on:
108+
push:
109+
branches:
110+
- main
111+
112+
jobs:
113+
bump-version:
114+
if: "!startsWith(github.event.head_commit.message, 'bump:')"
115+
runs-on: ubuntu-latest
116+
name: "Bump version and create changelog with commitizen"
117+
steps:
118+
- name: Check out
119+
uses: actions/checkout@v3
120+
with:
121+
fetch-depth: 0
122+
ssh-key: '${{ secrets.COMMIT_KEY }}'
123+
- name: Create bump and changelog
124+
uses: commitizen-tools/commitizen-action@master
125+
with:
126+
use_ssh: true
127+
```
128+
97129
## Troubleshooting
98130

99131
### Other actions are not triggered when the tag is pushed

action.yml

+5-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ inputs:
3636
required: false
3737
github_token:
3838
description: 'Token for the repo. Can be passed in using $\{{ secrets.GITHUB_TOKEN }}'
39-
required: true
39+
required: false
40+
use_ssh:
41+
description: 'Set to true if ssh-key has been configured for the actions/checkout'
42+
required: false
43+
default: "false"
4044
repository:
4145
description: 'Repository name to push. Default or empty value represents current github repository (${GITHUB_REPOSITORY})'
4246
default: ''

entrypoint.sh

+6-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ set -e
66
gpg --version
77
git --version
88

9-
if [[ -z $INPUT_GITHUB_TOKEN ]]; then
10-
echo 'Missing input "github_token: ${{ secrets.GITHUB_TOKEN }}".' >&2
9+
if [[ -z $INPUT_GITHUB_TOKEN && $INPUT_USE_SSH != "true" ]]; then
10+
echo 'Missing input "github_token: ${{ secrets.GITHUB_TOKEN }}" or "use_ssh", choose one.' >&2
1111
exit 1
1212
fi
1313

@@ -92,6 +92,10 @@ if [[ $INPUT_PUSH == 'true' ]]; then
9292
if [[ $INPUT_MERGE != 'true' && $GITHUB_EVENT_NAME == 'pull_request' ]]; then
9393
echo "Refusing to push on pull_request event since that would merge the pull request." >&2
9494
echo "You probably want to run on push to your default branch instead." >&2
95+
elif [[ $INPUT_USE_SSH == "true" ]]; then
96+
echo "Pushing to branch using SSH..."
97+
REMOTE_REPO="[email protected]:${INPUT_REPOSITORY}.git"
98+
git push "$REMOTE_REPO" "HEAD:${INPUT_BRANCH}" --tags
9599
else
96100
echo "Pushing to branch..."
97101
REMOTE_REPO="https://${GITHUB_ACTOR}:${INPUT_GITHUB_TOKEN}@github.com/${INPUT_REPOSITORY}.git"

0 commit comments

Comments
 (0)