├── Dockerfile ├── README.md ├── action.yml └── entrypoint.sh /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM alpine:3.11 2 | 3 | RUN apk add --no-cache bash grep curl 4 | 5 | COPY entrypoint.sh /entrypoint.sh 6 | 7 | ENTRYPOINT ["/entrypoint.sh"] 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gist-sync-action 2 | Github action to mirror a file into a Gist 3 | 4 | ## Setup 5 | 1. Create a new public Gist (https://gist.github.com) 6 | 2. Create a token with the `gist` scope (https://github.com/settings/tokens/new) 7 | 3. Create the file you'll be syncing to your Gist 8 | 9 | ## Usage 10 | ``` 11 | on: [push] 12 | 13 | jobs: 14 | gist-sync: 15 | name: gist-sync 16 | runs-on: ubuntu-latest 17 | steps: 18 | - name: Checkout 19 | uses: actions/checkout@v2 20 | - uses: popsiclestick/gist-sync-action@v1.2.0 21 | id: sync 22 | with: 23 | auth: ${{ secrets.GIST_TOKEN }} 24 | gist_url: https://gist.github.com// 25 | gist_title: Example Gist Title 26 | gist_description: Example Gist Description 27 | github_file: filename-to-sync 28 | ``` 29 | -------------------------------------------------------------------------------- /action.yml: -------------------------------------------------------------------------------- 1 | name: "gist-sync" 2 | author: "Michael Salsone" 3 | description: "Sync a Gist from a Github file" 4 | inputs: 5 | auth: 6 | description: "Auth token with gist permissions" 7 | required: true 8 | gist_url: 9 | description: "URL of the gist" 10 | required: true 11 | gist_title: 12 | description: "Title of the gist" 13 | required: true 14 | gist_description: 15 | description: "Description of the gist" 16 | required: true 17 | github_file: 18 | description: "The Filename with the contents we want to mirror" 19 | required: true 20 | runs: 21 | using: 'docker' 22 | image: 'Dockerfile' 23 | args: 24 | - ${{ inputs.auth }} 25 | - ${{ inputs.gist_url }} 26 | - ${{ inputs.gist_title }} 27 | - ${{ inputs.gist_description }} 28 | - ${{ inputs.github_file }} 29 | branding: 30 | icon: 'code' 31 | color: 'green' 32 | -------------------------------------------------------------------------------- /entrypoint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # $1 :: ${{ inputs.auth }} 4 | # $2 :: ${{ inputs.gist_url }} 5 | # $3 :: ${{ inputs.gist_title }} 6 | # $4 :: ${{ inputs.gist_description }} 7 | # $5 :: ${{ inputs.github_file }} 8 | 9 | Error() { 10 | echo "$1" 11 | exit "$2" 12 | } 13 | 14 | auth_token=$1 15 | 16 | gist_api="https://api.github.com/gists/" 17 | gist_id=$(grep -Po "\w+$" <<< "$2") 18 | gist_endpoint=$gist_api$gist_id 19 | 20 | title=$(echo "$3" | sed 's/\"/\\"/g') 21 | description=$(echo "$4" | sed 's/\"/\\"/g') 22 | 23 | [[ -r "$5" ]] || Error "The file '$5' does not exist or is not readable" 1 24 | content=$(sed -e 's/\\/\\\\/g' -e 's/\t/\\t/g' -e 's/\"/\\"/g' -e 's/\r//g' "$5" | sed -E ':a;N;$!ba;s/\r{0,1}\n/\\n/g') 25 | 26 | echo '{"description": "'"$description"'", "files": {"'"$title"'": {"content": "'"$content"'"}}}' > postContent.json || Error 'Failed to write temp json file' 2 27 | 28 | curl -s -X PATCH \ 29 | -H "Content-Type: application/json" \ 30 | -H "Authorization: token $auth_token" \ 31 | -d @postContent.json "$gist_endpoint" \ 32 | --fail --show-error || Error 'Failed to patch gist' 3 33 | --------------------------------------------------------------------------------