When working with workflows, you might find yourself performing the same set of tasks multiple times across workflows or even jobs. This is commonly seen in build scenarios where dependencies are downloaded. Because Actions runners are ephemeral, the files from a previous execution are deleted.
This inevitably can cause builds to run longer, especially when the number of dependencies used are numerous.
In the previous exercise on Continuous Integration
, you built a workflow that is performing automation when Pull Requests are opened to merge to the default branch. In this exercise, we will reuse that workflow to demonstrate a bit of caching.
- From the default branch of your repository, create a new branch of code called
feature/cache
- Open the file named
.github/workflows/ci-cd.yaml
- Replace the contents of the file with:
name: Continuous Integration & Delivery
on:
pull_request:
workflow_dispatch:
defaults:
run:
shell: bash
jobs:
ci:
name: Continuous Integration
runs-on: ubuntu-latest
defaults:
run:
working-directory: golang_app
steps:
- name: Path Setup
id: go-paths
working-directory: ${{github.workspace}}
run: |
echo "gomodcache=$(go env GOMODCACHE)" >> $GITHUB_OUTPUT
echo "gocache=$(go env GOCACHE)" >> $GITHUB_OUTPUT
- name: Clone
uses: actions/[email protected]
- name: Cache
uses: actions/[email protected]
with:
path: |
${{ steps.go-paths.outputs.gomodcache }}
${{ steps.go-paths.outputs.gocache }}
key: ${{ runner.os }}-gomodcache-${{ hashFiles('**/go.sum') }}
- name: Get Dependencies
run: go get app
- name: Build
run: go build
- name: Run Linting
uses: golangci/golangci-lint-action@v3
with:
working-directory: golang_app
- name: Run Tests
run: go test
- name: Store Artifact
uses: actions/[email protected]
with:
name: golang_app
path: golang_app/app
- Replace the contents of the file golang_app/main.go with the contents in ./golang_replacements/15-main.go
- Replace the contents of the file golang_app/go.mod with the contents in ./golang_replacements/15-go.mod
- Replace the contents of the file golang_app/go.sum with the contents in ./golang_replacements/15-go.sum
- Add & commit your changes, then publish your branch.
- Go to your repository, and view the Pull Requests tab.
- Create a pull request to merge
feature/cache
into your default branch.
In the steps above, you added several steps to the workflow file. These steps help to define the Go paths for caching, and then pass those paths to the actions/cache
action that handles the downloading & updating of caches. You will now see two additional steps (Cache
and Post Cache
)
No additional step is required to save your cache, because the action has post-execution hooks that handle it for you.
The result won't be a lot of time saved because the number of dependencies are very few, but in a real-world scenario this could save a lot, reducing dependency downloads from the 10's of minutes to just a few seconds.
Within the workflow overview, the caches for any given workflow can be seen. You will need to have a successful execution first, but afterwards the view will be available.
- Go to the
Actions
tab on your repository. - Click the
Continuous Integration & Delivery
workflow on the left. - Finally, click the
Caches
link from the left hand navigation. You can also do this from the top (no need to drill in toActions
) to see all caches across a repository.
- Click the green
Merge pull request
button on the pull request from step 1.9. This will put your code into the main branch. - Delete the published branch created in Step 1.
- Checkout to your default branch locally and pull down the changes.