How do I cache module downloads in CI?

Caching module downloads in Continuous Integration (CI) can greatly improve build times and reduce the load on your internet connection. By storing previously downloaded modules, you can avoid regenerating the same artifacts over and over again. This guide illustrates how to achieve effective caching in your CI pipeline using tools such as GitHub Actions, GitLab CI, or CircleCI.

Using GitHub Actions

In GitHub Actions, you can use the `actions/cache` action to cache your Go module files. Below is an example of how to set it up in your workflow file:

name: Go CI on: [push] jobs: build: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v2 - name: Cache Go modules uses: actions/cache@v2 with: path: /path/to/your/go/pkg/mod key: ${{ runner.os }}-go-${{ hashFiles('**/*.go') }} restore-keys: | ${{ runner.os }}-go- - name: Set up Go uses: actions/setup-go@v2 with: go-version: '1.17' - name: Install dependencies run: go mod download - name: Run tests run: go test ./...

Go CI caching module caching in CI optimize Go builds CI pipeline GitHub Actions caching