Skip to content

Commit

Permalink
feat: add post_command feature to do some teardown work (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
debugtalk committed Jan 26, 2022
1 parent e6ac942 commit 2875b2d
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 59 deletions.
13 changes: 7 additions & 6 deletions .github/workflows/autotest.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Test
name: Test

on:
on:
push:
branches-ignore:
- '*-prebuilt'
Expand All @@ -13,17 +13,17 @@ jobs:
matrix:
goos: [linux, windows, darwin]
goarch: [amd64, "386", arm64]
exclude:
exclude:
- goarch: "386"
goos: darwin
goos: darwin
- goarch: arm64
goos: windows
steps:
# - name: Wait release docker build for release branches
# if: contains(github.ref, 'release')
# uses: jakejarvis/wait-action@master
# with:
# time: '300s'
# time: '300s'
- name: Checkout
uses: actions/checkout@v2
- name: Set BUILD_TIME env
Expand All @@ -47,6 +47,7 @@ jobs:
release_tag: v0.1-test-assets
asset_name: testmain-acceptance-${{ matrix.goos }}-${{ matrix.goarch }}
retry: 5
post_command: echo "RELEASE_ASSET_FILE=$RELEASE_ASSET_FILE"

setup-go-test:
name: Set up Go Test
Expand All @@ -62,7 +63,7 @@ jobs:
# if: contains(github.ref, 'release')
# uses: jakejarvis/wait-action@master
# with:
# time: '300s'
# time: '300s'
- name: Checkout
uses: actions/checkout@v2
- name: Set GO_VERSION_TAG env
Expand Down
75 changes: 38 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
# Go Release GitHub Action
![Build Docker](https://github.com/wangyoucao577/go-release-action/workflows/Build%20Docker/badge.svg) ![PR Build](https://github.com/wangyoucao577/go-release-action/workflows/PR%20Build/badge.svg) [![Test](https://github.com/wangyoucao577/go-release-action/actions/workflows/autotest.yml/badge.svg)](https://github.com/wangyoucao577/go-release-action/actions/workflows/autotest.yml)
Automatically publish `Go` binaries to Github Release Assets through Github Action.
# Go Release GitHub Action
![Build Docker](https://github.com/wangyoucao577/go-release-action/workflows/Build%20Docker/badge.svg) ![PR Build](https://github.com/wangyoucao577/go-release-action/workflows/PR%20Build/badge.svg) [![Test](https://github.com/wangyoucao577/go-release-action/actions/workflows/autotest.yml/badge.svg)](https://github.com/wangyoucao577/go-release-action/actions/workflows/autotest.yml)
Automatically publish `Go` binaries to Github Release Assets through Github Action.

## Features
- Build `Go` binaries for release and publish to Github Release Assets.
- Customizable `Go` versions. `latest` by default.
- Support different `Go` project path in repository.
- Support multiple binaries in same repository.
- Customizable binary name.
- Support multiple `GOOS`/`GOARCH` build in parallel by [Github Action Matrix Strategy](https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idstrategymatrix) gracefully.
- Publish `.zip` instead of `.tar.gz` for `windows`.
- No `musl` library dependency issue on `linux`.
- Support extra command that will be executed before `go build`. You may want to use it to solve dependency if you're NOT using [Go Modules](https://github.com/golang/go/wiki/Modules).
- 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`. Another important usage is to use `make`(`Makefile`) for building on Unix-like systems.
- Support optional `.md5` along with artifacts.
- Support optional `.sha256` along with artifacts.
- Customizable release tag to support publish binaries per `push` or `workflow_dispatch`(manually trigger).
- Support overwrite assets if it's already exist.
- Support customizable asset names.
- Support private repositories.
- Support executable compression by [upx](https://github.com/upx/upx).
- Support retry if upload phase fails.
## Features
- Build `Go` binaries for release and publish to Github Release Assets.
- Customizable `Go` versions. `latest` by default.
- Support different `Go` project path in repository.
- Support multiple binaries in same repository.
- Customizable binary name.
- Support multiple `GOOS`/`GOARCH` build in parallel by [Github Action Matrix Strategy](https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idstrategymatrix) gracefully.
- Publish `.zip` instead of `.tar.gz` for `windows`.
- No `musl` library dependency issue on `linux`.
- Support extra command that will be executed before `go build`. You may want to use it to solve dependency if you're NOT using [Go Modules](https://github.com/golang/go/wiki/Modules).
- 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`. Another important usage is to use `make`(`Makefile`) for building on Unix-like systems.
- Support optional `.md5` along with artifacts.
- Support optional `.sha256` along with artifacts.
- Customizable release tag to support publish binaries per `push` or `workflow_dispatch`(manually trigger).
- Support overwrite assets if it's already exist.
- Support customizable asset names.
- Support private repositories.
- Support executable compression by [upx](https://github.com/upx/upx).
- Support retry if upload phase fails.

## Usage

Expand All @@ -31,7 +31,7 @@ Automatically publish `Go` binaries to Github Release Assets through Github Acti
```yaml
# .github/workflows/release.yaml

on:
on:
release:
types: [created]

Expand All @@ -50,7 +50,7 @@ jobs:

### Parameters

| Parameter | **Mandatory**/**Optional** | Description |
| Parameter | **Mandatory**/**Optional** | Description |
| --------- | -------- | ----------- |
| github_token | **Mandatory** | Your `GITHUB_TOKEN` for uploading releases to Github asserts. |
| goos | **Mandatory** | `GOOS` is the running program's operating system target: one of `darwin`, `freebsd`, `linux`, and so on. |
Expand All @@ -70,19 +70,20 @@ jobs:
| overwrite | **Optional** | Overwrite asset if it's already exist. `FALSE` by default. |
| asset_name | **Optional** | Customize asset name if do not want to use the default format `${BINARY_NAME}-${RELEASE_TAG}-${GOOS}-${GOARCH}`. <br>Make sure set it correctly, especially for matrix usage that you have to append `-${{ matrix.goos }}-${{ matrix.goarch }}`. A valid example could be `asset_name: binary-name-${{ matrix.goos }}-${{ matrix.goarch }}`. |
| retry | **Optional** | How many times retrying if upload fails. `3` by default. |
| post_command | **Optional** | Extra command that will be executed for teardown work. e.g. you can use it to upload artifacts to AWS s3 or aliyun OSS |

### Advanced Example

- Release for multiple OS/ARCH in parallel by matrix strategy.
- `Go` code is not in `.` of your repository.
- Customize binary name.
- Release for multiple OS/ARCH in parallel by matrix strategy.
- `Go` code is not in `.` of your repository.
- Customize binary name.
- Use `go 1.13.1` from downloadable URL instead of the default version.
- Package extra `LICENSE` and `README.md` into artifacts.
- Package extra `LICENSE` and `README.md` into artifacts.

```yaml
# .github/workflows/release.yaml

on:
on:
release:
types: [created]

Expand All @@ -95,11 +96,11 @@ jobs:
# build and publish in parallel: linux/386, linux/amd64, linux/arm64, windows/386, windows/amd64, darwin/amd64, darwin/arm64
goos: [linux, windows, darwin]
goarch: ["386", amd64, arm64]
exclude:
exclude:
- goarch: "386"
goos: darwin
goos: darwin
- goarch: arm64
goos: windows
goos: windows
steps:
- uses: actions/checkout@v2
- uses: wangyoucao577/go-release-action@v1.22
Expand All @@ -113,8 +114,8 @@ jobs:
extra_files: LICENSE README.md
```

### More Examples
Welcome share your usage for other people's reference!
### More Examples
Welcome share your usage for other people's reference!
- [wiki/More-Examples](https://github.com/wangyoucao577/go-release-action/wiki/More-Examples)

[:clap:](":clap:")[:clap:](":clap:")[:clap:](":clap:") Enjoy! Welcome [star](https://github.com/wangyoucao577/go-release-action/) if like it[:smile:](:smile:)
[:clap:](":clap:")[:clap:](":clap:")[:clap:](":clap:") Enjoy! Welcome [star](https://github.com/wangyoucao577/go-release-action/) if like it[:smile:](:smile:)
13 changes: 9 additions & 4 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ inputs:
description: 'Where to run `go build .`'
required: false
default: '.'
binary_name:
binary_name:
description: 'Specify another binary name if do not want to use repository basename'
required: false
default: ''
Expand Down Expand Up @@ -67,14 +67,18 @@ inputs:
description: "Overwrite asset if it's already exist."
required: false
default: 'FALSE'
asset_name:
asset_name:
description: 'Customize asset name if do not want to use the default format.'
required: false
default: ''
retry:
retry:
description: 'How many times retrying if upload fails.'
required: false
default: '3'
post_command:
description: 'Extra command that will be executed for teardown work'
required: false
default: ''

runs:
using: 'docker'
Expand All @@ -98,7 +102,8 @@ runs:
- ${{ inputs.overwrite }}
- ${{ inputs.asset_name }}
- ${{ inputs.retry }}
- ${{ inputs.post_command }}

branding:
icon: 'package'
icon: 'package'
color: 'blue'
30 changes: 18 additions & 12 deletions release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ if [ ${INPUT_GOOS} == 'windows' ]; then
EXT='.exe'
fi

# prefix for ldflags
# prefix for ldflags
LDFLAGS_PREFIX=''
if [ ! -z "${INPUT_LDFLAGS}" ]; then
LDFLAGS_PREFIX="-ldflags"
Expand All @@ -55,7 +55,7 @@ if [[ "${INPUT_BUILD_COMMAND}" =~ ^make.* ]]; then
cp ${BINARY_NAME}${EXT} ${BUILD_ARTIFACTS_FOLDER}/
fi
else
GOOS=${INPUT_GOOS} GOARCH=${INPUT_GOARCH} ${INPUT_BUILD_COMMAND} -o ${BUILD_ARTIFACTS_FOLDER}/${BINARY_NAME}${EXT} ${INPUT_BUILD_FLAGS} ${LDFLAGS_PREFIX} "${INPUT_LDFLAGS}"
GOOS=${INPUT_GOOS} GOARCH=${INPUT_GOARCH} ${INPUT_BUILD_COMMAND} -o ${BUILD_ARTIFACTS_FOLDER}/${BINARY_NAME}${EXT} ${INPUT_BUILD_FLAGS} ${LDFLAGS_PREFIX} "${INPUT_LDFLAGS}"
fi


Expand Down Expand Up @@ -83,34 +83,40 @@ ls -lha
# compress and package binary, then calculate checksum
RELEASE_ASSET_EXT='.tar.gz'
MEDIA_TYPE='application/gzip'
RELEASE_ASSET_FILE=${RELEASE_ASSET_NAME}${RELEASE_ASSET_EXT}
if [ ${INPUT_GOOS} == 'windows' ]; then
RELEASE_ASSET_EXT='.zip'
MEDIA_TYPE='application/zip'
( shopt -s dotglob; zip -vr ${RELEASE_ASSET_NAME}${RELEASE_ASSET_EXT} * )
( shopt -s dotglob; zip -vr ${RELEASE_ASSET_FILE} * )
else
( shopt -s dotglob; tar cvfz ${RELEASE_ASSET_NAME}${RELEASE_ASSET_EXT} * )
( shopt -s dotglob; tar cvfz ${RELEASE_ASSET_FILE} * )
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)
MD5_SUM=$(md5sum ${RELEASE_ASSET_FILE} | cut -d ' ' -f 1)
SHA256_SUM=$(sha256sum ${RELEASE_ASSET_FILE} | cut -d ' ' -f 1)

# prefix upload extra params
# 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 -logtostderr -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} -retry ${INPUT_RETRY}
github-assets-uploader -logtostderr -f ${RELEASE_ASSET_FILE} -mediatype ${MEDIA_TYPE} ${GITHUB_ASSETS_UPLOADR_EXTRA_OPTIONS} -repo ${GITHUB_REPOSITORY} -token ${INPUT_GITHUB_TOKEN} -tag ${RELEASE_TAG} -retry ${INPUT_RETRY}
if [ ${INPUT_MD5SUM^^} == 'TRUE' ]; then
MD5_EXT='.md5'
MD5_MEDIA_TYPE='text/plain'
echo ${MD5_SUM} >${RELEASE_ASSET_NAME}${RELEASE_ASSET_EXT}${MD5_EXT}
github-assets-uploader -logtostderr -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} -retry ${INPUT_RETRY}
echo ${MD5_SUM} >${RELEASE_ASSET_FILE}${MD5_EXT}
github-assets-uploader -logtostderr -f ${RELEASE_ASSET_FILE}${MD5_EXT} -mediatype ${MD5_MEDIA_TYPE} ${GITHUB_ASSETS_UPLOADR_EXTRA_OPTIONS} -repo ${GITHUB_REPOSITORY} -token ${INPUT_GITHUB_TOKEN} -tag ${RELEASE_TAG} -retry ${INPUT_RETRY}
fi

if [ ${INPUT_SHA256SUM^^} == 'TRUE' ]; then
SHA256_EXT='.sha256'
SHA256_MEDIA_TYPE='text/plain'
echo ${SHA256_SUM} >${RELEASE_ASSET_NAME}${RELEASE_ASSET_EXT}${SHA256_EXT}
github-assets-uploader -logtostderr -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} -retry ${INPUT_RETRY}
echo ${SHA256_SUM} >${RELEASE_ASSET_FILE}${SHA256_EXT}
github-assets-uploader -logtostderr -f ${RELEASE_ASSET_FILE}${SHA256_EXT} -mediatype ${SHA256_MEDIA_TYPE} ${GITHUB_ASSETS_UPLOADR_EXTRA_OPTIONS} -repo ${GITHUB_REPOSITORY} -token ${INPUT_GITHUB_TOKEN} -tag ${RELEASE_TAG} -retry ${INPUT_RETRY}
fi

# execute post-command if exist, e.g. upload to AWS s3 or aliyun OSS
if [ ! -z "${INPUT_POST_COMMAND}" ]; then
eval ${INPUT_POST_COMMAND}
fi

0 comments on commit 2875b2d

Please sign in to comment.