Skip to content

Commit

Permalink
Merge pull request #24 from wangyoucao577/feature/customize-target-re…
Browse files Browse the repository at this point in the history
…lease

Support publish to specific release on every `push` event
  • Loading branch information
wangyoucao577 authored Nov 13, 2020
2 parents d407567 + aea8b25 commit 1d13e5e
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 33 deletions.
12 changes: 10 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@

FROM debian:stretch-slim

RUN DEBIAN_FRONTEND=noninteractive apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y \
RUN DEBIAN_FRONTEND=noninteractive apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y \
curl \
wget \
git \
build-essential \
zip \
jq
jq \
ca-certificates \
&& rm -rf /var/lib/apt/lists/*

# github-assets-uploader to provide robust github assets upload
RUN wget --no-check-certificate --progress=dot:mega https://github.com/wangyoucao577/assets-uploader/releases/download/v0.2.0/github-assets-uploader-v0.2.0-linux-amd64.tar.gz -O github-assets-uploader.tar.gz && \
tar -zxf github-assets-uploader.tar.gz && \
mv github-assets-uploader /usr/sbin/ && \
rm -f github-assets-uploader.tar.gz && \
github-assets-uploader -version

COPY *.sh /
ENTRYPOINT ["/entrypoint.sh"]
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ Automatically publish `Go` binaries to Github Release Assets through Github Acti
- Rich parameters support for `go build`(e.g. `-ldflags`, etc.).
- Support package extra files into artifacts (e.g., `LICENSE`, `README.md`, etc).
- Support customize build command, e.g., use [packr2](https://github.com/gobuffalo/packr/tree/master/v2)(`packr2 build`) instead of `go build`.
- Support optional `.md5` along with artifacts.
- Support optional `.md5` along with artifacts.
- Support optional `.sha256` along with artifacts.
- Customizable release tag to support publish binaries per `push`.
- Support overwrite assets if it's already exist.

## Usage

Expand Down Expand Up @@ -59,7 +61,8 @@ jobs:
| extra_files | **Optional** | Extra files that will be packaged into artifacts either. Multiple files separated by space. Note that extra folders can be allowed either since internal `cp -r` already in use. <br>E.g., `extra_files: LICENSE README.md` |
| md5sum | **Optional** | Publish `.md5` along with artifacts, `TRUE` by default. |
| sha256sum | **Optional** | Publish `.sha256` along with artifacts, `FALSE` by default. |

| release_tag | **Optional** | Target release tag to publish your binaries to. It's dedicated to publish binaries on every `push` into one specified release page since there's no target in this case. DON'T set it if you trigger the action by `release: [created]` event as most people do.|
| overwrite | **Optional** | Overwrite asset if it's already exist. `FALSE` by default. |

### Advanced Example

Expand Down
10 changes: 10 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ inputs:
description: 'Publish `.sha256` along with artifacts.'
required: false
default: 'FALSE'
release_tag:
description: 'Upload binaries to specified release page that indicated by Git tag.'
required: false
default: ''
overwrite:
description: "Overwrite asset if it's already exist."
required: false
default: 'FALSE'

runs:
using: 'docker'
Expand All @@ -73,6 +81,8 @@ runs:
- ${{ inputs.extra_files }}
- ${{ inputs.md5sum }}
- ${{ inputs.sha256sum }}
- ${{ inputs.release_tag }}
- ${{ inputs.overwrite }}

branding:
icon: 'package'
Expand Down
59 changes: 30 additions & 29 deletions release.sh
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
#!/bin/bash -eux

# prepare binary/release name
# prepare binary_name/release_tag/release_asset_name
BINARY_NAME=$(basename ${GITHUB_REPOSITORY})
if [ x${INPUT_BINARY_NAME} != x ]; then
BINARY_NAME=${INPUT_BINARY_NAME}
fi
RELEASE_TAG=$(basename ${GITHUB_REF})
if [ ! -z "${INPUT_RELEASE_TAG}" ]; then
RELEASE_TAG=${INPUT_RELEASE_TAG}
fi
RELEASE_ASSET_NAME=${BINARY_NAME}-${RELEASE_TAG}-${INPUT_GOOS}-${INPUT_GOARCH}

# prepare upload URL
RELEASE_ASSETS_UPLOAD_URL=$(cat ${GITHUB_EVENT_PATH} | jq -r .release.upload_url)
RELEASE_ASSETS_UPLOAD_URL=${RELEASE_ASSETS_UPLOAD_URL%\{?name,label\}}
# prompt error if non-supported event
if [ ${GITHUB_EVENT_NAME} == 'release' ]; then
echo "Event: ${GITHUB_EVENT_NAME}"
elif [ ${GITHUB_EVENT_NAME} == 'push' ]; then
echo "Event: ${GITHUB_EVENT_NAME}"
else
echo "Unsupport event: ${GITHUB_EVENT_NAME}!"
exit 1
fi

# execute pre-command if exist, e.g. `go get -v ./...`
if [ ! -z "${INPUT_PRE_COMMAND}" ]; then
Expand Down Expand Up @@ -47,43 +56,35 @@ ls -lha

# compress and package binary, then calculate checksum
RELEASE_ASSET_EXT='.tar.gz'
MEDIA_TYPE='application/gzip'
if [ ${INPUT_GOOS} == 'windows' ]; then
RELEASE_ASSET_EXT='.zip'
MEDIA_TYPE='application/zip'
zip -vr ${RELEASE_ASSET_NAME}${RELEASE_ASSET_EXT} *
else
tar cvfz ${RELEASE_ASSET_NAME}${RELEASE_ASSET_EXT} *
fi
MD5_SUM=$(md5sum ${RELEASE_ASSET_NAME}${RELEASE_ASSET_EXT} | cut -d ' ' -f 1)
SHA256_SUM=$(sha256sum ${RELEASE_ASSET_NAME}${RELEASE_ASSET_EXT} | cut -d ' ' -f 1)

# update binary and checksum
curl \
--fail \
-X POST \
--data-binary @${RELEASE_ASSET_NAME}${RELEASE_ASSET_EXT} \
-H 'Content-Type: application/gzip' \
-H "Authorization: Bearer ${INPUT_GITHUB_TOKEN}" \
"${RELEASE_ASSETS_UPLOAD_URL}?name=${RELEASE_ASSET_NAME}${RELEASE_ASSET_EXT}"
echo $?
# prefix upload extra params
GITHUB_ASSETS_UPLOADR_EXTRA_OPTIONS=''
if [ ${INPUT_OVERWRITE^^} == 'TRUE' ]; then
GITHUB_ASSETS_UPLOADR_EXTRA_OPTIONS="-overwrite"
fi

# update binary and checksum
github-assets-uploader -f ${RELEASE_ASSET_NAME}${RELEASE_ASSET_EXT} -mediatype ${MEDIA_TYPE} ${GITHUB_ASSETS_UPLOADR_EXTRA_OPTIONS} -repo ${GITHUB_REPOSITORY} -token ${INPUT_GITHUB_TOKEN} -tag ${RELEASE_TAG}
if [ ${INPUT_MD5SUM^^} == 'TRUE' ]; then
curl \
--fail \
-X POST \
--data ${MD5_SUM} \
-H 'Content-Type: text/plain' \
-H "Authorization: Bearer ${INPUT_GITHUB_TOKEN}" \
"${RELEASE_ASSETS_UPLOAD_URL}?name=${RELEASE_ASSET_NAME}${RELEASE_ASSET_EXT}.md5"
echo $?
MD5_EXT='.md5'
MD5_MEDIA_TYPE='text/plain'
echo ${MD5_SUM} >${RELEASE_ASSET_NAME}${RELEASE_ASSET_EXT}${MD5_EXT}
github-assets-uploader -f ${RELEASE_ASSET_NAME}${RELEASE_ASSET_EXT}${MD5_EXT} -mediatype ${MD5_MEDIA_TYPE} ${GITHUB_ASSETS_UPLOADR_EXTRA_OPTIONS} -repo ${GITHUB_REPOSITORY} -token ${INPUT_GITHUB_TOKEN} -tag ${RELEASE_TAG}
fi

if [ ${INPUT_SHA256SUM^^} == 'TRUE' ]; then
curl \
--fail \
--X POST \
--data ${SHA256_SUM} \
-H 'Content-Type: text/plain' \
-H "Authorization: Bearer ${INPUT_GITHUB_TOKEN}" \
"${RELEASE_ASSETS_UPLOAD_URL}?name=${RELEASE_ASSET_NAME}${RELEASE_ASSET_EXT}.sha256"
echo $?
SHA256_EXT='.sha256'
SHA256_MEDIA_TYPE='text/plain'
echo ${SHA256_SUM} >${RELEASE_ASSET_NAME}${RELEASE_ASSET_EXT}${SHA256_EXT}
github-assets-uploader -f ${RELEASE_ASSET_NAME}${RELEASE_ASSET_EXT}${SHA256_EXT} -mediatype ${SHA256_MEDIA_TYPE} ${GITHUB_ASSETS_UPLOADR_EXTRA_OPTIONS} -repo ${GITHUB_REPOSITORY} -token ${INPUT_GITHUB_TOKEN} -tag ${RELEASE_TAG}
fi

0 comments on commit 1d13e5e

Please sign in to comment.