├── start.js ├── LICENSE ├── action.yml ├── start.sh └── README.md /start.js: -------------------------------------------------------------------------------- 1 | const spawn = require('child_process').spawn; 2 | const path = require("path"); 3 | 4 | const exec = (cmd, args=[]) => new Promise((resolve, reject) => { 5 | console.log(`Started: ${cmd} ${args.join(" ")}`) 6 | const app = spawn(cmd, args, { stdio: 'inherit' }); 7 | app.on('close', code => { 8 | if(code !== 0){ 9 | err = new Error(`Invalid status code: ${code}`); 10 | err.code = code; 11 | return reject(err); 12 | }; 13 | return resolve(code); 14 | }); 15 | app.on('error', reject); 16 | }); 17 | 18 | const main = async () => { 19 | await exec('bash', [path.join(__dirname, './start.sh')]); 20 | }; 21 | 22 | main().catch(err => { 23 | console.error(err); 24 | console.error(err.stack); 25 | process.exit(err.code || -1); 26 | }) -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 actions-js team 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /action.yml: -------------------------------------------------------------------------------- 1 | name: 'GitHub Commit & Push' 2 | description: 'Push changes made by actions right back into the current repository.' 3 | author: "actions-js" 4 | inputs: 5 | github_token: 6 | description: 'Token for the repo. Can be passed in using $\{{ secrets.GITHUB_TOKEN }}' 7 | required: true 8 | author_email: 9 | description: 'Email used to configure user.email in `git config`.' 10 | default: 'github-actions[bot]@users.noreply.github.com' 11 | required: false 12 | author_name: 13 | description: 'Name used to configure user.name in `git config`.' 14 | default: 'github-actions[bot]' 15 | required: false 16 | coauthor_email: 17 | description: 'Email used to make a co-authored commit.' 18 | required: false 19 | coauthor_name: 20 | description: 'Name used to make a co-authored commit.' 21 | required: false 22 | message: 23 | description: 'Commit message.' 24 | required: false 25 | repository: 26 | description: 'Repository name to push. Default or empty value represents current github repository (${GITHUB_REPOSITORY}).' 27 | default: '' 28 | required: false 29 | branch: 30 | description: 'Destination branch to push changes.' 31 | required: false 32 | default: 'main' 33 | empty: 34 | description: 'Allow empty commit.' 35 | required: false 36 | amend: 37 | description: 'Determines if the commit should be amended.' 38 | required: false 39 | default: 'false' 40 | force: 41 | description: 'Determines if force push is used.' 42 | required: false 43 | tags: 44 | description: 'Determines if --tags is used.' 45 | required: false 46 | rebase: 47 | description: 'Determines if `git pull --rebase` is used before the push. This is useful if multiple actions try to push at the same time.' 48 | required: false 49 | directory: 50 | description: 'Directory to change to before pushing.' 51 | required: false 52 | default: '.' 53 | runs: 54 | using: 'node20' 55 | main: 'start.js' 56 | branding: 57 | icon: 'arrow-up-circle' 58 | color: 'green' 59 | -------------------------------------------------------------------------------- /start.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | set -e 3 | 4 | timestamp=$(date -u +"%Y-%m-%dT%H:%M:%SZ") 5 | 6 | INPUT_AUTHOR_EMAIL=${INPUT_AUTHOR_EMAIL:-'github-actions[bot]@users.noreply.github.com'} 7 | INPUT_AUTHOR_NAME=${INPUT_AUTHOR_NAME:-'github-actions[bot]'} 8 | INPUT_COAUTHOR_EMAIL=${INPUT_COAUTHOR_EMAIL:-''} 9 | INPUT_COAUTHOR_NAME=${INPUT_COAUTHOR_NAME:-''} 10 | INPUT_MESSAGE=${INPUT_MESSAGE:-"chore: autopublish ${timestamp}"} 11 | INPUT_BRANCH=${INPUT_BRANCH:-master} 12 | INPUT_AMEND=${INPUT_AMEND:-false} 13 | INPUT_FORCE=${INPUT_FORCE:-false} 14 | INPUT_TAGS=${INPUT_TAGS:-false} 15 | INPUT_REBASE=${INPUT_REBASE:-false} 16 | INPUT_EMPTY=${INPUT_EMPTY:-false} 17 | INPUT_DIRECTORY=${INPUT_DIRECTORY:-'.'} 18 | REPOSITORY=${INPUT_REPOSITORY:-$GITHUB_REPOSITORY} 19 | 20 | echo "Push to branch $INPUT_BRANCH"; 21 | [ -z "${INPUT_GITHUB_TOKEN}" ] && { 22 | echo 'Missing input "github_token: ${{ secrets.GITHUB_TOKEN }}".'; 23 | exit 1; 24 | }; 25 | 26 | if ${INPUT_EMPTY}; then 27 | _EMPTY='--allow-empty' 28 | fi 29 | 30 | if ${INPUT_AMEND}; then 31 | _AMEND='--amend --no-edit' 32 | fi 33 | 34 | if ${INPUT_FORCE}; then 35 | _FORCE_OPTION='--force' 36 | fi 37 | 38 | if ${INPUT_TAGS}; then 39 | _TAGS='--tags' 40 | fi 41 | 42 | cd "${INPUT_DIRECTORY}" 43 | 44 | remote_repo="https://${GITHUB_ACTOR}:${INPUT_GITHUB_TOKEN}@github.com/${REPOSITORY}.git" 45 | 46 | git config http.sslVerify false 47 | git config --local user.email "${INPUT_AUTHOR_EMAIL}" 48 | git config --local user.name "${INPUT_AUTHOR_NAME}" 49 | 50 | git add -A 51 | 52 | if ${INPUT_AMEND}; then 53 | if [ -n "${INPUT_COAUTHOR_EMAIL}" ] && [ -n "${INPUT_COAUTHOR_NAME}" ]; then 54 | git commit ${_AMEND} -m "${INPUT_MESSAGE} 55 | 56 | Co-authored-by: ${INPUT_COAUTHOR_NAME} <${INPUT_COAUTHOR_EMAIL}>" || exit 0 57 | else 58 | git commit ${_AMEND} -m "${INPUT_MESSAGE}" $_EMPTY || exit 0 59 | fi 60 | 61 | elif [ -n "${INPUT_COAUTHOR_EMAIL}" ] && [ -n "${INPUT_COAUTHOR_NAME}" ]; then 62 | git commit -m "${INPUT_MESSAGE} 63 | 64 | 65 | Co-authored-by: ${INPUT_COAUTHOR_NAME} <${INPUT_COAUTHOR_EMAIL}>" $_EMPTY || exit 0 66 | else 67 | git commit -m "${INPUT_MESSAGE}" $_EMPTY || exit 0 68 | fi 69 | 70 | if ${INPUT_REBASE}; then 71 | git pull --rebase 72 | fi 73 | 74 | git push "${remote_repo}" HEAD:"${INPUT_BRANCH}" --follow-tags $_FORCE_OPTION $_TAGS; -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GitHub Action for GitHub Commit & Push 2 | 3 | The GitHub Actions for commiting & pushing to GitHub repository local changes authorizing using GitHub token. 4 | 5 | With ease: 6 | - update new code placed in the repository, e.g. by running a linter on it, 7 | - track changes in script results using Git as archive, 8 | - publish page using GitHub-Pages, 9 | - mirror changes to a separate repository. 10 | 11 | ## Usage 12 | 13 | ### Example Workflow file 14 | 15 | An example workflow to authenticate with GitHub Platform: 16 | 17 | ```yaml 18 | jobs: 19 | build: 20 | runs-on: ubuntu-latest 21 | steps: 22 | - uses: actions/checkout@master 23 | with: 24 | persist-credentials: false # otherwise, the token used is the GITHUB_TOKEN, instead of your personal token 25 | fetch-depth: 0 # otherwise, you will fail to push refs to dest repo 26 | - name: Create local changes 27 | run: | 28 | ... 29 | - name: Commit & Push changes 30 | uses: actions-js/push@master 31 | with: 32 | github_token: ${{ secrets.GITHUB_TOKEN }} 33 | ``` 34 | 35 | ### Inputs 36 | 37 | | name | value | default | description | 38 | | -------------- | ------ | --------------------------- | ----------- | 39 | | github_token | string | | Token for the repo. Can be passed in using `${{ secrets.GITHUB_TOKEN }}`. | 40 | | author_email | string | 'github-actions[bot]@users.noreply.github.com' | Email used to configure user.email in `git config`. | 41 | | author_name | string | 'github-actions[bot]' | Name used to configure user.name in `git config`. | 42 | | coauthor_email | string | | Email used to make a co-authored commit. | 43 | | coauthor_name | string | | Name used to make a co-authored commit. | 44 | | message | string | 'chore: autopublish ${date}' | Commit message. | 45 | | branch | string | 'main' | Destination branch to push changes. | 46 | | empty | boolean | false | Allow empty commit. | 47 | | amend | boolean | false | Determines if the commit should be amended. Needs to be used with `force` input to force push the amended commit. | 48 | | force | boolean | false | Determines if force push is used. | 49 | | tags | boolean | false | Determines if `--tags` is used. | 50 | | rebase | boolean | false | Determines if `git pull --rebase` is used before the push. This is useful if multiple actions try to push at the same time. | 51 | | directory | string | '.' | Directory to change to before pushing. | 52 | | repository | string | '' | Repository name. Default or empty repository name represents current github repository. If you want to push to other repository, you should make a [personal access token](https://github.com/settings/tokens) and use it as the `github_token` input. | 53 | 54 | ## License 55 | 56 | The Dockerfile and associated scripts and documentation in this project are released under the [MIT License](LICENSE). 57 | 58 | ## Credits 59 | 60 | This is a slight modification of the [ad-m/github-push-action](https://github.com/ad-m/github-push-action) action. 61 | 62 | ## No affiliation with GitHub Inc. 63 | 64 | GitHub are registered trademarks of GitHub, Inc. GitHub name used in this project are for identification purposes only. The project is not associated in any way with GitHub Inc. and is not an official solution of GitHub Inc. It was made available in order to facilitate the use of the site GitHub. 65 | --------------------------------------------------------------------------------