setup-go/README.md

355 lines
9.2 KiB
Markdown
Raw Normal View History

2025-09-22 15:35:30 +05:30
# Setup Go Action
2019-06-19 09:44:17 -04:00
2022-12-23 12:03:14 +09:00
[![Basic validation](https://github.com/actions/setup-go/actions/workflows/basic-validation.yml/badge.svg)](https://github.com/actions/setup-go/actions/workflows/basic-validation.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)
2019-08-12 15:13:31 -04:00
2025-09-22 15:00:07 +05:30
This action sets up a Go environment for use in GitHub Actions by:
2025-09-22 15:35:30 +05:30
- Optionally downloading and caching a version of Go by version and adding to PATH
- Registering problem matchers for error output
- Providing intelligent caching for Go modules and build outputs
2019-06-19 09:44:17 -04:00
2025-09-22 15:35:30 +05:30
## Quick Start
2019-07-17 10:50:58 -04:00
2025-09-22 15:35:30 +05:30
```yaml
steps:
- uses: actions/checkout@v5
- uses: actions/setup-go@v6
with:
go-version: '1.21'
- run: go version
```
2025-09-22 15:35:30 +05:30
## Breaking Changes
### V6 Changes
2025-09-19 14:53:21 +05:30
2025-09-22 15:00:07 +05:30
#### Node Runtime Upgrade
2025-09-22 15:35:30 +05:30
- **Upgraded from Node 20 to Node 24**
- ⚠️ **Action Required**: Ensure your runner is on version v2.327.1 or later for compatibility
- See [Release Notes](https://github.com/actions/runner/releases) for more details
2025-09-19 14:53:21 +05:30
2025-09-22 15:35:30 +05:30
#### Enhanced Go Toolchain Management
2025-09-19 14:53:21 +05:30
2025-09-22 15:35:30 +05:30
V6 introduces significant improvements for reliable and consistent Go version selection:
2025-09-19 14:53:21 +05:30
2025-09-22 15:35:30 +05:30
**Toolchain Directive Support**
2025-09-22 15:00:07 +05:30
Now correctly interprets both `go` and `toolchain` directives from `go.mod`:
```go
2025-09-19 14:53:21 +05:30
go 1.21.0 // Minimum required version
toolchain go1.21.6 // V6 uses this exact version
```
2025-09-22 15:35:30 +05:30
**Advanced Version Resolution**
2025-09-22 15:00:07 +05:30
Supports comprehensive version patterns:
2025-09-22 15:35:30 +05:30
- Comparison operators: `>=1.21.0`, `<1.22.0`
- Semantic versioning: `~1.21.0` (patch updates), `^1.21.0` (minor updates)
- Wildcards: `1.21.x`, `1.*`
2025-09-19 14:53:21 +05:30
2025-09-22 15:35:30 +05:30
**Intelligent Caching**
Cache keys now incorporate toolchain-specific metadata, eliminating version conflicts when switching between Go versions in your workflows.
2025-09-19 14:53:21 +05:30
2025-09-22 15:35:30 +05:30
**Migration Impact**
These changes ensure your workflows use the exact Go version your project requires, improving build reproducibility and reducing version-related issues.
2025-09-22 15:00:07 +05:30
For more details, see the [full release notes](https://github.com/actions/setup-go/releases).
2025-09-22 15:00:07 +05:30
### V5 Changes
2025-09-22 15:35:30 +05:30
- Upgraded Node.js runtime from node16 to node20
2025-09-22 15:35:30 +05:30
- See [full release notes](https://github.com/actions/setup-go/releases) for complete details
2025-09-22 15:35:30 +05:30
## Version Resolution Behavior
2025-09-22 15:35:30 +05:30
The action follows this resolution order:
1. **Local cache** - Checks for a cached version match
2. **go-versions repository** - Pulls from the main branch of the [go-versions repository](https://github.com/actions/go-versions)
3. **Direct download** - Falls back to downloading directly from [go.dev](https://go.dev/dl/)
2025-09-22 15:35:30 +05:30
To change the default behavior, use the `check-latest` input.
2022-04-20 16:11:14 +02:00
2025-09-22 15:35:30 +05:30
> **Note**: The setup-go action uses executable binaries built by the Golang team. The action does not build Go from source code.
## Usage Examples
### Basic Usage
2020-02-09 14:39:34 -05:00
```yaml
steps:
- uses: actions/checkout@v5
- uses: actions/setup-go@v6
2021-11-08 19:28:08 +09:00
with:
2025-09-22 15:35:30 +05:30
go-version: '1.21'
2025-09-22 15:00:07 +05:30
- run: go run hello.go
2020-02-09 14:39:34 -05:00
```
2025-09-22 15:35:30 +05:30
### Version Specifications
2025-09-22 15:00:07 +05:30
2025-09-22 15:35:30 +05:30
#### Semantic Versioning
2025-09-22 15:00:07 +05:30
```yaml
steps:
- uses: actions/checkout@v5
- uses: actions/setup-go@v6
with:
2025-09-22 15:35:30 +05:30
go-version: '^1.21.1' # Latest patch release of 1.21.x
- run: go version
```
2020-02-10 19:18:01 -05:00
```yaml
steps:
- uses: actions/checkout@v5
- uses: actions/setup-go@v6
with:
2025-09-22 15:35:30 +05:30
go-version: '>=1.20.0' # Version 1.20.0 or higher
- run: go version
```
2025-09-22 15:35:30 +05:30
> **Important**: Due to YAML parsing behavior, always wrap version numbers in single quotes:
2025-09-22 15:00:07 +05:30
> ```yaml
2025-09-22 15:35:30 +05:30
> go-version: '1.20' # Correct
> go-version: 1.20 # Incorrect - YAML parser interprets as 1.2
2025-09-22 15:00:07 +05:30
> ```
#### Pre-release Versions
```yaml
steps:
- uses: actions/checkout@v5
- uses: actions/setup-go@v6
2021-11-08 19:28:08 +09:00
with:
2025-09-22 15:35:30 +05:30
go-version: '1.22.0-rc.1'
2021-11-08 19:28:08 +09:00
- run: go version
2020-02-10 19:18:01 -05:00
```
2025-09-22 15:35:30 +05:30
#### Version Aliases
**Stable Release**
2019-07-17 10:50:58 -04:00
```yaml
2019-07-25 21:28:46 -04:00
steps:
- uses: actions/checkout@v5
- uses: actions/setup-go@v6
with:
2025-09-22 15:35:30 +05:30
go-version: 'stable' # Latest stable version
2025-09-22 15:00:07 +05:30
- run: go version
```
2025-09-22 15:35:30 +05:30
**Previous Stable Release**
```yaml
steps:
- uses: actions/checkout@v5
- uses: actions/setup-go@v6
with:
go-version: 'oldstable' # Previous stable version
- run: go version
```
2025-09-22 15:35:30 +05:30
> **Note**: Using aliases is equivalent to using the corresponding minor release with `check-latest: true`
2025-09-22 15:35:30 +05:30
### Version from go.mod File
2025-09-22 15:35:30 +05:30
The action can automatically detect the Go version from your project's `go.mod` or `go.work` file:
```yaml
steps:
- uses: actions/checkout@v5
- uses: actions/setup-go@v6
2021-11-08 19:28:08 +09:00
with:
2025-09-22 15:35:30 +05:30
go-version-file: 'go.mod'
- run: go version
2019-07-17 10:50:58 -04:00
```
2025-09-22 15:35:30 +05:30
**Version Resolution from go.mod:**
1. Uses the `toolchain` directive version if present
2. Falls back to the `go` directive version
3. If no patch version is specified, uses the latest available patch
> **Note**: If both `go-version` and `go-version-file` are provided, `go-version` takes precedence.
2025-09-22 15:35:30 +05:30
### Check Latest Version
```yaml
steps:
- uses: actions/checkout@v5
- uses: actions/setup-go@v6
with:
2025-09-22 15:35:30 +05:30
go-version: '1.21'
check-latest: true # Always check for the latest patch release
- run: go version
```
2025-09-22 15:35:30 +05:30
**Performance Considerations:**
- `check-latest: false` (default) - Uses cached versions for faster builds
- `check-latest: true` - Downloads the latest version, slower but ensures up-to-date releases
### Caching
#### Automatic Caching
Caching is enabled by default and automatically handles:
- Go modules (based on `go.sum`)
- Build outputs
- Toolchain-specific metadata
```yaml
steps:
- uses: actions/checkout@v5
- uses: actions/setup-go@v6
with:
2025-09-22 15:35:30 +05:30
go-version: '1.21'
# cache: true (default)
- run: go run hello.go
```
2025-09-22 15:35:30 +05:30
#### Custom Cache Dependencies
2025-09-22 15:35:30 +05:30
For projects with multiple dependency files or monorepos:
```yaml
steps:
- uses: actions/checkout@v5
- uses: actions/setup-go@v6
with:
2025-09-22 15:35:30 +05:30
go-version: '1.21'
cache-dependency-path: |
2025-09-22 15:00:07 +05:30
subdir/go.sum
tools/go.sum
- run: go run hello.go
2025-09-22 15:00:07 +05:30
```
2025-09-22 15:35:30 +05:30
**Using Glob Patterns:**
```yaml
cache-dependency-path: "**/*.sum"
```
2022-05-12 17:04:39 +09:00
2025-09-22 15:35:30 +05:30
#### Disable Caching
2022-05-12 17:04:39 +09:00
```yaml
steps:
- uses: actions/checkout@v5
- uses: actions/setup-go@v6
with:
2025-09-22 15:35:30 +05:30
go-version: '1.21'
cache: false
- run: go run hello.go
2022-05-12 17:04:39 +09:00
```
2019-07-17 10:50:58 -04:00
2025-09-22 15:00:07 +05:30
### Matrix Testing
2025-09-22 15:35:30 +05:30
Test across multiple Go versions:
2019-07-17 10:50:58 -04:00
```yaml
jobs:
2025-09-22 15:35:30 +05:30
test:
runs-on: ubuntu-latest
2019-07-17 10:50:58 -04:00
strategy:
matrix:
2025-09-22 15:35:30 +05:30
go-version: ['1.20', '1.21', '1.22']
2019-07-25 21:28:46 -04:00
steps:
- uses: actions/checkout@v5
2025-09-22 15:35:30 +05:30
- uses: actions/setup-go@v6
2019-07-17 10:50:58 -04:00
with:
2025-09-22 15:35:30 +05:30
go-version: ${{ matrix.go-version }}
- run: go test ./...
2019-07-17 10:50:58 -04:00
```
2019-06-19 09:44:17 -04:00
2025-09-22 15:35:30 +05:30
## Advanced Configuration
2025-09-22 15:00:07 +05:30
2025-09-22 15:35:30 +05:30
### Supported Version Syntax
2025-09-22 15:00:07 +05:30
2025-09-22 15:35:30 +05:30
| Syntax Type | Example | Description |
|-------------|---------|-------------|
| Specific version | `1.21.5` | Exact version |
| Semantic range | `^1.21.0` | Compatible with 1.21.0 |
| Comparison operators | `>=1.20.0` | Version 1.20.0 or higher |
| Wildcards | `1.21.x` | Latest patch of 1.21 |
| Pre-release | `1.22.0-rc.1` | Release candidate |
| Aliases | `stable`, `oldstable` | Latest stable versions |
2025-09-22 15:35:30 +05:30
For more information about semantic versioning, see the [semver documentation](https://semver.org/).
2025-09-22 15:35:30 +05:30
### Complete Input Reference
2025-09-22 15:35:30 +05:30
```yaml
- uses: actions/setup-go@v6
with:
# Version or version range of Go to use
go-version: '1.21'
# Path to go.mod or go.work file
go-version-file: 'go.mod'
# Check for latest version
check-latest: false
# GitHub token for authentication
token: ${{ github.token }}
# Enable/disable caching
cache: true
# Path to dependency files for caching
cache-dependency-path: 'go.sum'
# Architecture to install (auto-detected if not specified)
architecture: 'x64'
```
2025-09-22 15:35:30 +05:30
## Platform Support
2025-09-22 15:35:30 +05:30
### GitHub Enterprise Server (GHES)
2025-09-22 15:00:07 +05:30
2025-09-22 15:35:30 +05:30
When using setup-go on GHES:
2025-09-22 15:00:07 +05:30
2025-09-22 15:35:30 +05:30
1. **Pre-installed**: The action comes pre-installed if Actions is enabled
2. **Rate limits**: Unauthenticated requests are limited to 60/hour per IP
3. **Authentication**: Use a personal access token for higher rate limits:
```yaml
2025-09-22 15:35:30 +05:30
- uses: actions/setup-go@v6
with:
token: ${{ secrets.GH_DOTCOM_TOKEN }}
go-version: '1.21'
```
2025-09-22 15:35:30 +05:30
4. **Offline runners**: For runners without internet access, see [Setting up the tool cache on self-hosted runners](https://docs.github.com/en/actions/hosting-your-own-runners/managing-self-hosted-runners/about-self-hosted-runners#using-self-hosted-runners-in-a-workflow)
2025-09-22 15:00:07 +05:30
2025-09-22 15:35:30 +05:30
### Self-hosted Runners
2025-09-22 15:35:30 +05:30
For self-hosted runners without internet access, ensure Go versions are pre-cached in the runner's tool cache.
2025-01-16 08:47:07 -06:00
2025-09-22 15:35:30 +05:30
## Recommended Permissions
2025-01-16 08:47:07 -06:00
```yaml
permissions:
2025-09-22 15:35:30 +05:30
contents: read # Required to checkout code and install dependencies
2025-01-16 08:47:07 -06:00
```
2025-09-22 15:35:30 +05:30
## Troubleshooting
2025-09-22 15:00:07 +05:30
2025-09-22 15:35:30 +05:30
### Common Issues
2025-09-22 15:00:07 +05:30
2025-09-22 15:35:30 +05:30
**Version not found**
- Ensure the version exists in the [Go releases](https://go.dev/dl/)
- Check if you're using the correct version format
- Try using `check-latest: true`
2025-09-22 15:00:07 +05:30
2025-09-22 15:35:30 +05:30
**Caching issues**
- Cache conflicts may occur when switching Go versions
- V6 resolves this with toolchain-specific cache keys
- Manually clear cache if needed using GitHub Actions cache management
2025-09-22 15:00:07 +05:30
2025-09-22 15:35:30 +05:30
**YAML parsing**
- Always wrap version numbers in single quotes
- Avoid bare numbers that YAML might misinterpret
2025-09-22 15:00:07 +05:30
2025-09-22 15:35:30 +05:30
## License
2019-06-19 09:44:17 -04:00
2025-09-22 15:35:30 +05:30
The scripts and documentation in this project are released under the [MIT License](LICENSE).
2019-06-19 09:44:17 -04:00
2025-09-22 15:35:30 +05:30
## Contributions
2019-06-19 09:44:17 -04:00
2025-09-22 15:35:30 +05:30
Contributions are welcome! See our [Contributor's Guide](docs/contributors.md) for details.
2020-02-09 00:21:39 -05:00
2025-09-22 15:35:30 +05:30
## Code of Conduct
2020-02-09 00:21:39 -05:00
2025-09-22 15:35:30 +05:30
👋 Be nice. See our [Code of Conduct](CODE_OF_CONDUCT.md).