From 3fe53a7d31deb46d918465ece03d19456437f72b Mon Sep 17 00:00:00 2001 From: Stefan Zweifel Date: Tue, 20 Aug 2019 20:43:07 +0200 Subject: [PATCH 01/15] Add actions.yml --- actions.yml | 22 ++++++++++++++++++++++ entrypoint.sh | 2 +- 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 actions.yml diff --git a/actions.yml b/actions.yml new file mode 100644 index 0000000..fd3ab09 --- /dev/null +++ b/actions.yml @@ -0,0 +1,22 @@ +name: Git Auto Commit +description: 'Automatically commits files which have been changed.' + +inputs: + commit_message: + description: 'Commit message' + required: true + commit_author_name: + description: 'Name of the commit author' + required: true + commit_author_email: + description: 'Email address of the commit author' + required: true + +runs: + using: 'docker' + image: 'Dockerfile' + entrypoint: 'entrypoint.sh' + +branding: + icon: 'git-commit' + color: orange diff --git a/entrypoint.sh b/entrypoint.sh index 85b3cf6..b334a5d 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -6,5 +6,5 @@ git config --global user.name "GitHub Actions" git add -A git status -git commit -m "$COMMIT_MESSAGE" --author="$COMMIT_AUTHOR_NAME <$COMMIT_AUTHOR_EMAIL>" || echo "No changes found. Nothing to commit." +git commit -m "$INPUT_COMMIT_MESSAGE" --author="$INPUT_COMMIT_AUTHOR_NAME <$INPUTCOMMIT_AUTHOR_EMAIL>" || echo "No changes found. Nothing to commit." git push -u origin HEAD From 6057956931c563c8c2c57b31f96a3f82fb491afe Mon Sep 17 00:00:00 2001 From: Stefan Zweifel Date: Tue, 20 Aug 2019 20:51:00 +0200 Subject: [PATCH 02/15] Fix Typo --- entrypoint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entrypoint.sh b/entrypoint.sh index b334a5d..df9287d 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -6,5 +6,5 @@ git config --global user.name "GitHub Actions" git add -A git status -git commit -m "$INPUT_COMMIT_MESSAGE" --author="$INPUT_COMMIT_AUTHOR_NAME <$INPUTCOMMIT_AUTHOR_EMAIL>" || echo "No changes found. Nothing to commit." +git commit -m "$INPUT_COMMIT_MESSAGE" --author="$INPUT_COMMIT_AUTHOR_NAME <$INPUT_COMMIT_AUTHOR_EMAIL>" || echo "No changes found. Nothing to commit." git push -u origin HEAD From 8620a4b74426edfd13c233462bbd523d95a7f065 Mon Sep 17 00:00:00 2001 From: Stefan Zweifel Date: Tue, 20 Aug 2019 21:05:10 +0200 Subject: [PATCH 03/15] Update README --- README.md | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index d5dab16..23ba55a 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ The most common use case for this, is when you're running a Linter or Code-Style In this example I'm running `php-cs-fixer` in a PHP project. + ```terraform workflow "php-cs-fixer" { on = "push" @@ -39,17 +40,33 @@ action "auto-commit-php-cs-fixer" { } ``` + +---- + +New GitHub Actions syntax: + +```yaml +- uses: stefanzweifel/git-auto-commit-action@dev + with: + commit_author_email: john.doe@example.com + commit_author_name: John Doe + commit_message: Apply automatic changes + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} +``` + + ## Secrets The `GITHUB_TOKEN` secret is required. Add the secret in the Workflow Editor on github.com. -## Environment variables +## Inputs -The following environment variables are required: +The following inputs are required -- `COMMIT_MESSAGE`: The commit message used when changes are available -- `COMMIT_AUTHOR_EMAIL`: The Commit Authors Email Address -- `COMMIT_AUTHOR_NAME`: The Commit Authors Name +- `commit_author_email`: The commit message used when changes are available +- `commit_author_name`: The Commit Authors Email Address +- `commit_message`: The Commit Authors Name ## Versioning From 1dd709d949e780cc591495df145f0394307c9187 Mon Sep 17 00:00:00 2001 From: Stefan Zweifel Date: Tue, 20 Aug 2019 21:05:17 +0200 Subject: [PATCH 04/15] Update Dockerfile --- Dockerfile | 9 --------- actions.yml | 2 ++ 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/Dockerfile b/Dockerfile index 583904d..e8da79c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,14 +1,5 @@ FROM alpine/git:1.0.7 -LABEL "com.github.actions.name"="Auto Commit changed files" -LABEL "com.github.actions.description"="Auotmatically commit changed files and push changes back to remote repository." -LABEL "com.github.actions.icon"="git-commit" -LABEL "com.github.actions.color"="orange" - -LABEL "repository"="http://github.com/stefanzweifel/git-auto-commit-action" -LABEL "homepage"="http://github.com/stefanzweifel/git-auto-commit-action" -LABEL "maintainer"="Stefan Zweifel " - COPY entrypoint.sh /entrypoint.sh ENTRYPOINT ["sh", "/entrypoint.sh"] diff --git a/actions.yml b/actions.yml index fd3ab09..2e87334 100644 --- a/actions.yml +++ b/actions.yml @@ -1,6 +1,8 @@ name: Git Auto Commit description: 'Automatically commits files which have been changed.' +author: Stefan Zweifel + inputs: commit_message: description: 'Commit message' From 6280a31960ebe604a98fe5e2adb3c31fc6f68e41 Mon Sep 17 00:00:00 2001 From: Stefan Zweifel Date: Tue, 20 Aug 2019 21:12:28 +0200 Subject: [PATCH 05/15] Remove entrypoint --- actions.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/actions.yml b/actions.yml index 2e87334..84af776 100644 --- a/actions.yml +++ b/actions.yml @@ -17,7 +17,6 @@ inputs: runs: using: 'docker' image: 'Dockerfile' - entrypoint: 'entrypoint.sh' branding: icon: 'git-commit' From fdf062fc1c7d8cfbbdadf8a0dab388609d39bedc Mon Sep 17 00:00:00 2001 From: Stefan Zweifel Date: Tue, 27 Aug 2019 20:26:44 +0200 Subject: [PATCH 06/15] Switch to branch and setup remote origin --- README.md | 1 + entrypoint.sh | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/README.md b/README.md index 23ba55a..e4e5f24 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,7 @@ New GitHub Actions syntax: commit_author_name: John Doe commit_message: Apply automatic changes env: + TOKEN: ${{ secrets.TOKEN }} # Personal Access Token GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} ``` diff --git a/entrypoint.sh b/entrypoint.sh index df9287d..073ed4a 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,6 +1,12 @@ #!/bin/sh set -eu +# Switch to branch from current Workflow run +git switch "${GITHUB_REF:11}" + +# Set origin URL +git remote set-url origin https://$TOKEN:x-oauth-basic@github.com/$GITHUB_REPOSITORY + git config --global user.email "actions@github.com" git config --global user.name "GitHub Actions" From a93a9a308d7ab4037c17628e5d0891cd9bc71808 Mon Sep 17 00:00:00 2001 From: Stefan Zweifel Date: Tue, 27 Aug 2019 20:30:31 +0200 Subject: [PATCH 07/15] Use git checkout --- entrypoint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entrypoint.sh b/entrypoint.sh index 073ed4a..419e8a6 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -2,7 +2,7 @@ set -eu # Switch to branch from current Workflow run -git switch "${GITHUB_REF:11}" +git checkout "${GITHUB_REF:11}" # Set origin URL git remote set-url origin https://$TOKEN:x-oauth-basic@github.com/$GITHUB_REPOSITORY From 4b2201fcd1984de36ce06e31b5eafaaa63eacec0 Mon Sep 17 00:00:00 2001 From: Stefan Zweifel Date: Sat, 31 Aug 2019 18:02:55 +0200 Subject: [PATCH 08/15] Update Entrypoint --- entrypoint.sh | 54 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 46 insertions(+), 8 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index 419e8a6..33468e7 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -2,15 +2,53 @@ set -eu # Switch to branch from current Workflow run -git checkout "${GITHUB_REF:11}" +# git checkout "${GITHUB_REF:11}" # Set origin URL -git remote set-url origin https://$TOKEN:x-oauth-basic@github.com/$GITHUB_REPOSITORY +# git remote set-url origin https://$TOKEN:x-oauth-basic@github.com/$GITHUB_REPOSITORY -git config --global user.email "actions@github.com" -git config --global user.name "GitHub Actions" +# git config --global user.email "actions@github.com" +# git config --global user.name "GitHub Actions" -git add -A -git status -git commit -m "$INPUT_COMMIT_MESSAGE" --author="$INPUT_COMMIT_AUTHOR_NAME <$INPUT_COMMIT_AUTHOR_EMAIL>" || echo "No changes found. Nothing to commit." -git push -u origin HEAD +# git add -A +# git status +# git commit -m "$INPUT_COMMIT_MESSAGE" --author="$INPUT_COMMIT_AUTHOR_NAME <$INPUT_COMMIT_AUTHOR_EMAIL>" || echo "No changes found. Nothing to commit." +# git push -u origin HEAD + + + +# Set up .netrc file with GitHub credentials +git_setup ( ) { + cat <<- EOF > $HOME/.netrc + machine github.com + login $GITHUB_ACTOR + password $GITHUB_TOKEN + + machine api.github.com + login $GITHUB_ACTOR + password $GITHUB_TOKEN +EOF + chmod 600 $HOME/.netrc + + # Git requires our "name" and email address -- use GitHub handle + git config user.email "$GITHUB_ACTOR@users.noreply.github.com" + git config user.name "$GITHUB_ACTOR" + + # Push to the current branch if PUSH_BRANCH hasn't been overriden + : ${PUSH_BRANCH:=`echo "$GITHUB_REF" | awk -F / '{ print $3 }' `} +} + + +# This section only runs if there have been file changes +echo "Checking for uncommitted changes in the git working tree." +if ! git diff --quiet +then + git_setup + + git checkout $PUSH_BRANCH + git add . + git commit -m "$INPUT_COMMIT_MESSAGE" --author="$INPUT_COMMIT_AUTHOR_NAME <$INPUT_COMMIT_AUTHOR_EMAIL>" || echo "No changes found. Nothing to commit." + git push --set-upstream origin $PUSH_BRANCH +else + echo "Working tree clean. Nothing to commit." +fi From fbe300afdfecf4495bf1e80911192bec96f73c08 Mon Sep 17 00:00:00 2001 From: Stefan Zweifel Date: Sat, 31 Aug 2019 18:07:25 +0200 Subject: [PATCH 09/15] Tinker tinker --- entrypoint.sh | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index 33468e7..1b27752 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -35,7 +35,7 @@ EOF git config user.name "$GITHUB_ACTOR" # Push to the current branch if PUSH_BRANCH hasn't been overriden - : ${PUSH_BRANCH:=`echo "$GITHUB_REF" | awk -F / '{ print $3 }' `} + # : ${PUSH_BRANCH:=`echo "$GITHUB_REF" | awk -F / '{ print $3 }' `} } @@ -45,10 +45,12 @@ if ! git diff --quiet then git_setup - git checkout $PUSH_BRANCH + git checkout "${GITHUB_REF:11}" + # git checkout $PUSH_BRANCH git add . git commit -m "$INPUT_COMMIT_MESSAGE" --author="$INPUT_COMMIT_AUTHOR_NAME <$INPUT_COMMIT_AUTHOR_EMAIL>" || echo "No changes found. Nothing to commit." - git push --set-upstream origin $PUSH_BRANCH + git push --set-upstream origin "${GITHUB_REF:11}" + # git push --set-upstream origin $PUSH_BRANCH else echo "Working tree clean. Nothing to commit." fi From ef070dad0586d01cbcbdcc70b70d94e376bd421a Mon Sep 17 00:00:00 2001 From: Stefan Zweifel Date: Sat, 31 Aug 2019 18:10:59 +0200 Subject: [PATCH 10/15] Updates --- entrypoint.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/entrypoint.sh b/entrypoint.sh index 1b27752..68ecc49 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -49,7 +49,8 @@ then # git checkout $PUSH_BRANCH git add . git commit -m "$INPUT_COMMIT_MESSAGE" --author="$INPUT_COMMIT_AUTHOR_NAME <$INPUT_COMMIT_AUTHOR_EMAIL>" || echo "No changes found. Nothing to commit." - git push --set-upstream origin "${GITHUB_REF:11}" + git push -u origin HEAD + # git push --set-upstream origin "${GITHUB_REF:11}" # git push --set-upstream origin $PUSH_BRANCH else echo "Working tree clean. Nothing to commit." From 49fa6b4e7a8d92bae68874442ecbc3f78f7a97c7 Mon Sep 17 00:00:00 2001 From: Stefan Zweifel Date: Sat, 31 Aug 2019 18:14:28 +0200 Subject: [PATCH 11/15] WIP --- entrypoint.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index 68ecc49..1b27752 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -49,8 +49,7 @@ then # git checkout $PUSH_BRANCH git add . git commit -m "$INPUT_COMMIT_MESSAGE" --author="$INPUT_COMMIT_AUTHOR_NAME <$INPUT_COMMIT_AUTHOR_EMAIL>" || echo "No changes found. Nothing to commit." - git push -u origin HEAD - # git push --set-upstream origin "${GITHUB_REF:11}" + git push --set-upstream origin "${GITHUB_REF:11}" # git push --set-upstream origin $PUSH_BRANCH else echo "Working tree clean. Nothing to commit." From 8d47eb33c597ad26c347ecdc55c756742ca8732c Mon Sep 17 00:00:00 2001 From: Stefan Zweifel Date: Sat, 31 Aug 2019 18:15:13 +0200 Subject: [PATCH 12/15] Use Actions as Git User --- entrypoint.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index 1b27752..fd3aeb5 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -7,8 +7,8 @@ set -eu # Set origin URL # git remote set-url origin https://$TOKEN:x-oauth-basic@github.com/$GITHUB_REPOSITORY -# git config --global user.email "actions@github.com" -# git config --global user.name "GitHub Actions" +git config --global user.email "actions@github.com" +git config --global user.name "GitHub Actions" # git add -A # git status @@ -31,8 +31,8 @@ EOF chmod 600 $HOME/.netrc # Git requires our "name" and email address -- use GitHub handle - git config user.email "$GITHUB_ACTOR@users.noreply.github.com" - git config user.name "$GITHUB_ACTOR" + # git config user.email "$GITHUB_ACTOR@users.noreply.github.com" + # git config user.name "$GITHUB_ACTOR" # Push to the current branch if PUSH_BRANCH hasn't been overriden # : ${PUSH_BRANCH:=`echo "$GITHUB_REF" | awk -F / '{ print $3 }' `} From 0364ecfe57919e82e6e57cb9d28297caa71e5fbb Mon Sep 17 00:00:00 2001 From: Stefan Zweifel Date: Sat, 31 Aug 2019 18:34:01 +0200 Subject: [PATCH 13/15] Update Entrypoint --- entrypoint.sh | 34 ++++++++-------------------------- 1 file changed, 8 insertions(+), 26 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index fd3aeb5..58fe291 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,22 +1,6 @@ #!/bin/sh set -eu -# Switch to branch from current Workflow run -# git checkout "${GITHUB_REF:11}" - -# Set origin URL -# git remote set-url origin https://$TOKEN:x-oauth-basic@github.com/$GITHUB_REPOSITORY - -git config --global user.email "actions@github.com" -git config --global user.name "GitHub Actions" - -# git add -A -# git status -# git commit -m "$INPUT_COMMIT_MESSAGE" --author="$INPUT_COMMIT_AUTHOR_NAME <$INPUT_COMMIT_AUTHOR_EMAIL>" || echo "No changes found. Nothing to commit." -# git push -u origin HEAD - - - # Set up .netrc file with GitHub credentials git_setup ( ) { cat <<- EOF > $HOME/.netrc @@ -28,14 +12,10 @@ git_setup ( ) { login $GITHUB_ACTOR password $GITHUB_TOKEN EOF - chmod 600 $HOME/.netrc + chmod 600 $HOME/.netrc - # Git requires our "name" and email address -- use GitHub handle - # git config user.email "$GITHUB_ACTOR@users.noreply.github.com" - # git config user.name "$GITHUB_ACTOR" - - # Push to the current branch if PUSH_BRANCH hasn't been overriden - # : ${PUSH_BRANCH:=`echo "$GITHUB_REF" | awk -F / '{ print $3 }' `} + git config --global user.email "actions@github.com" + git config --global user.name "GitHub Actions" } @@ -45,12 +25,14 @@ if ! git diff --quiet then git_setup + # Switch to branch from current Workflow run git checkout "${GITHUB_REF:11}" - # git checkout $PUSH_BRANCH + git add . - git commit -m "$INPUT_COMMIT_MESSAGE" --author="$INPUT_COMMIT_AUTHOR_NAME <$INPUT_COMMIT_AUTHOR_EMAIL>" || echo "No changes found. Nothing to commit." + + git commit -m "$INPUT_COMMIT_MESSAGE" --author="$INPUT_COMMIT_AUTHOR_NAME <$INPUT_COMMIT_AUTHOR_EMAIL>" + git push --set-upstream origin "${GITHUB_REF:11}" - # git push --set-upstream origin $PUSH_BRANCH else echo "Working tree clean. Nothing to commit." fi From e14499263cc6c81f8cf517471c1f8c21ba924d13 Mon Sep 17 00:00:00 2001 From: Stefan Zweifel Date: Sat, 31 Aug 2019 18:34:50 +0200 Subject: [PATCH 14/15] Add GH Actions Stuff to Dockerfile --- Dockerfile | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Dockerfile b/Dockerfile index e8da79c..583904d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,14 @@ FROM alpine/git:1.0.7 +LABEL "com.github.actions.name"="Auto Commit changed files" +LABEL "com.github.actions.description"="Auotmatically commit changed files and push changes back to remote repository." +LABEL "com.github.actions.icon"="git-commit" +LABEL "com.github.actions.color"="orange" + +LABEL "repository"="http://github.com/stefanzweifel/git-auto-commit-action" +LABEL "homepage"="http://github.com/stefanzweifel/git-auto-commit-action" +LABEL "maintainer"="Stefan Zweifel " + COPY entrypoint.sh /entrypoint.sh ENTRYPOINT ["sh", "/entrypoint.sh"] From fc34a4d3a261890c4d63fdf43c5a75c4b89afcde Mon Sep 17 00:00:00 2001 From: Stefan Zweifel Date: Sat, 31 Aug 2019 18:43:23 +0200 Subject: [PATCH 15/15] Update README --- README.md | 83 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 42 insertions(+), 41 deletions(-) diff --git a/README.md b/README.md index e4e5f24..65869ef 100644 --- a/README.md +++ b/README.md @@ -1,49 +1,16 @@ # git-auto-commit-action This GitHub Action automatically commits files which have been changed during a Workflow run and pushes the Commit back to GitHub. -The Committer is "GitHub Actions " and the Author of the Commit can be configured with environment variables. +The Committer is "GitHub Actions " and the Author of the Commit can be configured with input variables. If no changes are available, the Actions does nothing. This Action has been inspired and adapted from the [auto-commit](https://github.com/cds-snc/github-actions/tree/master/auto-commit -)-Action of the Canadian Digital Service. +)-Action of the Canadian Digital Service and the [commit](https://github.com/elstudio/actions-js-build/blob/41d604d6e73d632e22eac40df8cc69b5added04b/commit/entrypoint.sh)-Action by Eric Johnson. ## Usage -You have to have an Action in your Workflow, which changes some of your project files. -The most common use case for this, is when you're running a Linter or Code-Style fixer on GitHub Actions. - -In this example I'm running `php-cs-fixer` in a PHP project. - - -```terraform -workflow "php-cs-fixer" { - on = "push" - resolves = [ - "auto-commit-php-cs-fixer" - ] -} - -action "php-cs-fixer" { - uses = "docker://oskarstark/php-cs-fixer-ga" -} - -action "auto-commit-php-cs-fixer" { - needs = ["php-cs-fixer"] - uses = "stefanzweifel/git-auto-commit-action@v1.0.0" - secrets = ["GITHUB_TOKEN"] - env = { - COMMIT_MESSAGE = "Apply php-cs-fixer changes" - COMMIT_AUTHOR_EMAIL = "john.doe@example.com" - COMMIT_AUTHOR_NAME = "John Doe" - } -} -``` - - ----- - -New GitHub Actions syntax: +Add the following step at the end of your job. ```yaml - uses: stefanzweifel/git-auto-commit-action@dev @@ -52,16 +19,13 @@ New GitHub Actions syntax: commit_author_name: John Doe commit_message: Apply automatic changes env: - TOKEN: ${{ secrets.TOKEN }} # Personal Access Token GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} ``` +The Action will only commit files back, if changes are available. The resulting commit **will not trigger** another GitHub Actions Workflow run! -## Secrets -The `GITHUB_TOKEN` secret is required. Add the secret in the Workflow Editor on github.com. - -## Inputs +### Inputs The following inputs are required @@ -69,6 +33,43 @@ The following inputs are required - `commit_author_name`: The Commit Authors Email Address - `commit_message`: The Commit Authors Name +### Environment Variables + +The `GITHUB_TOKEN` secret is required. It is automatically available in your repository. You have to add it to the configuration though. + +## Example Usage + +This Action will only work, if the job in your workflow changes project files. +The most common use case for this, is when you're running a Linter or Code-Style fixer on GitHub Actions. + +In this example I'm running `php-cs-fixer` in a PHP project. + + +```yaml +on: push +name: php-cs-fixer +jobs: + php-cs-fixer: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v1 + with: + fetch-depth: 1 + + - name: Run php-cs-fixer + uses: docker://oskarstark/php-cs-fixer-ga + + - name: Commit changed files + uses: stefanzweifel/git-auto-commit-action@v2.0.0 + with: + commit_author_email: hello@stefanzweifel.io + commit_author_name: Stefan Zweifel + commit_message: Apply php-cs-fixer changes + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + +``` ## Versioning