Skip to content

Commit

Permalink
Add support for build and linter cache and with-cache command (#90)
Browse files Browse the repository at this point in the history
This PR aims to add a bunch more support for caching:
- Support `build` cache, `golangci-lint` cache.
- Rename `save-cache` and `load-cache` to `save-mod-cache` and `load-mod-cache`.
- Add new command `with-cache` that wraps up some steps with all the caches.
  • Loading branch information
pete-woods authored Nov 27, 2024
1 parent ee89c8a commit f065dad
Show file tree
Hide file tree
Showing 12 changed files with 169 additions and 56 deletions.
61 changes: 33 additions & 28 deletions .circleci/test-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ jobs:
- run:
name: "Check out sample project."
command: git clone https://github.com/CircleCI-Public/circleci-demo-go.git ~/project
- go/load-cache:
key: "integration"
- go/mod-download
- run: go version && go build ./...
- go/save-cache:
- go/with-cache:
key: "integration"
mod: true
golangci-lint: true
steps:
- go/mod-download
- run: go version && go build ./...
int-test-cimg-base:
docker:
- image: cimg/base:stable
Expand All @@ -53,12 +54,13 @@ jobs:
name: "Check out sample project."
command: git clone https://github.com/CircleCI-Public/circleci-demo-go.git ~/project
- go/install
- go/load-cache:
key: "integration"
- go/mod-download
- run: go version && go build ./...
- go/save-cache:
- go/with-cache:
key: "integration"
mod: true
golangci-lint: true
steps:
- go/mod-download
- run: go version && go build ./...
int-test-cimg-python:
docker:
- image: cimg/python:3.11
Expand All @@ -76,12 +78,13 @@ jobs:
name: "Check out sample project."
command: git clone https://github.com/CircleCI-Public/circleci-demo-go.git ~/project
- go/install
- go/load-cache:
key: "integration"
- go/mod-download
- run: go version && go build ./...
- go/save-cache:
- go/with-cache:
key: "integration"
mod: true
golangci-lint: true
steps:
- go/mod-download
- run: go version && go build ./...
int-test-vm-linux:
machine:
image: ubuntu-2004:current
Expand All @@ -91,12 +94,13 @@ jobs:
command: git clone https://github.com/CircleCI-Public/circleci-demo-go.git ~/project
- go/install:
version: 1.16.4
- go/load-cache:
key: "integration"
- go/mod-download
- run: go version && go build ./...
- go/save-cache:
- go/with-cache:
key: "integration"
mod: true
golangci-lint: true
steps:
- go/mod-download
- run: go version && go build ./...
int-test-vm-arm64:
machine:
image: ubuntu-2204:current
Expand All @@ -107,12 +111,13 @@ jobs:
command: git clone https://github.com/CircleCI-Public/circleci-demo-go.git ~/project
- go/install:
version: 1.16.4
- go/load-cache:
key: "integration-arm"
- go/mod-download
- run: go version && go build ./...
- go/save-cache:
key: "integration-arm"
- go/with-cache:
key: "integration"
mod: true
golangci-lint: true
steps:
- go/mod-download
- run: go version && go build ./...
# test to make sure the dirty cache issue doesn't crop back up
int-test-dirty-cache:
machine:
Expand All @@ -123,11 +128,11 @@ jobs:
command: git clone https://github.com/CircleCI-Public/circleci-demo-go.git ~/project
- go/install:
version: 1.15.1
- go/load-cache:
- go/load-mod-cache:
key: "integration"
- go/mod-download
- run: go version && go build ./...
- go/save-cache:
- go/save-mod-cache:
key: "integration"
- go/install:
version: 1.16.4
Expand Down
10 changes: 10 additions & 0 deletions src/commands/load-build-cache.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
description: "Load cached Go build cache."
parameters:
key:
description: "User-configurable component for cache key. Useful for avoiding collisions in complex workflows."
type: string
default: ""
steps:
- restore_cache:
keys:
- v1-<< parameters.key >>-go-build-{{ .Environment.CIRCLE_JOB }}-{{ arch }}-{{ checksum "go.sum" }}-{{ epoch | round "72h" }}
10 changes: 0 additions & 10 deletions src/commands/load-cache.yml

This file was deleted.

10 changes: 10 additions & 0 deletions src/commands/load-golangci-lint-cache.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
description: "Load cached Go build cache."
parameters:
key:
description: "User-configurable component for cache key. Useful for avoiding collisions in complex workflows."
type: string
default: ""
steps:
- restore_cache:
keys:
- v1-<< parameters.key >>-golangci-lint-{{ .Environment.CIRCLE_JOB }}-{{ arch }}-{{ checksum "go.sum" }}-{{ epoch | round "72h" }}
10 changes: 10 additions & 0 deletions src/commands/load-mod-cache.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
description: "Load cached Go modules."
parameters:
key:
description: "User-configurable component for cache key. Useful for avoiding collisions in complex workflows."
type: string
default: ""
steps:
- restore_cache:
keys:
- v1-<< parameters.key >>-go-mod-{{ arch }}-{{ checksum "go.sum" }}
4 changes: 2 additions & 2 deletions src/commands/mod-download-cached.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
description: "Download and cache Go modules"
steps:
- load-cache
- load-mod-cache
- mod-download
- save-cache
- save-mod-cache
15 changes: 15 additions & 0 deletions src/commands/save-build-cache.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
description: "Save Go build cache."
parameters:
key:
description: "User-configurable component for cache key. Useful for avoiding collisions in complex workflows."
type: string
default: ""
path:
description: "Path to cache."
type: string
default: "/home/circleci/.cache/go-build"
steps:
- save_cache:
key: v1-<< parameters.key >>-go-build-{{ .Environment.CIRCLE_JOB }}-{{ arch }}-{{ checksum "go.sum" }}-{{ epoch | round "72h" }}
paths:
- << parameters.path >>
16 changes: 16 additions & 0 deletions src/commands/save-golangci-lint-cache.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
description: "Save golangci-lint cache."
parameters:
key:
description: "User-configurable component for cache key. Useful for avoiding collisions in complex workflows."
type: string
default: ""
path:
description: "Path to cache."
type: string
default: "/home/circleci/.cache/golangci-lint"
steps:
- save_cache:
key: v1-<< parameters.key >>-golangci-lint-{{ .Environment.CIRCLE_JOB }}-{{ arch }}-{{ checksum "go.sum" }}-{{ epoch | round "72h" }}
paths:
- << parameters.path >>

Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
description: "Save Go modules to cache."
parameters:
key:
description: "The cache key to use. The key is immutable."
description: "User-configurable component for cache key. Useful for avoiding collisions in complex workflows."
type: string
default: "go-mod"
default: ""
path:
description: "Path to cache."
type: string
# /home/circleci/go is the GOPATH in the cimg/go Docker image
default: "/home/circleci/go/pkg/mod"
steps:
- save_cache:
key: << parameters.key >>-{{ arch }}-{{ checksum "go.sum" }}
key: v1-<< parameters.key >>-go-mod-{{ arch }}-{{ checksum "go.sum" }}
paths:
- << parameters.path >>
56 changes: 56 additions & 0 deletions src/commands/with-cache.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
description: |
Runs the given steps
parameters:
steps:
description: "The steps to run with caching."
type: steps
key:
description: "User-configurable component for cache key. Useful for avoiding collisions in complex workflows."
type: string
default: ""
build:
description: "Whether to use go-build cache."
type: boolean
default: true
mod:
description: |
Whether to use go module cache. If most of your dependencies are public, it is faster to use the public
Go module proxy, so this defaults to `false`.
type: boolean
default: false
golangci-lint:
description: "Whether to use golangci-lint cache. Useful only in steps with linting, so defaults to false."
type: boolean
default: false
steps:
- when:
condition: << parameters.build >>
steps:
- load-build-cache:
key: << parameters.key >>
- when:
condition: << parameters.mod >>
steps:
- load-mod-cache:
key: << parameters.key >>
- when:
condition: << parameters.golangci-lint >>
steps:
- load-golangci-lint-cache:
key: << parameters.key >>
- steps: << parameters.steps >>
- when:
condition: << parameters.build >>
steps:
- save-build-cache:
key: << parameters.key >>
- when:
condition: << parameters.mod >>
steps:
- save-mod-cache:
key: << parameters.key >>
- when:
condition: << parameters.golangci-lint >>
steps:
- save-golangci-lint-cache:
key: << parameters.key >>
18 changes: 9 additions & 9 deletions src/examples/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ usage:
tag: "1.16"
steps:
- checkout
- go/load-cache
- go/mod-download
- go/save-cache
- go/test:
race: true
failfast: true
covermode: "atomic" # The preferred setting when enabling race
timeout: 15m #The amount of time to allow the go tests to run before timing out, defaults to "10m"
no_output_timeout: 15m # The amount of time to allow the orb to run without output before timing out, defaults to "10m"
- go/with-cache:
steps:
- go/mod-download
- go/test:
race: true
failfast: true
covermode: "atomic" # The preferred setting when enabling race
timeout: 15m #The amount of time to allow the go tests to run before timing out, defaults to "10m"
no_output_timeout: 15m # The amount of time to allow the orb to run without output before timing out, defaults to "10m"
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ usage:
build:
executor:
name: go/default
tag: "1.16"
tag: "1.23"
steps:
- checkout
- go/load-cache
- go/mod-download
- go/save-cache
- go/with-cache:
steps:
- go/mod-download
- go/test

0 comments on commit f065dad

Please sign in to comment.