diff --git a/.gitattributes b/.gitattributes index 541fd55..4994dee 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1 +1,2 @@ +* text=auto .licenses/** -diff linguist-generated=true \ No newline at end of file diff --git a/.github/workflows/check-dist.yml b/.github/workflows/check-dist.yml index 0816ccb..7be140a 100644 --- a/.github/workflows/check-dist.yml +++ b/.github/workflows/check-dist.yml @@ -21,10 +21,10 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Set Node.js 16.x - uses: actions/setup-node@v2 + uses: actions/setup-node@v3 with: node-version: 16.x @@ -44,7 +44,7 @@ jobs: id: diff # If index.js was different than expected, upload the expected version as an artifact - - uses: actions/upload-artifact@v2 + - uses: actions/upload-artifact@v3 if: ${{ failure() && steps.diff.conclusion == 'failure' }} with: name: dist diff --git a/.github/workflows/licensed.yml b/.github/workflows/licensed.yml index 55402d9..3662014 100644 --- a/.github/workflows/licensed.yml +++ b/.github/workflows/licensed.yml @@ -13,9 +13,9 @@ jobs: runs-on: ubuntu-latest name: Check licenses steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Set Node.js 16.x - uses: actions/setup-node@v2 + uses: actions/setup-node@v3 with: node-version: 16.x - run: npm ci diff --git a/.github/workflows/versions.yml b/.github/workflows/versions.yml index c0bb369..79683ec 100644 --- a/.github/workflows/versions.yml +++ b/.github/workflows/versions.yml @@ -22,7 +22,7 @@ jobs: go: [1.12, 1.13, 1.14] steps: - name: Checkout - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: setup-go ${{ matrix.go }} uses: ./ @@ -41,7 +41,7 @@ jobs: os: [ubuntu-latest, windows-latest, macos-latest] go-version: [1.16, 1.17] steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Setup Go and check latest uses: ./ with: @@ -60,13 +60,13 @@ jobs: go: [1.12.16, 1.13.11, 1.14.3] steps: - name: Checkout - uses: actions/checkout@v2 - + uses: actions/checkout@v3 + - name: setup-go ${{ matrix.go }} uses: ./ with: go-version: ${{ matrix.go }} - + - name: verify go run: __tests__/verify-go.sh ${{ matrix.go }} shell: bash @@ -81,7 +81,7 @@ jobs: go: [1.9, 1.8.6] steps: - name: Checkout - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: setup-go ${{ matrix.go }} uses: ./ diff --git a/.github/workflows/workflow.yml b/.github/workflows/workflow.yml index 9e18f39..df67fa8 100644 --- a/.github/workflows/workflow.yml +++ b/.github/workflows/workflow.yml @@ -17,10 +17,10 @@ jobs: operating-system: [ubuntu-latest, windows-latest] steps: - name: Checkout - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: Setup node 16 - uses: actions/setup-node@v2 + uses: actions/setup-node@v3 with: node-version: 16 cache: npm diff --git a/CONDUCT b/CODE_OF_CONDUCT.md similarity index 96% rename from CONDUCT rename to CODE_OF_CONDUCT.md index 517657b..1f045f4 100644 --- a/CONDUCT +++ b/CODE_OF_CONDUCT.md @@ -55,7 +55,7 @@ a project may be further defined and clarified by project maintainers. ## Enforcement Instances of abusive, harassing, or otherwise unacceptable behavior may be -reported by contacting the project team at opensource@github.com. All +reported by contacting the project team at opensource+actions/setup-go@github.com. All complaints will be reviewed and investigated and will result in a response that is deemed necessary and appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. @@ -73,4 +73,4 @@ available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.ht [homepage]: https://www.contributor-covenant.org For answers to common questions about this code of conduct, see -https://www.contributor-covenant.org/faq \ No newline at end of file +https://www.contributor-covenant.org/faq diff --git a/README.md b/README.md index 01bee17..4d497f6 100644 --- a/README.md +++ b/README.md @@ -1,28 +1,33 @@ # setup-go -

- GitHub Actions status - - versions status -

+[![build-test](https://github.com/actions/setup-go/actions/workflows/workflow.yml/badge.svg)](https://github.com/actions/setup-go/actions/workflows/workflow.yml) +[![Validate 'setup-go'](https://github.com/actions/setup-go/actions/workflows/versions.yml/badge.svg)](https://github.com/actions/setup-go/actions/workflows/versions.yml) This action sets up a go environment for use in actions by: -- optionally downloading and caching a version of Go by version and adding to PATH -- registering problem matchers for error output +- Optionally downloading and caching a version of Go by version and adding to `PATH`. +- Registering problem matchers for error output. # V3 -The V3 offers: -- Adds GOBIN to the PATH -- Proxy Support +The V3 edition of the action offers: + +- Adds `GOBIN` to the `PATH` +- Proxy support - Check latest version +<<<<<<< HEAD - Caching packages dependencies - Bug Fixes (including issues around version matching and semver) +======= +- Bug fixes (including issues around version matching and semver) +>>>>>>> main The action will first check the local cache for a version match. If a version is not found locally, it will pull it from the `main` branch of the [go-versions](https://github.com/actions/go-versions/blob/main/versions-manifest.json) repository. On miss or failure, it will fall back to downloading directly from [go dist](https://storage.googleapis.com/golang). To change the default behavior, please use the [check-latest input](#check-latest-version). +**Note:** The `setup-go` action uses executable binaries which are built by Golang side. The action does not build golang from source code. + Matching by [semver spec](https://github.com/npm/node-semver): + ```yaml steps: - uses: actions/checkout@v3 @@ -42,6 +47,7 @@ steps: ``` Matching an unstable pre-release: + ```yaml steps: - uses: actions/checkout@v3 @@ -64,7 +70,8 @@ steps: See [action.yml](action.yml) -## Basic: +## Basic + ```yaml steps: - uses: actions/checkout@v3 @@ -75,7 +82,7 @@ steps: ``` -## Check latest version: +## Check latest version The `check-latest` flag defaults to `false`. Use the default or set `check-latest` to `false` if you prefer stability and if you want to ensure a specific Go version is always used. @@ -93,38 +100,8 @@ steps: - run: go run hello.go ``` -## Caching dependency files and build outputs: +## Matrix testing -The action has a built-in functionality for caching and restoring go modules and build outputs. It uses [actions/cache](https://github.com/actions/cache) under the hood but requires less configuration settings.The `cache` input is optional, and caching is turned off by default. - -The action defaults to search for the dependency file - go.sum in the repository root, and uses its hash as a part of the cache key. Use `cache-dependency-path` input for cases when multiple dependency files are used, or they are located in different subdirectories. - -**Caching without specifying dependency file path** -```yaml -steps: - - uses: actions/checkout@v3 - - uses: actions/setup-go@v3 - with: - go-version: '1.17' - check-latest: true - cache: true - - run: go run hello.go -``` - -**Caching in monorepos** -```yaml -steps: - - uses: actions/checkout@v3 - - uses: actions/setup-go@v3 - with: - go-version: '1.17' - check-latest: true - cache: true - cache-dependency-path: subdir/go.sum - - run: go run hello.go -``` - -## Matrix Testing: ```yaml jobs: build: @@ -143,11 +120,13 @@ jobs: ``` ### Supported version syntax + The `go-version` input supports the following syntax: -Specific versions: `1.15`, `1.16.1`, `1.17.0-rc.2`, `1.16.0-beta.1` -SemVer's version range syntax: `^1.13.1`, `>=1.18.0-rc.1` -For more information about semantic versioning please refer [semver](https://github.com/npm/node-semver) documentation +- Specific versions: `1.15`, `1.16.1`, `1.17.0-rc.2`, `1.16.0-beta.1` +- SemVer's version range syntax: `^1.13.1`, `>=1.18.0-rc.1` + +For more information about semantic versioning, please refer to [semver](https://github.com/npm/node-semver) documentation. # License @@ -155,8 +134,8 @@ The scripts and documentation in this project are released under the [MIT Licens # Contributions -Contributions are welcome! See [Contributor's Guide](docs/contributors.md) +Contributions are welcome! See [Contributor's Guide](docs/contributors.md) ## Code of Conduct -:wave: Be nice. See [our code of conduct](CONDUCT) +:wave: Be nice. See [our code of conduct](CONDUCT) diff --git a/__tests__/setup-go.test.ts b/__tests__/setup-go.test.ts index 92c6531..dc67d3c 100644 --- a/__tests__/setup-go.test.ts +++ b/__tests__/setup-go.test.ts @@ -105,6 +105,11 @@ describe('setup-go', () => { jest.restoreAllMocks(); }, 100000); + it('can extract the major.minor.patch version from a given Go version string', async () => { + const goVersionOutput = 'go version go1.16.6 darwin/amd64'; + expect(main.parseGoVersion(goVersionOutput)).toBe('1.16.6'); + }); + it('can find 1.9.7 from manifest on osx', async () => { os.platform = 'darwin'; os.arch = 'x64'; diff --git a/action.yml b/action.yml index a83667d..4d99467 100644 --- a/action.yml +++ b/action.yml @@ -1,7 +1,7 @@ name: 'Setup Go environment' description: 'Setup a Go environment and add it to the PATH' author: 'GitHub' -inputs: +inputs: go-version: description: 'The Go version to download (if necessary) and use. Supports semver spec and ranges.' check-latest: @@ -16,6 +16,8 @@ inputs: cache-dependency-path: description: 'Used to specify the path to a dependency file - go.sum' outputs: + go-version: + description: 'The installed Go version. Useful when given a version range as input.' cache-hit: description: 'A boolean value to indicate if a cache was hit' runs: diff --git a/docs/adrs/0000-caching-dependencies.md b/docs/adrs/0000-caching-dependencies.md new file mode 100644 index 0000000..dc8d858 --- /dev/null +++ b/docs/adrs/0000-caching-dependencies.md @@ -0,0 +1,69 @@ +# 0. Caching dependencies +Date: 2022-04-13 + +Status: Accepted + +# Context +`actions/setup-go` is the one of the most popular action related to Golang in GitHub Actions. Many customers use it in conjunction with [actions/cache](https://github.com/actions/cache) to speed up dependency installation process. +See more examples on proper usage in [actions/cache documentation](https://github.com/actions/cache/blob/main/examples.md#go---modules). + +# Goals & Anti-Goals +Integration of caching functionality into `actions/setup-go` action will bring the following benefits for action users: +- Decrease the entry threshold for using the cache for Go dependencies and simplify initial configuration +- Simplify YAML pipelines because there will be no need for additional steps to enable caching +- More users will use cache for Go so more customers will have fast builds! + +We don't pursue the goal to provide wide customization of caching in scope of `actions/setup-go` action. The purpose of this integration is covering ~90% of basic use-cases. If user needs flexible customization, we should advice them to use `actions/cache` directly. + +# Decision +- Add `cache` input parameter to `actions/setup-go`. For now, input will accept the following values: + - `true` - enable caching for go dependencies + - `false`- disable caching for go dependencies. This value will be set as default value +- Cache feature will be disabled by default to make sure that we don't break existing customers. We will consider enabling cache by default in next major releases +- Action will try to search a go.sum files in the repository and throw error in the scenario that it was not found +- The hash of found file will be used as cache key (the same approach like [actions/cache](https://github.com/actions/cache/blob/main/examples.md#go---modules) recommends) +- The following key cache will be used `${{ runner.os }}-go${{ go-version }}-${{ hashFiles('') }}` +- Action will cache global cache from the `go env GOMODCACHE` and `go env GOCACHE` commands. +- Add a `cache-dependency-path` input parameter to `actions/setup-go`. The new input will accept an array or regex of dependency files. The field will accept a path (relative to the repository root) to dependency files. If the provided path contains wildcards, the action will search all matching files and calculate a common hash like the ${{ hashFiles('**/go.sum') }} YAML construction does. + +# Example of real use-cases + + - With cache + +```yml +steps: +- uses: actions/checkout@v3 +- uses: actions/setup-go@v3 + with: + go-version: '18' + cache: true +``` + + - With cache-dependency-path + + ```yml +steps: +- uses: actions/checkout@v3 +- uses: actions/setup-go@v3 + with: + go-version: '18' + cache: true + cache-dependency-path: **/go.sum +``` + + ```yml +steps: +- uses: actions/checkout@v3 +- uses: actions/setup-go@v3 + with: + go-version: '18' + cache: true + cache-dependency-path: | + **/go.sum + **/go.mod +``` + +# Release process + +As soon as functionality is implemented, we will release minor update of action. No need to bump major version since there are no breaking changes for existing users. +After that, we will update [starter-workflows](https://github.com/actions/starter-workflows/blob/main/ci/go.yml) \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index 22bb7bc..bd16c41 100644 --- a/src/main.ts +++ b/src/main.ts @@ -56,6 +56,8 @@ export async function run() { let goVersion = (cp.execSync(`${goPath} version`) || '').toString(); core.info(goVersion); + core.setOutput('go-version', parseGoVersion(goVersion)); + core.startGroup('go env'); let goEnv = (cp.execSync(`${goPath} env`) || '').toString(); core.info(goEnv); @@ -75,7 +77,7 @@ export async function addBinToPath(): Promise { } let buf = cp.execSync('go env GOPATH'); - if (buf) { + if (buf.length > 1) { let gp = buf.toString().trim(); core.debug(`go env GOPATH :${gp}:`); if (!fs.existsSync(gp)) { @@ -95,3 +97,11 @@ export async function addBinToPath(): Promise { } return added; } + +export function parseGoVersion(versionString: string): string { + // get the installed version as an Action output + // based on go/src/cmd/go/internal/version/version.go: + // fmt.Printf("go version %s %s/%s\n", runtime.Version(), runtime.GOOS, runtime.GOARCH) + // expecting go for runtime.Version() + return versionString.split(' ')[2].slice('go'.length); +}