Skip to content

Commit

Permalink
Extract bundle cache action
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyCTHsu committed Mar 7, 2025
1 parent 266c2e7 commit a484ff0
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 50 deletions.
52 changes: 52 additions & 0 deletions .github/actions/bundle-cache/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: 'Prepare bundle cache'

description: 'Sets up bundle cache for GitHub Actions'

outputs:
cache-key:
description: 'Cache key for the bundle'
value: ${{ steps.restore-cache.outputs.cache-primary-key }}
lockfile:
description: 'Lockfile name'
value: ${{ steps.lockfile.outputs.lockfile }}

runs:
using: "composite"
steps:
- id: ruby-alias
shell: bash
run: |
engine=$(ruby -e "puts RUBY_ENGINE")
alias=$(ruby -e "puts RUBY_ENGINE_VERSION.split('.').take(2).join")
echo "ruby-alias=$engine-$alias" >> "$GITHUB_OUTPUT"
- name: Generate lockfile
id: lockfile
shell: bash
env:
LOCKFILE: lockfile-${{ steps.ruby-alias.outputs.ruby-alias }}
run: |
bundle lock
echo "lockfile=$LOCKFILE" >> "$GITHUB_OUTPUT"
- name: Upload lockfile
uses: actions/upload-artifact@65c4c4a1ddee5b72f698fdd19549f0f0fb45cf08 # v4.6.0
with:
name: ${{ steps.lockfile.outputs.lockfile }}
path: "*.lock"
- name: Restore cache
uses: actions/cache/restore@1bd1e32a3bdc45362d1e726936510720a7c30a57 # v4.2.0
id: restore-cache
with:
key: bundle-${{ runner.os }}-${{ runner.arch }}-${{ steps.ruby-alias.outputs.ruby-alias }}-${{ hashFiles('*.lock') }}
path: "/usr/local/bundle"
- if: steps.restore-cache.outputs.cache-hit != 'true'
shell: bash
run: bundle install
- if: steps.restore-cache.outputs.cache-hit != 'true'
name: Save cache
uses: actions/cache/save@1bd1e32a3bdc45362d1e726936510720a7c30a57 # v4.2.0
with:
key: "${{ steps.restore-cache.outputs.cache-primary-key }}"
path: "/usr/local/bundle"
- run: bundle check
shell: bash
8 changes: 4 additions & 4 deletions .github/actions/bundle-restore/action.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name: 'Restore Bundle from cache'
name: 'Restore bundle cache'

# TODO: Improve description
description: 'Restore the bundle from cache'
description: 'Restores bundle cache for GitHub Actions'

inputs:
lockfile-name:
lockfile:
description: 'Name of the lockfile artifact to download'
required: true
cache-key:
Expand All @@ -17,7 +17,7 @@ runs:
- name: Download lockfile
uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8
with:
name: ${{ inputs.lockfile-name }}
name: ${{ inputs.lockfile }}

- name: Restore cache
uses: actions/cache/restore@1bd1e32a3bdc45362d1e726936510720a7c30a57 # v4.2.0
Expand Down
38 changes: 8 additions & 30 deletions .github/workflows/_unit_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,41 +28,19 @@ jobs:
outputs:
batches: "${{ steps.set-batches.outputs.batches }}"
misc: "${{ steps.set-batches.outputs.misc }}"
cache-key: "${{ steps.restore-cache.outputs.cache-primary-key }}"
lockfile: "${{ steps.lockfile.outputs.lockfile }}"
cache-key: "${{ steps.bundle-cache.outputs.cache-key }}"
lockfile: "${{ steps.bundle-cache.outputs.lockfile }}"
container:
image: ghcr.io/datadog/images-rb/engines/${{ inputs.engine }}:${{ inputs.version }}
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
persist-credentials: false
- name: Generate lockfile
id: lockfile
env:
LOCKFILE: lockfile-${{ inputs.alias }}-${{ github.run_id }}
run: |
bundle lock
echo "lockfile=$LOCKFILE" >> "$GITHUB_OUTPUT"

- name: Upload lockfile
uses: actions/upload-artifact@65c4c4a1ddee5b72f698fdd19549f0f0fb45cf08 # v4.6.0
with:
name: ${{ steps.lockfile.outputs.lockfile }}
path: "*.lock"
- name: Restore cache
uses: actions/cache/restore@1bd1e32a3bdc45362d1e726936510720a7c30a57 # v4.2.0
id: restore-cache
with:
key: bundle-${{ runner.os }}-${{ runner.arch }}-${{ inputs.alias }}-${{ hashFiles('*.lock') }}
path: "/usr/local/bundle"
- if: steps.restore-cache.outputs.cache-hit != 'true'
run: bundle install
- if: steps.restore-cache.outputs.cache-hit != 'true'
name: Save cache
uses: actions/cache/save@1bd1e32a3bdc45362d1e726936510720a7c30a57 # v4.2.0
with:
key: "${{ steps.restore-cache.outputs.cache-primary-key }}"
path: "/usr/local/bundle"
- name: Prepare bundle cache
id: bundle-cache
uses: ./.github/actions/bundle-cache

- id: set-batches
name: Distribute tasks into batches
run: |
Expand Down Expand Up @@ -158,7 +136,7 @@ jobs:
- name: Restore bundle cache
uses: ./.github/actions/bundle-restore
with:
lockfile-name: ${{ needs.batch.outputs.lockfile }}
lockfile: ${{ needs.batch.outputs.lockfile }}
cache-key: ${{ needs.batch.outputs.cache-key }}
- name: Build & Test
uses: ./.github/actions/build-test
Expand Down Expand Up @@ -233,7 +211,7 @@ jobs:
- name: Restore bundle cache
uses: ./.github/actions/bundle-restore
with:
lockfile-name: ${{ needs.batch.outputs.lockfile }}
lockfile: ${{ needs.batch.outputs.lockfile }}
cache-key: ${{ needs.batch.outputs.cache-key }}
- name: Build & Test
uses: ./.github/actions/build-test
Expand Down
42 changes: 26 additions & 16 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,16 @@ jobs:
name: build
runs-on: ubuntu-24.04
container: ghcr.io/datadog/images-rb/engines/ruby:3.3
outputs:
lockfile: ${{ steps.bundle-cache.outputs.lockfile }}
cache-key: ${{ steps.bundle-cache.outputs.cache-key }}
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
persist-credentials: false
- run: bundle lock
- uses: actions/upload-artifact@65c4c4a1ddee5b72f698fdd19549f0f0fb45cf08 # v4.6.0
id: lockfile
with:
name: 'check-lockfile-${{ github.sha }}-${{ github.run_id }}'
path: '*.lock'
if-no-files-found: error
- name: Prepare bundle cache
id: bundle-cache
uses: ./.github/actions/bundle-cache

rubocop:
name: rubocop/lint
Expand All @@ -41,8 +40,11 @@ jobs:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
persist-credentials: false
- uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8
- run: bundle install
- name: Restore bundle cache
uses: ./.github/actions/bundle-restore
with:
lockfile: ${{ needs.build.outputs.lockfile }}
cache-key: ${{ needs.build.outputs.cache-key }}
- run: bundle exec rake rubocop

standard:
Expand All @@ -54,9 +56,11 @@ jobs:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
persist-credentials: false
- uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8
- name: Install dependencies
run: bundle install
- name: Restore bundle cache
uses: ./.github/actions/bundle-restore
with:
lockfile: ${{ needs.build.outputs.lockfile }}
cache-key: ${{ needs.build.outputs.cache-key }}
- run: bundle exec rake standard

steep:
Expand All @@ -68,9 +72,11 @@ jobs:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
persist-credentials: false
- uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8
- name: Install dependencies
run: bundle install
- name: Restore bundle cache
uses: ./.github/actions/bundle-restore
with:
lockfile: ${{ needs.build.outputs.lockfile }}
cache-key: ${{ needs.build.outputs.cache-key }}
- name: Check for stale signature files
run: bundle exec rake rbs:stale
- name: Check for missing signature files
Expand All @@ -91,7 +97,11 @@ jobs:
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
persist-credentials: false
- uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8 # requires the lockfile
- name: Restore bundle cache
uses: ./.github/actions/bundle-restore
with:
lockfile: ${{ needs.build.outputs.lockfile }}
cache-key: ${{ needs.build.outputs.cache-key }}
- uses: DataDog/datadog-sca-github-action@main
with:
dd_api_key: ${{ secrets.DD_API_KEY }}
Expand Down

0 comments on commit a484ff0

Please sign in to comment.