Skip to content

Commit

Permalink
chore(ci): re-use cache key from build workflow (#11299)
Browse files Browse the repository at this point in the history
This makes build cache key generation reusable across different workflows. The test and release workflows now use the same "recipe" for cache key generation--no more copy/paste.
  • Loading branch information
flrgh authored Jul 27, 2023
1 parent 91dcd61 commit 68b7b6f
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 28 deletions.
45 changes: 45 additions & 0 deletions .github/actions/build-cache-key/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Build Cache Key

description: >
Generates a cache key suitable for save/restore of Kong builds.
inputs:
prefix:
description: 'String prefix applied to the build cache key'
required: false
default: 'build'

outputs:
cache-key:
description: 'The generated cache key'
value: ${{ steps.cache-key.outputs.CACHE_KEY }}

runs:
using: composite
steps:
- name: Generate cache key
id: cache-key
shell: bash
run: |
# please keep these sorted
FILE_HASHES=(
${{ hashFiles('.bazelignore') }}
${{ hashFiles('.bazelrc') }}
${{ hashFiles('.bazelversion') }}
${{ hashFiles('.github/actions/build-cache-key/**') }}
${{ hashFiles('.github/workflows/build.yml') }}
${{ hashFiles('.requirements') }}
${{ hashFiles('BUILD.bazel') }}
${{ hashFiles('WORKSPACE') }}
${{ hashFiles('bin/kong') }}
${{ hashFiles('bin/kong-health') }}
${{ hashFiles('build/**') }}
${{ hashFiles('kong-*.rockspec') }}
${{ hashFiles('kong.conf.default') }}
${{ hashFiles('kong/**') }}
)
HASH=$(sha256sum - <<< "${FILE_HASHES[*]}" | awk '{print $1}' )
CACHE_KEY=${{ inputs.prefix }}::${HASH}
echo "cache-key: ${CACHE_KEY}"
echo "CACHE_KEY=${CACHE_KEY}" >> $GITHUB_OUTPUT
16 changes: 13 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ on:
relative-build-root:
required: true
type: string
outputs:
cache-key:
description: 'Computed cache key, used for restoring cache in other workflows'
value: ${{ jobs.build.outputs.cache-key }}

env:
BUILD_ROOT: ${{ github.workspace }}/${{ inputs.relative-build-root }}
Expand All @@ -14,6 +18,9 @@ jobs:
name: Build dependencies
runs-on: ubuntu-22.04

outputs:
cache-key: ${{ steps.cache-key.outputs.cache-key }}

steps:
- name: Checkout Kong source code
uses: actions/checkout@v3
Expand All @@ -24,13 +31,16 @@ jobs:
- name: Build WASM Test Filters
uses: ./.github/actions/build-wasm-test-filters

- name: Generate cache key
id: cache-key
uses: ./.github/actions/build-cache-key

- name: Lookup build cache
id: cache-deps
uses: actions/cache@v3
with:
path: |
${{ env.BUILD_ROOT }}
key: ${{ hashFiles('bin/kong', '.requirements', 'kong-*.rockspec', '.bazelversion', '.bazelrc', 'build/**', 'BUILD.bazel', 'WORKSPACE', '.github/workflows/build_and_test.yml') }}
path: ${{ env.BUILD_ROOT }}
key: ${{ steps.cache-key.outputs.cache-key }}

- name: Install packages
if: steps.cache-deps.outputs.cache-hit != 'true'
Expand Down
22 changes: 8 additions & 14 deletions .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,8 @@ jobs:
id: cache-deps
uses: actions/cache@v3
with:
path: |
${{ env.BUILD_ROOT }}
key: ${{ hashFiles('bin/kong', '.requirements', 'kong-*.rockspec', '.bazelversion', '.bazelrc', 'build/**', 'BUILD.bazel', 'WORKSPACE', '.github/workflows/build_and_test.yml') }}
path: ${{ env.BUILD_ROOT }}
key: ${{ needs.build.outputs.cache-key }}

- name: Check test-helpers doc generation
run: |
Expand Down Expand Up @@ -162,10 +161,8 @@ jobs:
id: cache-deps
uses: actions/cache@v3
with:
path: |
${{ env.BUILD_ROOT }}
key: ${{ hashFiles('bin/kong', '.requirements', 'kong-*.rockspec', '.bazelversion', '.bazelrc', 'build/**', 'BUILD.bazel', 'WORKSPACE', '.github/workflows/build_and_test.yml') }}
path: ${{ env.BUILD_ROOT }}
key: ${{ needs.build.outputs.cache-key }}

- name: Build WASM Test Filters
uses: ./.github/actions/build-wasm-test-filters
Expand Down Expand Up @@ -267,10 +264,8 @@ jobs:
id: cache-deps
uses: actions/cache@v3
with:
path: |
${{ env.BUILD_ROOT }}
key: ${{ hashFiles('bin/kong', '.requirements', 'kong-*.rockspec', '.bazelversion', '.bazelrc', 'build/**', 'BUILD.bazel', 'WORKSPACE', '.github/workflows/build_and_test.yml') }}
path: ${{ env.BUILD_ROOT }}
key: ${{ needs.build.outputs.cache-key }}

- name: Build WASM Test Filters
uses: ./.github/actions/build-wasm-test-filters
Expand Down Expand Up @@ -330,9 +325,8 @@ jobs:
id: cache-deps
uses: actions/cache@v3
with:
path: |
${{ env.BUILD_ROOT }}
key: ${{ hashFiles('bin/kong', '.requirements', 'kong-*.rockspec', '.bazelversion', '.bazelrc', 'build/**', 'BUILD.bazel', 'WORKSPACE', '.github/workflows/build_and_test.yml') }}
path: ${{ env.BUILD_ROOT }}
key: ${{ needs.build.outputs.cache-key }}

- name: Install Test::Nginx
run: |
Expand Down
21 changes: 13 additions & 8 deletions .github/workflows/perf.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,25 @@ jobs:
(startsWith(github.event.comment.body, '/perf') || startsWith(github.event.comment.body, '/flamegraph'))
)
outputs:
cache-key: ${{ steps.cache-key.outputs.cache-key }}

steps:
- name: Checkout Kong source code
uses: actions/checkout@v3

- name: Generate cache key
id: cache-key
uses: ./.github/actions/build-cache-key
with:
prefix: perf

- name: Lookup build cache
id: cache-deps
uses: actions/cache@v3
with:
path: |
${{ env.BUILD_ROOT }}
key: perf-${{ hashFiles('.requirements', 'kong-*.rockspec', '.bazelversion', '.bazelrc', 'build/**', 'BUILD.bazel', 'WORKSPACE', '.github/workflows/build_and_test.yml') }}
path: ${{ env.BUILD_ROOT }}
key: ${{ steps.cache-key.outputs.cache-key }}

- name: Install packages
if: steps.cache-deps.outputs.cache-hit != 'true'
Expand Down Expand Up @@ -72,8 +80,6 @@ jobs:
run: |
make install-dev-rocks
# the above should be same as build_and_test.yml expect that perf.yml is used in cache_key

perf:
name: RPS, latency and flamegraphs
runs-on: ubuntu-22.04
Expand Down Expand Up @@ -116,9 +122,8 @@ jobs:
if: env.GHA_CACHE == 'true'
uses: actions/cache@v3
with:
path: |
${{ env.BUILD_ROOT }}
key: perf-${{ hashFiles('.requirements', 'kong-*.rockspec', '.bazelversion', '.bazelrc', 'build/**', 'BUILD.bazel', 'WORKSPACE', '.github/workflows/build_and_test.yml') }}
path: ${{ env.BUILD_ROOT }}
key: ${{ needs.build-packages.outputs.cache-key }}

- name: Install performance test Dependencies
run: |
Expand Down
12 changes: 9 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,20 @@ jobs:
- name: Swap git with https
run: git config --global url."https://github".insteadOf git://github

- name: Generate build cache key
id: cache-key
if: env.GHA_CACHE == 'true'
uses: ./.github/actions/build-cache-key
with:
prefix: ${{ matrix.label }}-build

- name: Cache Packages
id: cache-deps
if: env.GHA_CACHE == 'true'
uses: actions/cache@v3
with:
path: |
bazel-bin/pkg
key: ${{ matrix.label }}-build-${{ hashFiles('bin/kong', '.requirements', 'kong-*.rockspec', 'kong/**/*.lua', '**/*.bzl', '**/*.bazel') }}
path: bazel-bin/pkg
key: ${{ steps.cache-key.outputs.cache-key }}

- name: Set .requirements into environment variables
run: |
Expand Down

1 comment on commit 68b7b6f

@khcp-gha-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bazel Build

Docker image available kong/kong:68b7b6f9cb9eb52af0ed558b2952a3239926b5b7
Artifacts available https://github.com/Kong/kong/actions/runs/5684056729

Please sign in to comment.