|
1 | 1 | # Go version action
|
2 | 2 |
|
3 |
| -> Provide go versions for use in your workflow |
| 3 | +A GitHub action for using the latest released Go version and the |
| 4 | +minimal support Go version (from go.mod) and a build matrix of them |
| 5 | +and all versions in between. |
| 6 | + |
| 7 | +## Motive |
| 8 | + |
| 9 | +Being consistent is hard. |
| 10 | + |
| 11 | +I used the hard code the Go versions my projects needed to test and |
| 12 | +builds against in my GitHub Actions workflow files. |
| 13 | + |
| 14 | +Of course the result was that I used different versions in the |
| 15 | +`go.mod` file and my workflow files. And whenever a new version of Go |
| 16 | +was released I forgot to add the new version to my build matrix and my |
| 17 | +projects weren't tested on the new release(s). |
| 18 | + |
| 19 | +So I build this action. |
| 20 | + |
| 21 | +The action reads the minimal supported Go version from your `go.mod` |
| 22 | +file and exposes it as a variable to you workflow. |
| 23 | + |
| 24 | +It also pulls the list of release tags from |
| 25 | +https://github.com/golang/go and exposes the latest released Go |
| 26 | +version as a variable as well. |
| 27 | + |
| 28 | +From the list of released go versions and the minimal version your |
| 29 | +module supports we also build a "matrix" variable to be used as a |
| 30 | +build matrix. |
| 31 | + |
| 32 | +While we are at it we also extract the modules path from the `go.mod` |
| 33 | +file even though it hasn't really anything to do with versions ;) |
4 | 34 |
|
5 | 35 | ## Inputs
|
6 | 36 |
|
| 37 | +If your `go mod` file is located in a non standard location you can |
| 38 | +specify the working directory where it is located: |
| 39 | + |
7 | 40 | ```yaml
|
8 |
| - working-directory: |
9 |
| - description: Working direcory where you go.mod file is located |
10 |
| - required: false |
11 |
| - default: . |
| 41 | +working-directory: |
| 42 | + description: Working direcory where you go.mod file is located |
| 43 | + required: false |
| 44 | + default: . |
12 | 45 | ```
|
13 | 46 |
|
14 | 47 | ## Outputs
|
15 | 48 |
|
16 | 49 | ```yaml
|
17 |
| - latest: |
18 |
| - description: The latest go version |
19 |
| - minimal: |
20 |
| - description: The minial go version (as specified by go.mod) |
21 |
| - matrix: |
22 |
| - description: An array of go versions from the minimum supported version to the latest released version |
23 |
| - module: |
24 |
| - description: The go module name (as specified by go.mod) |
| 50 | +latest: |
| 51 | + description: The latest go version |
| 52 | +minimal: |
| 53 | + description: The minial go version (as specified by go.mod) |
| 54 | +matrix: |
| 55 | + description: An (stringified) array of go versions from the minimum supported version to the latest released version |
| 56 | +module: |
| 57 | + description: The go module name (as specified by go.mod) |
| 58 | +``` |
| 59 | +
|
| 60 | +## Examples |
| 61 | +
|
| 62 | +Let's say your `go.mod` specifies go 1.13 as the minimal supported |
| 63 | +version and you want your workflow to setup go version 1.13 using the |
| 64 | +[actions/setup-go](https://github.com/actions/setup-go) action: |
| 65 | + |
| 66 | +```yaml |
| 67 | +name: My go workflow |
| 68 | +on: pull_request |
| 69 | +
|
| 70 | +jobs: |
| 71 | + my-go-workflow: |
| 72 | + runs-on: ubuntu-latest |
| 73 | + steps: |
| 74 | + - uses: actions/checkout@v2 |
| 75 | + - uses: arnested/go-version-action@v1 |
| 76 | + id: go-version |
| 77 | + - name: Install Go ${{ steps.go-version.outputs.minimal }} |
| 78 | + uses: actions/setup-go@v2 |
| 79 | + with: |
| 80 | + go-version: ${{ steps.go-version.outputs.minimal }} |
| 81 | +``` |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | +If you want do matrix test of all Go versions from your minimally |
| 86 | +supported version up to the latest released version we need to do a |
| 87 | +bit more. |
| 88 | + |
| 89 | +We have to run the version lookup as a separate job and let the test |
| 90 | +job depend on it: |
| 91 | + |
| 92 | +```yaml |
| 93 | +on: push |
| 94 | +name: Test |
| 95 | +
|
| 96 | +jobs: |
| 97 | + go-versions: |
| 98 | + name: Lookup go versions |
| 99 | + runs-on: ubuntu-latest |
| 100 | + outputs: |
| 101 | + matrix: ${{ steps.versions.outputs.matrix }} |
| 102 | + steps: |
| 103 | + - uses: actions/checkout@v2 |
| 104 | + - uses: arnested/go-version-action@v1 |
| 105 | + id: versions |
| 106 | + test: |
| 107 | + name: Test |
| 108 | + runs-on: ubuntu-latest |
| 109 | + needs: go-versions |
| 110 | + strategy: |
| 111 | + matrix: |
| 112 | + version: ${{ fromJSON(needs.go-versions.outputs.matrix) }} |
| 113 | + steps: |
| 114 | + - uses: actions/checkout@v2 |
| 115 | + - name: Install Go |
| 116 | + uses: actions/setup-go@v2 |
| 117 | + with: |
| 118 | + go-version: ${{ matrix.version }} |
| 119 | + - name: go test |
| 120 | + run: go test -v -race -cover -covermode=atomic -coverprofile=coverage.txt ./... |
25 | 121 | ```
|
| 122 | + |
| 123 | + |
0 commit comments