Skip to content

Commit 04f0034

Browse files
committed
initial commit
0 parents  commit 04f0034

File tree

6 files changed

+243
-0
lines changed

6 files changed

+243
-0
lines changed

.github/workflows/test.yml

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
name: Test and Tag
2+
3+
on:
4+
push:
5+
branches:
6+
# explicit deny all here to make sure there isnt a commit loop
7+
- '!*'
8+
- main
9+
10+
jobs:
11+
test_and_tag:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Checkout
15+
uses: actions/checkout@v2
16+
17+
- name: check if tag already exists
18+
run: git fetch --depth=1 origin +refs/tags/*:refs/tags/* && git rev-parse "$(cat VERSION)" >/dev/null 2>&1 && exit 1 || exit 0
19+
20+
- name: create test file
21+
run: echo $(date) > tmp.txt
22+
23+
- name: create test directory
24+
run: mkdir -p tmp && cp tmp.txt tmp && echo "second cool file" > tmp/tmp2.txt
25+
26+
- name: test with file
27+
uses: ./
28+
id: filetest
29+
with:
30+
source: tmp.txt
31+
destination_repo: leigholiver/commit-with-deploy-key
32+
destination_branch: file_test
33+
deploy_key: ${{ secrets.DEPLOY_KEY }}
34+
35+
- name: test commit hash output
36+
run: echo "the commit hash for the file test is ${{ steps.filetest.outputs.commit_hash }}"
37+
38+
- name: test with directory
39+
uses: ./
40+
with:
41+
source: tmp
42+
destination_repo: leigholiver/commit-with-deploy-key
43+
destination_branch: directory_test
44+
deploy_key: ${{ secrets.DEPLOY_KEY }}
45+
46+
- name: test destination directory
47+
uses: ./
48+
with:
49+
source: tmp
50+
destination_repo: leigholiver/commit-with-deploy-key
51+
destination_branch: destination_directory_test
52+
destination_folder: destination
53+
deploy_key: ${{ secrets.DEPLOY_KEY }}
54+
55+
- name: test destination directory with removal
56+
uses: ./
57+
with:
58+
source: tmp
59+
destination_repo: leigholiver/commit-with-deploy-key
60+
destination_branch: destination_directory_test_with_delete
61+
delete_destination: true
62+
deploy_key: ${{ secrets.DEPLOY_KEY }}
63+
64+
- name: test with different username
65+
uses: ./
66+
with:
67+
source: tmp.txt
68+
destination_repo: leigholiver/commit-with-deploy-key
69+
destination_branch: username_test
70+
deploy_key: ${{ secrets.DEPLOY_KEY }}
71+
git_username: some different user
72+
73+
- name: test with different commit message
74+
uses: ./
75+
with:
76+
source: tmp.txt
77+
destination_repo: leigholiver/commit-with-deploy-key
78+
destination_branch: commit_message_test
79+
deploy_key: ${{ secrets.DEPLOY_KEY }}
80+
commit_message: testing a custom commit message (${{ github.sha }})
81+
82+
- name: create tag
83+
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

Dockerfile

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Container image that runs your code
2+
FROM alpine:3.10
3+
4+
RUN apk update && apk add git openssh-client
5+
6+
# Copies your code file from your action repository to the filesystem path `/` of the container
7+
COPY entrypoint.sh /entrypoint.sh
8+
9+
# Code file to execute when the docker container starts up (`entrypoint.sh`)
10+
ENTRYPOINT ["/entrypoint.sh"]

README.md

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Commit with deploy key
2+
Action to commit a file/directory to another repo using a deploy key.
3+
4+
## Example usage
5+
* Create an SSH key pair to use for the commits
6+
* Add the public key to your destination repo as a deploy key with push access
7+
* Add the private key to your source repo as a secret
8+
9+
```yaml
10+
uses: leigholiver/commit-with-deploy-key@v1.0.0
11+
with:
12+
source: build_output
13+
destination_folder: dist
14+
destination_repo: leigholiver/commit-with-deploy-key
15+
deploy_key: ${{ secrets.DEPLOY_KEY }}
16+
```
17+
18+
## Inputs
19+
### `source`
20+
**Required** The file/directory to commit
21+
22+
### `deploy_key`
23+
**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
24+
25+
### `destination_repo`
26+
**Required** Git repository to push changes to
27+
28+
### `destination_folder`
29+
Directory in the destination repo to push changes to (default `.`)
30+
31+
### `destination_branch`
32+
Branch in destination repo to push changes to (default `main`)
33+
34+
### `delete_destination`
35+
Delete destination directory contents before copy? (default `false`)
36+
37+
### `git_username`/`git_email`
38+
Github user to use for the commit (default: `${{ github.actor }}`)
39+
40+
### `commit_message`
41+
Commit message (default `<action name> from <commit hash>`)
42+
43+
## Outputs
44+
### `commit_hash`
45+
SHA hash of the generated commit

VERSION

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v1.0.0

action.yml

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
name: 'Commit with deploy key'
2+
description: 'Commit to another repository using a deploy key'
3+
inputs:
4+
source:
5+
description: 'The file/directory to commit'
6+
required: true
7+
deploy_key:
8+
description: 'Private SSH key. Public key must be added to the destination repostitory as a deploy key with push access'
9+
required: true
10+
destination_repo:
11+
description: 'Git repository to push changes to'
12+
required: true
13+
destination_folder:
14+
description: 'Directory in destination repo to push changes to'
15+
required: false
16+
default: '.'
17+
destination_branch:
18+
description: 'Branch in destination repo to push changes to (default `main`)'
19+
required: false
20+
default: 'main'
21+
delete_destination:
22+
description: 'Delete destination directory contents before copy? (default `false`)'
23+
required: false
24+
default: false
25+
git_username:
26+
description: 'Git username'
27+
required: false
28+
default: ${{ github.actor }}
29+
git_email:
30+
description: 'Git email address'
31+
required: false
32+
default: ${{ github.actor }}
33+
commit_message:
34+
description: 'Commit message'
35+
required: false
36+
default: "${{ github.job }} from https://github.com/${{ github.repository }}/commit/${{ github.sha }}"
37+
outputs:
38+
commit_hash:
39+
description: 'The SHA hash of the generated commit'
40+
runs:
41+
using: 'docker'
42+
image: 'Dockerfile'
43+
args:
44+
- ${{ inputs.source }}
45+
- ${{ inputs.destination_repo }}
46+
- ${{ inputs.destination_folder }}
47+
- ${{ inputs.delete_destination }}
48+
- ${{ inputs.deploy_key }}
49+
- ${{ inputs.git_username }}
50+
- ${{ inputs.git_email }}
51+
- ${{ inputs.commit_message }}

entrypoint.sh

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/bin/sh -l
2+
set -e
3+
4+
if [ ! -f "${INPUT_SOURCE}" ] && [ ! -d "${INPUT_SOURCE}" ]; then
5+
echo "No source specified"
6+
exit 1
7+
fi
8+
9+
# set up some directories
10+
CALLING_DIR=$(pwd)
11+
WORKING_DIR=$(mktemp -d)
12+
REAL_DESTINATION=$(realpath $WORKING_DIR/$INPUT_DESTINATION_FOLDER)/
13+
14+
# set up the github deploy key
15+
mkdir -p ~/.ssh
16+
echo "${INPUT_DEPLOY_KEY}" > ~/.ssh/id_rsa
17+
chmod 600 ~/.ssh/id_rsa
18+
19+
# set up git
20+
git config --global user.name "${INPUT_GIT_USERNAME}"
21+
git config --global user.email "${INPUT_GIT_EMAIL}"
22+
ssh-keyscan -H github.com > ~/.ssh/known_hosts
23+
GIT_SSH='ssh -i /github/home/.ssh/id_rsa -o UserKnownHostsFile=/github/home/.ssh/known_hosts'
24+
25+
# clone the repo into our working directory and cd to it
26+
GIT_SSH_COMMAND=$GIT_SSH git clone git@github.com:$INPUT_DESTINATION_REPO.git $WORKING_DIR
27+
cd $WORKING_DIR
28+
29+
# checkout the destination branch, creating it if it doesn't exist
30+
git checkout $INPUT_DESTINATION_BRANCH || git checkout -b $INPUT_DESTINATION_BRANCH
31+
32+
# ensure destination directory exists, and is emptied if appropriate
33+
mkdir -p $REAL_DESTINATION
34+
cd $REAL_DESTINATION
35+
if [ "${INPUT_DELETE_DESTINATION}" = "true" ]; then
36+
git rm -rf .
37+
fi
38+
39+
# do the copy
40+
if [ -f $CALLING_DIR/$INPUT_SOURCE ]; then
41+
cp -a $CALLING_DIR/$INPUT_SOURCE .
42+
elif [ -d $CALLING_DIR/$INPUT_SOURCE ]; then
43+
cp -a $CALLING_DIR/$INPUT_SOURCE/* .
44+
fi
45+
46+
# commit and push
47+
git add .
48+
git commit -m "${INPUT_COMMIT_MESSAGE}"
49+
GIT_SSH_COMMAND=$GIT_SSH git push -u origin $INPUT_DESTINATION_BRANCH
50+
51+
# output the commit hash
52+
echo "::set-output name=commit_hash::$(git rev-parse HEAD)"
53+
exit 0

0 commit comments

Comments
 (0)