Depot Cache can be used with Go from Depot's managed GitHub Actions runners, from your local machine, from any CI/CD system, or within containerized builds using Dockerfiles or Bake files.
Depot GitHub Actions runners are pre-configured to use Depot Cache with Go - each runner is launched with the GOCACHEPROG environment variable pre-populated with the connection details for Depot Cache.
If you don't want Depot to set up the GOCACHEPROG environment variable on each runner, disable Allow Actions jobs to automatically connect to Depot Cache in your organization settings page. You can manually configure GOCACHEPROG to use Depot Cache as described in the "Using Depot Cache from your local machine or any CI/CD system" section.
depot/build-push-actionWhen using depot/build-push-action to build Docker images that contain Go projects, your build needs access to Go's remote cache credentials to benefit from caching.
These credentials are not automatically available inside your Docker build environment. Unlike builds running directly on Depot-managed GitHub Actions runners (which have automatic access to Depot Cache environment variables), containerized builds execute in isolated VMs that require explicit configuration.
Follow these steps to securely pass your Go cache credentials into your Docker build:
Store the Depot token in a GitHub Secret named DEPOT_TOKEN.
Configure your GitHub Action to pass secrets to the container build:
- name: Build and push
uses: depot/build-push-action@v1
with:
context: .
file: ./Dockerfile
push: true
tags: your-image:tag
secrets: |
"DEPOT_TOKEN=${{ secrets.DEPOT_TOKEN }}"# syntax=docker/dockerfile:1
# ... other Dockerfile instructions
# Install Depot CLI
RUN curl -L https://depot.dev/install-cli.sh | sh
# Mount secret and set GOCACHEPROG
RUN --mount=type=secret,id=DEPOT_TOKEN,env=DEPOT_TOKEN \
PATH="/root/.depot/bin:$PATH" \
GOCACHEPROG="depot gocache" \
go build -v ./Adding # syntax=docker/dockerfile:1 as the first line of your Dockerfile enables mounting secrets as environment variables.
To manually configure Go to use Depot Cache, set the GOCACHEPROG in your environment:
export GOCACHEPROG="depot gocache"The depot CLI will need to have authorization to write to the cache.
If you are a member of multiple organizations, and you are authenticating with a user token, you must instead specify which organization should be used for cache storage as follows:
export GOCACHEPROG='depot gocache --organization ORG_ID'To clean the cache, you can use the typical go clean workflow:
go clean -cacheTo set verbose output, add the --verbose option:
export GOCACHEPROG='depot gocache --verbose'After Go is configured to use Depot Cache, you can then run your builds as you normally would. Go will automatically communicate with GOCACHEPROG to fetch from Depot Cache and reuse any stored build artifacts from your previous builds.
When building directly with Depot CLI, follow these steps:
# syntax=docker/dockerfile:1
# ... other Dockerfile instructions
# Install Depot CLI
RUN curl -L https://depot.dev/install-cli.sh | sh
# Mount secret and set GOCACHEPROG
RUN --mount=type=secret,id=DEPOT_TOKEN,env=DEPOT_TOKEN \
PATH="/root/.depot/bin:$PATH" \
GOCACHEPROG="depot gocache" \
go build -v ./Adding # syntax=docker/dockerfile:1 as the first line of your Dockerfile enables mounting secrets as environment variables.
depot build --secret id=DEPOT_TOKEN,env=DEPOT_TOKEN -t your-image:tag .Or with Docker Buildx:
docker buildx build --secret id=DEPOT_TOKEN,env=DEPOT_TOKEN -t your-image:tag .When using Bake files to build Docker images containing Go projects, you can pass secrets through the target.secret attribute:
docker-bake.hcl file:target "default" {
context = "."
dockerfile = "Dockerfile"
tags = ["your-image:tag"]
secret = [
{
type = "env"
id = "DEPOT_TOKEN"
}
]
}# syntax=docker/dockerfile:1
# ... other Dockerfile instructions
# Install Depot CLI
RUN curl -L https://depot.dev/install-cli.sh | sh
# Mount secret and set GOCACHEPROG
RUN --mount=type=secret,id=DEPOT_TOKEN,env=DEPOT_TOKEN \
PATH="/root/.depot/bin:$PATH" \
GOCACHEPROG="depot gocache" \
go build -v ./Adding # syntax=docker/dockerfile:1 as the first line of your Dockerfile enables mounting secrets as environment variables.
depot bake:DEPOT_TOKEN=your_token depot bake