Skip to content

Commit

Permalink
Add option to update Docker cache key based on the number of times th… (
Browse files Browse the repository at this point in the history
#64)

* Add option to update Docker cache key based on the number of times the docker should be pulled from the docker registry

* Update description of update_times_a_day input in action.yml

* Exit quickly when UPDATE_TIMES_A_DAY not provided

* add not in check of UPDATE_TIMES_A_DAY
  • Loading branch information
MishaKav authored Apr 15, 2024
1 parent d64f157 commit 7052387
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
34 changes: 33 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ inputs:
description: "Run parser in debug mode"
required: false
default: false
update_times_a_day:
description: 'Number of times a day the docker should be pull from the docker registry. Whole number between 1 and 24'
required: false
runs:
using: "composite"
steps:
Expand Down Expand Up @@ -100,6 +103,36 @@ runs:
ref: ${{ fromJSON(fromJSON(github.event.inputs.client_payload)).cmRepoRef }}
path: "gitstream/cm/"

- name: Get Docker cache key
id: cache-key
if: ${{ inputs.update_times_a_day }}
uses: actions/github-script@v7
env:
UPDATE_TIMES_A_DAY: ${{ inputs.update_times_a_day }}
with:
script: |
require('${{ github.action_path }}/script.js')(core);
- name: Cache Docker
id: cache-gitstream-docker
if: ${{ env.DOCKER_KEY }}
uses: actions/cache@v4
with:
path: /tmp/**.tar
key: ${{ env.DOCKER_KEY }}

- name: Docker Pull
if: steps.cache-gitstream-docker.outputs.cache-hit != 'true'
shell: bash
run: |
docker pull gitstream/rules-engine:latest
docker save -o /tmp/${{ env.DOCKER_KEY }}.tar gitstream/rules-engine:latest
- name: Docker Load
if: steps.cache-gitstream-docker.outputs.cache-hit == 'true'
shell: bash
run: docker load -i /tmp/${{ env.DOCKER_KEY }}.tar

- name: Run The Action
if: always()
run: |
Expand All @@ -109,7 +142,6 @@ runs:
value="${!var}"
echo "$var=$value" >> "$env_file"
done
docker pull gitstream/rules-engine:latest
docker run --env-file $env_file \
-v $(pwd)/gitstream:/code \
-e HEAD_REF=$'upstream/${{ steps.safe-strings.outputs.head_ref }}' \
Expand Down
21 changes: 21 additions & 0 deletions script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module.exports = core => {
const { RUNNER_OS, UPDATE_TIMES_A_DAY } = process.env;

if (!UPDATE_TIMES_A_DAY) {
return;
}

// calculate the docker-key cache value
const times = Number(UPDATE_TIMES_A_DAY);

if (!Number.isInteger(times) || times <= 0 || times >= 25) {
core.error(`"update_times_a_day" should be a whole number in range [1..24]`);
process.exit(2);
}

const date = new Date();
const uniqNum = Math.ceil(((date.getHours() + 1) / 24) * times);
const uniqValue = `${date.toLocaleDateString('en-US').replaceAll('/', '-')}-${uniqNum}`;
const dockerKey = `${RUNNER_OS}-gitstream-docker-${uniqValue}`;
core.exportVariable('DOCKER_KEY', dockerKey);
};

0 comments on commit 7052387

Please sign in to comment.