From e69c0c047e4c4480c33eed11c1d5246e8c59b8a0 Mon Sep 17 00:00:00 2001 From: Pascal Zimmermann Date: Wed, 15 Jun 2022 13:46:08 +0200 Subject: [PATCH] Add deploy key functionality (#120) * Add deploy key functionality --- .github/workflows/build.yml | 18 +++++++++--------- README.md | 26 ++++++++++++++++++++++++++ action.yml | 3 +++ start.sh | 12 +++++++----- 4 files changed, 45 insertions(+), 14 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 89b166a..0819c9d 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -7,14 +7,14 @@ on: jobs: build: - + runs-on: ubuntu-latest - + steps: - - uses: actions/checkout@master - - name: Verify action syntax - # The action should not publish any real changes, but should succeed. - uses: './' - with: - github_token: '${{ secrets.GITHUB_TOKEN }}' - branch: '${{ github.ref }}' + - uses: actions/checkout@master + - name: Verify action syntax + # The action should not publish any real changes, but should succeed. + uses: './' + with: + github_token: '${{ secrets.GITHUB_TOKEN }}' + branch: '${{ github.ref }}' \ No newline at end of file diff --git a/README.md b/README.md index e1c30fa..092b27d 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,32 @@ jobs: branch: ${{ github.ref }} ``` +An example workflow to authenticate with GitHub Platform via Deploy Keys or in general SSH: + +```yaml +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: + ssh-key: ${{ secrets.SSH_PRIVATE_KEY }} + persist-credentials: true + - name: Create local changes + run: | + ... + - name: Commit files + run: | + git config --local user.email "41898282+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: + ssh: true + branch: ${{ github.ref }} +``` + ### Inputs | name | value | default | description | diff --git a/action.yml b/action.yml index d382fad..1f3d910 100644 --- a/action.yml +++ b/action.yml @@ -13,6 +13,9 @@ inputs: description: 'GitHub url or GitHub Enterprise url' required: true default: ${{ github.server_url }} + ssh: + description: 'Specify if ssh should be used' + required: false repository: description: 'Repository name to push. Default or empty value represents current github repository (${GITHUB_REPOSITORY})' default: '' diff --git a/start.sh b/start.sh index 5faf53a..c14e139 100755 --- a/start.sh +++ b/start.sh @@ -2,6 +2,7 @@ set -e INPUT_FORCE=${INPUT_FORCE:-false} +INPUT_SSH=${INPUT_SSH:-false} INPUT_TAGS=${INPUT_TAGS:-false} INPUT_DIRECTORY=${INPUT_DIRECTORY:-'.'} _FORCE_OPTION='' @@ -13,17 +14,18 @@ echo "Push to branch $INPUT_BRANCH"; exit 1; }; -if ${INPUT_FORCE}; then - _FORCE_OPTION='--force' -fi - if ${INPUT_TAGS}; then _TAGS='--tags' fi cd ${INPUT_DIRECTORY} -remote_repo="${INPUT_GITHUB_URL_PROTOCOL}//${GITHUB_ACTOR}:${INPUT_GITHUB_TOKEN}@${INPUT_GITHUB_URL}/${REPOSITORY}.git" +if ${INPUT_SSH}; then + remote_repo="git@${INPUT_GITHUB_URL}:${REPOSITORY}.git" +else + remote_repo="${INPUT_GITHUB_URL_PROTOCOL}//${GITHUB_ACTOR}:${INPUT_GITHUB_TOKEN}@${INPUT_GITHUB_URL}/${REPOSITORY}.git" +fi + git config --local --add safe.directory ${INPUT_DIRECTORY} git push "${remote_repo}" HEAD:${INPUT_BRANCH} --follow-tags $_FORCE_OPTION $_TAGS;