Skip to content

Commit

Permalink
Adopt and use hashes (#2)
Browse files Browse the repository at this point in the history
* Use hashing instead of timestamps

* change CI routines

* Inline actions
  • Loading branch information
fido-node authored Nov 5, 2024
1 parent 1cc0213 commit 11d5fb1
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 3 deletions.
50 changes: 50 additions & 0 deletions .github/actions/scala_restore_cache/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: restore-sbt-cache
description: 'cache sbt deps and compiled classes'
author: 'Dmitry Muzyka'
inputs:
clean_build:
description: 'build all'
required: false
default: "false"
runs:
using: 'composite'
steps:
- name: Make cache key files
shell: bash
run: |
echo "${LAST_COMPLETED_JOB_SHA1}" > last-completed-job.sha1
echo "${GITHUB_SHA}" > current.sha1
echo "${LATEST_CACHE_SHA1}" > latest-maybe-failed.sha1
date +%Y-%m > current_month
- name: Restore compilation cache
if: ${{ inputs.clean_build != 'true' }}
uses: actions/cache/restore@v3
with:
path: |
build_cache
key: v2-build-cache-${{ github.job }}-${{ hashFiles('current_month') }}-${{ hashFiles('current.sha1') }}
restore-keys: |
v2-build-cache-${{ github.job }}-${{ hashFiles('current_month') }}-${{ hashFiles('current.sha1') }}
v2-build-cache-${{ github.job }}-${{ hashFiles('current_month') }}-${{ hashFiles('latest-maybe-failed.sha1') }}
v2-build-cache-${{ github.job }}-${{ hashFiles('current_month') }}-${{ hashFiles('last-completed-job.sha1') }}
v2-build-cache-${{ github.job }}-${{ hashFiles('current_month') }}-
- name: Apply compilation cache
if: ${{ inputs.clean_build != 'true' }}
shell: bash
run: |
RUNNER_TRACKING_ID="" && tar -xf build_cache/targets.tar 2>/dev/null &
- name: cleanup sbt cache once per month
shell: bash
run: |
mkdir -p ~/.cache
test -f ~/.cache/.timecreated || touch ~/.cache/.timecreated
if test "$(find ~/.cache/.timecreated -mtime +30)"; then
echo "run cleanup"
find ~/.cache ~/.ivy2 -type f -mtime +30 -delete
find ~/.cache ~/.ivy2 -type d -empty -delete
mkdir -p ~/.cache
touch ~/.cache/.timecreated
fi
44 changes: 44 additions & 0 deletions .github/actions/scala_save_cache/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: save-sbt-cache
description: 'cache sbt deps and compiled classes'
author: 'Dmitry Muzyka'
inputs:
clean_build:
description: 'build all'
required: false
default: "false"
runs:
using: 'composite'
steps:
- name: Build cache directory
shell: bash
run: |
targets=$(git clean -ndx | cut -c14- | grep /$ | grep -E -v '(build_cache|persist|test_results)')
tar -cf build_cache/targets.tar --exclude=*/target/test-reports/*.xml --exclude=*.log --exclude=*/target/scoverage-report --exclude=*/target/coverage-report --exclude=project/target/active.json $targets || :
- name: Make cache key file
shell: bash
run: |
echo "${GITHUB_SHA}" > current.sha1
date +%Y-%m > current_month
- name: check if compilation cache exists
id: check-compilation-cache
uses: actions/cache/restore@v3
with:
path: |
build_cache
key: v2-build-cache-${{ github.job }}-${{ hashFiles('current_month') }}-${{ hashFiles('current.sha1') }}
lookup-only: 'true'

- name: Save compilation cache
if: steps.check-compilation-cache.outputs.cache-hit != 'true'
uses: actions/cache/save@v3
with:
path: |
build_cache
key: v2-build-cache-${{ github.job }}-${{ hashFiles('current_month') }}-${{ hashFiles('current.sha1') }}

- name: Clean up
shell: bash
run: rm -rf build_cache current.sha1
# / save sbt cache
57 changes: 57 additions & 0 deletions .github/actions/set_vars/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: set-ci-vars
description: 'set ci vars.'
author: 'Dmitry Muzyka'
inputs:
clean_build:
description: 'build all'
required: false
default: "false"
runs:
using: 'composite'
steps:
- name: "set git config"
shell: bash
run: |
git config --global --add safe.directory "$(pwd)"
git fetch origin master
git describe --tags --abbrev=0 --first-parent
# set vars
- name: set branch
shell: bash
run: |
branch="$(echo -n ${{ github.event.ref }} | sed 's#refs/heads/##g; s#/#-#g' | tr '[:upper:]' '[:lower:]')"
echo "branch=\"${branch}\"" >> $GITHUB_ENV
- name: set new version
shell: bash
run: |
if [[ ${{env.branch}} == "master" ]]; then
version="$(date +'%Y.%m.%d')-${{github.run_number}}"
else
version="$(date +'%Y.%m.%d')-${branch}-${{github.run_number}}"
fi
version=$(echo $version | sed 's/"//g')
echo "version=$version"
echo "version=$version" >> $GITHUB_ENV
- name: Set LAST_COMPLETED_JOB_SHA1
shell: bash
run: |
if [[ '${{ inputs.clean_build }}' == 'true' ]]; then
echo "clean build, will bundle & test all"
echo "LAST_COMPLETED_JOB_SHA1=None" >> $GITHUB_ENV
else
.github/helpers/set-last-completed-job-sha1.sh
fi
- name: print debug info
shell: bash
run: |
echo "previous successful build sha: ${LAST_COMPLETED_JOB_SHA1}"
echo "previous successful version: $(git tag --points-at ${LAST_COMPLETED_JOB_SHA1})"
echo "latest cache sha: ${LATEST_CACHE_SHA1}"
echo "branch: ${branch}"
echo "version: ${version}"
# /set vars
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:

# set vars
- name: set vars
uses: whisklabs/gha-workflows/actions/set_vars@master
uses: ./.github/actions/set_vars
with:
clean_build: ${{ github.event.inputs.clean_build }}

Expand All @@ -43,7 +43,7 @@ jobs:
distribution: 'temurin'

- name: restore cache
uses: whisklabs/gha-workflows/actions/scala_restore_cache@master
uses: ./.github/actions/scala_restore_cache
with:
clean_build: ${{ github.event.inputs.clean_build }}

Expand Down Expand Up @@ -76,5 +76,5 @@ jobs:
- name: label vcs
run: git tag $version && git push --tag
- name: Save Scala Cache
uses: whisklabs/gha-workflows/actions/scala_save_cache@master
uses: ./.github/actions/scala_save_cache

0 comments on commit 11d5fb1

Please sign in to comment.