From 1233dffc335435ce2640b18418b79b4cdecbb073 Mon Sep 17 00:00:00 2001 From: Qu4k Date: Mon, 20 Jul 2020 22:51:46 +0200 Subject: [PATCH] chore: from Dockerfile to js --- Dockerfile | 6 ----- LICENSE | 21 +++++++++++++++++ README.md | 63 +++++++++++++++++++++++++++++++++++++++++++++------ action.yml | 37 +++++++++++++++++++++++++++--- entrypoint.sh | 28 ----------------------- start.js | 26 +++++++++++++++++++++ start.sh | 40 ++++++++++++++++++++++++++++++++ 7 files changed, 177 insertions(+), 44 deletions(-) delete mode 100644 Dockerfile create mode 100644 LICENSE delete mode 100644 entrypoint.sh create mode 100644 start.js create mode 100644 start.sh diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index c2951ea..0000000 --- a/Dockerfile +++ /dev/null @@ -1,6 +0,0 @@ -FROM alpine:3.9 - -RUN apk --no-cache add openssl git curl openssh-client bash - -COPY entrypoint.sh /entrypoint.sh -ENTRYPOINT [ "/entrypoint.sh" ] \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..319388a --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 actions-js team + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md index b28e9e5..6b40b76 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,58 @@ -# push +# GitHub Action for GitHub Commit & Push -Push changes made by actions right back into the current repository. +The GitHub Actions for commiting & pushing to GitHub repository local changes authorizing using GitHub token. -```yml -uses: actions-js/push@v1 -env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} -``` \ No newline at end of file +With ease: +- update new code placed in the repository, e.g. by running a linter on it, +- track changes in script results using Git as archive, +- publish page using GitHub-Pages, +- mirror changes to a separate repository. + +## Usage + +### Example Workflow file + +An example workflow to authenticate with GitHub Platform: + +```yaml +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@master + with: + persist-credentials: false # otherwise, the token used is the GITHUB_TOKEN, instead of your personal token + fetch-depth: 0 # otherwise, you will failed to push refs to dest repo + - name: Create local changes + run: | + ... + - name: Commit & Push changes + uses: actions-js/push@master + with: + github_token: ${{ secrets.GITHUB_TOKEN }} +``` + +### Inputs + +| name | value | default | description | +| ------------ | ------ | ----------------- | ----------- | +| github_token | string | | Token for the repo. Can be passed in using `${{ secrets.GITHUB_TOKEN }}`. | +| user_email | string | action@github.com | Email used to configure user.email in `git config`. | +| user_name | string | GitHub Action | Name used to configure user.name in `git config`. | +| branch | string | 'master' | Destination branch to push changes. | +| force | boolean | false | Determines if force push is used. | +| tags | boolean | false | Determines if `--tags` is used. | +| directory | string | '.' | Directory to change to before pushing. | +| 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. | + +## License + +The Dockerfile and associated scripts and documentation in this project are released under the [MIT License](LICENSE). + +## Credits + +This is a slight modification of the [ad-m/github-push-action](https://github.com/ad-m/github-push-action) action. + +## No affiliation with GitHub Inc. + +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. \ No newline at end of file diff --git a/action.yml b/action.yml index d825bca..0b844f0 100644 --- a/action.yml +++ b/action.yml @@ -1,8 +1,39 @@ -name: 'Push' +name: 'GitHub Commit & Push' description: 'Push changes made by actions right back into the current repository.' +author: "actions-js" +inputs: + github_token: + description: 'Token for the repo. Can be passed in using $\{{ secrets.GITHUB_TOKEN }}' + required: true + user_email: + description: 'Token for the repo. Can be passed in using $\{{ secrets.GITHUB_TOKEN }}' + default: 'action@github.com' + required: false + user_name: + description: 'Token for the repo. Can be passed in using $\{{ secrets.GITHUB_TOKEN }}' + default: 'Github Action' + required: false + repository: + description: 'Repository name to push. Default or empty value represents current github repository (${GITHUB_REPOSITORY})' + default: '' + required: false + branch: + description: 'Destination branch to push changes' + required: false + default: 'master' + force: + description: 'Determines if force push is used' + required: false + tags: + description: 'Determines if --tags is used' + required: false + directory: + description: 'Directory to change to before pushing.' + required: false + default: '.' runs: - using: 'docker' - image: 'Dockerfile' + using: 'node12' + main: 'start.js' branding: icon: 'arrow-up-circle' color: 'green' \ No newline at end of file diff --git a/entrypoint.sh b/entrypoint.sh deleted file mode 100644 index 37d0cd6..0000000 --- a/entrypoint.sh +++ /dev/null @@ -1,28 +0,0 @@ -#!/bin/sh - -# a github token is needed to grant -# push priviledges for the current repo -if [ -z "${GITHUB_TOKEN}" ]; then - echo "error: not found GITHUB_TOKEN" - exit 1 -fi - -repository="https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git" - -git config http.sslVerify false -git config user.name "Push Bot" -git config user.email "actions@users.noreply.github.com" -git remote add publisher "${repository}" - -git show-ref -git branch --verbose - -# publish any new files -git checkout master -git add -A - -timestamp=$(date -u) -git commit -m "Automated push: ${timestamp} ${GITHUB_SHA}" || exit 0 - -git pull --rebase publisher master -git push publisher master \ No newline at end of file diff --git a/start.js b/start.js new file mode 100644 index 0000000..d0eff21 --- /dev/null +++ b/start.js @@ -0,0 +1,26 @@ +const spawn = require('child_process').spawn; +const path = require("path"); + +const exec = (cmd, args=[]) => new Promise((resolve, reject) => { + console.log(`Started: ${cmd} ${args.join(" ")}`) + const app = spawn(cmd, args, { stdio: 'inherit' }); + app.on('close', code => { + if(code !== 0){ + err = new Error(`Invalid status code: ${code}`); + err.code = code; + return reject(err); + }; + return resolve(code); + }); + app.on('error', reject); +}); + +const main = async () => { + await exec('bash', [path.join(__dirname, './start.sh')]); +}; + +main().catch(err => { + console.error(err); + console.error(err.stack); + process.exit(err.code || -1); +}) \ No newline at end of file diff --git a/start.sh b/start.sh new file mode 100644 index 0000000..06b8b10 --- /dev/null +++ b/start.sh @@ -0,0 +1,40 @@ +#!/bin/sh +set -e + +INPUT_USER_EMAIL=${INPUT_USER_EMAIL:-'action@github.com'} +INPUT_USER_NAME=${INPUT_USER_NAME:-'GitHub Action'} +INPUT_BRANCH=${INPUT_BRANCH:-master} +INPUT_FORCE=${INPUT_FORCE:-false} +INPUT_TAGS=${INPUT_TAGS:-false} +INPUT_DIRECTORY=${INPUT_DIRECTORY:-'.'} +_FORCE_OPTION='' +REPOSITORY=${INPUT_REPOSITORY:-$GITHUB_REPOSITORY} + +echo "Push to branch $INPUT_BRANCH"; +[ -z "${INPUT_GITHUB_TOKEN}" ] && { + echo 'Missing input "github_token: ${{ secrets.GITHUB_TOKEN }}".'; + exit 1; +}; + +if ${INPUT_FORCE}; then + _FORCE_OPTION='--force' +fi + +if ${INPUT_TAGS}; then + _TAGS='--tags' +fi + +cd ${INPUT_DIRECTORY} + +remote_repo="https://${GITHUB_ACTOR}:${INPUT_GITHUB_TOKEN}@github.com/${REPOSITORY}.git" + +git config http.sslVerify false +git config --local user.email "${INPUT_USER_EMAIL}" +git config --local user.name "${INPUT_USER_NAME}" + +timestamp=$(date -u) + +git add -A +git commit -m "chore: autopublish ${timestamp}" || exit 0 + +git push "${remote_repo}" HEAD:${INPUT_BRANCH} --follow-tags $_FORCE_OPTION $_TAGS; \ No newline at end of file