├── .gitignore ├── Dockerfile ├── LICENSE ├── README.md ├── action.yml └── upload-to-release /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .env -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM alpine:latest 2 | LABEL author="Jason Etcovitch " 3 | 4 | LABEL "com.github.actions.name"="Upload to Release" 5 | LABEL "com.github.actions.description"="Uploads a file to a new release." 6 | LABEL "com.github.actions.icon"="package" 7 | LABEL "com.github.actions.color"="blue" 8 | 9 | RUN apk add --no-cache \ 10 | bash \ 11 | ca-certificates \ 12 | curl \ 13 | jq 14 | 15 | COPY upload-to-release /usr/bin/upload-to-release 16 | 17 | ENTRYPOINT ["upload-to-release"] 18 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Jason Etcovitch 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. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

Upload to Release

2 |

A GitHub Action that uploads a file to a new release.

3 | 4 | ## Usage 5 | 6 | This action uploads any file to a new release: 7 | 8 | image 9 | 10 | One example workflow is to build and save a Docker image then upload it to a release: 11 | 12 | ```yaml 13 | # .github/workflows/build-docker-image.yml 14 | name: build-docker-image 15 | 16 | on: release 17 | 18 | jobs: 19 | build-docker-image: 20 | name: Build and upload docker image 21 | runs-on: ubuntu-latest 22 | steps: 23 | - name: Pull source 24 | uses: actions/checkout@v1 25 | 26 | - name: Build Docker image 27 | uses: actions/docker/cli@master 28 | with: 29 | args: build . -t my-image 30 | 31 | - name: Save the image 32 | uses: actions/docker/cli@master 33 | with: 34 | args: save my-image:latest 35 | 36 | - name: Upload to release 37 | uses: JasonEtco/upload-to-release@master 38 | with: 39 | args: my-image.tar 40 | env: 41 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 42 | ``` 43 | 44 | ## Requirements 45 | 46 | You must pass at least one argument, the path to the file you want to attach. You must also include the `GITHUB_TOKEN` secret, otherwise uploading the file will not work. 47 | 48 | ```yaml 49 | - name: Upload to release 50 | uses: JasonEtco/upload-to-release@master 51 | with: 52 | args: my-image.tar 53 | env: 54 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 55 | ``` 56 | 57 | ### Content-Type 58 | 59 | You may also need to pass an additional argument, the `Content-Type` header used when uploading your file (this is `application/zip` by default): 60 | 61 | ```yaml 62 | - name: Upload to release 63 | uses: JasonEtco/upload-to-release@master 64 | with: 65 | args: my-image.tar application/zip 66 | env: 67 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 68 | ``` 69 | 70 | ### Event type 71 | 72 | The event type your workflow is triggered from is critical to the function of this action. If you are not using `on: release` for your workflow this action will not work correctly. 73 | 74 | The event sent to the action by GitHub only contains an artifact upload URL when using the `release` event type to trigger the workflow. 75 | 76 | If you need other workflows not based on the `release` event, you should use multiple workflow definitions. 77 | -------------------------------------------------------------------------------- /action.yml: -------------------------------------------------------------------------------- 1 | name: upload-to-release 2 | description: Uploads a file to a GitHub release 3 | author: JasonEtco 4 | branding: 5 | color: blue 6 | icon: package 7 | runs: 8 | using: docker 9 | image: Dockerfile 10 | -------------------------------------------------------------------------------- /upload-to-release: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | set -o pipefail 5 | 6 | # Ensure that the GITHUB_TOKEN secret is included 7 | if [[ -z "$GITHUB_TOKEN" ]]; then 8 | echo "Set the GITHUB_TOKEN env variable." 9 | exit 1 10 | fi 11 | 12 | # Ensure that the file path is present 13 | if [[ -z "$1" ]]; then 14 | echo "You must pass at least one argument to this action, the path to the file to upload." 15 | exit 1 16 | fi 17 | 18 | # Only upload to non-draft releases 19 | IS_DRAFT=$(jq --raw-output '.release.draft' $GITHUB_EVENT_PATH) 20 | if [ "$IS_DRAFT" = true ]; then 21 | echo "This is a draft, so nothing to do!" 22 | exit 0 23 | fi 24 | 25 | # Prepare the headers 26 | AUTH_HEADER="Authorization: token ${GITHUB_TOKEN}" 27 | CONTENT_LENGTH_HEADER="Content-Length: $(stat -c%s "${1}")" 28 | 29 | if [[ -z "$2" ]]; then 30 | CONTENT_TYPE_HEADER="Content-Type: ${2}" 31 | else 32 | CONTENT_TYPE_HEADER="Content-Type: application/zip" 33 | fi 34 | 35 | # Build the Upload URL from the various pieces 36 | RELEASE_ID=$(jq --raw-output '.release.id' $GITHUB_EVENT_PATH) 37 | if [[ -z "${RELEASE_ID}" ]]; then 38 | echo "There was no release ID in the GitHub event. Are you using the release event type?" 39 | exit 1 40 | fi 41 | 42 | FILENAME=$(basename $1) 43 | UPLOAD_URL="https://uploads.github.com/repos/${GITHUB_REPOSITORY}/releases/${RELEASE_ID}/assets?name=${FILENAME}" 44 | echo "$UPLOAD_URL" 45 | 46 | # Upload the file 47 | curl \ 48 | -f \ 49 | -sSL \ 50 | -XPOST \ 51 | -H "${AUTH_HEADER}" \ 52 | -H "${CONTENT_LENGTH_HEADER}" \ 53 | -H "${CONTENT_TYPE_HEADER}" \ 54 | --upload-file "${1}" \ 55 | "${UPLOAD_URL}" 56 | --------------------------------------------------------------------------------