diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..f276d9d --- /dev/null +++ b/.editorconfig @@ -0,0 +1,12 @@ +root = true + +[*] +charset = utf-8 +end_of_line = lf +indent_size = 2 +indent_style = tab +insert_final_newline = true +trim_trailing_whitespace = true + +[*.yaml] +indent_style = space diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..241fd04 --- /dev/null +++ b/LICENSE @@ -0,0 +1,20 @@ +The MIT License (MIT) + +Copyright (c) 2021 Peter Mescalchin + +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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..7829c1d --- /dev/null +++ b/README.md @@ -0,0 +1,50 @@ +# Action Golang with cache + +Composite GitHub Action which combines the perfect pairing of [actions/setup-go](https://github.com/actions/setup-go) with [actions/cache](https://github.com/actions/cache) for the caching of both the Golang module and build caches. + +![nuts and gum](https://user-images.githubusercontent.com/1818757/134792061-2fb04549-ed6d-4e4d-a805-3de6ea90f261.jpg) + +Reducing all these workflow steps: + +```yaml +steps: + - name: Setup Golang + uses: actions/setup-go@v2 + with: + go-version: ~1.17 + - name: Setup Golang caches + uses: actions/cache@v2 + with: + path: | + ~/.cache/go-build + ~/go/pkg/mod + key: ${{ runner.os }}-golang-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ runner.os }}-golang- +``` + +down to this: + +```yaml +steps: + - name: Setup Golang with cache + uses: magnetikonline/action-golang-cache@v1 + with: + go-version: ~1.17 +``` + +Action correctly sets the build and module cache paths for Linux, macOS _and_ Windows runners as per the [Golang cache examples](https://github.com/actions/cache/blob/main/examples.md#go---modules). + +In addition an optional `cache-key-suffix` input can be used to control the generated cache key - handy where a Golang program is compiled multiple times for different binaries across multiple workflow definitions - resulting in different optimized build cache path contents: + +```yaml +steps: + - name: Testing action + uses: magnetikonline/action-golang-cache@v1 + with: + go-version: ~1.17 + cache-key-suffix: -apples + +# cache key: ${{ runner.os }}-golang-apples-${{ hashFiles('**/go.sum') }} +# restore key: ${{ runner.os }}-golang-apples- +``` diff --git a/action.yaml b/action.yaml new file mode 100644 index 0000000..93f3778 --- /dev/null +++ b/action.yaml @@ -0,0 +1,46 @@ +name: Golang with cache +author: Peter Mescalchin +description: Setup requested Golang version with enabled module and build caching. + +inputs: + go-version: + description: The desired Go version to use. + required: true + cache-key-suffix: + description: Optional cache key suffix. + default: + +runs: + using: composite + steps: + - name: Setup Golang + uses: actions/setup-go@v2 + with: + go-version: ${{ inputs.go-version }} + - name: Determine OS Golang paths + id: golang-path + run: | + buildPath="~/.cache/go-build" + modulePath="~/go/pkg/mod" + + if [[ $RUNNER_OS == "macOS" ]]; then + buildPath="~/Library/Caches/go-build" + fi + + if [[ $RUNNER_OS == "Windows" ]]; then + buildPath="~\AppData\Local\go-build" + modulePath="~\go\pkg\mod" + fi + + echo "::set-output name=build::$buildPath" + echo "::set-output name=module::$modulePath" + shell: bash + - name: Setup Golang caches + uses: actions/cache@v2 + with: + path: | + ${{ steps.golang-path.outputs.build }} + ${{ steps.golang-path.outputs.module }} + key: ${{ runner.os }}-golang${{ inputs.cache-key-suffix }}-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ runner.os }}-golang${{ inputs.cache-key-suffix }}-