From f0aed5ebbd04a9a6a3d5af25b8132ea581b43d2c Mon Sep 17 00:00:00 2001 From: lifegpc Date: Sat, 7 Oct 2023 04:56:23 +0000 Subject: [PATCH] Try enable cache for docker --- .github/workflows/docker.yml | 6 ++++++ scripts/get_cache_key.py | 19 ++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 7ded165..ad03ac8 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -9,6 +9,7 @@ on: - 'doc/**' - docker-compose.yml - README.md + workflow_dispatch: concurrency: group: "docker" @@ -23,6 +24,9 @@ jobs: with: fetch-depth: 0 submodules: true + - name: Get cache key + id: cache_key + run: python3 scripts/get_cache_key.py --docker - name: Login to Docker Hub uses: docker/login-action@v3 with: @@ -37,6 +41,8 @@ jobs: uses: docker/build-push-action@v5 with: context: . + cache-from: type=gha,scope=docker-${{ steps.cache_key.outputs.cache_key }} + cache-to: type=gha,mode=max,scope=docker-${{ steps.cache_key.outputs.cache_key }} push: true tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} diff --git a/scripts/get_cache_key.py b/scripts/get_cache_key.py index f6520ab..6e1f539 100644 --- a/scripts/get_cache_key.py +++ b/scripts/get_cache_key.py @@ -19,6 +19,18 @@ def sha256(data) -> str: return s.hexdigest() +def hashfile(fn): + if exists(fn): + s = _sha256() + with open(fn, 'rb') as f: + c = f.read(256) + while len(c) > 0: + s.update(c) + c = f.read(256) + return s.hexdigest() + return '' + + def hash_file(feature, prefix) -> str: if prefix is None: fns = [f"build_{feature}.sh"] @@ -43,8 +55,9 @@ def hash_file(feature, prefix) -> str: try: p = ArgumentParser(description='Get the cache key which used in action/cache') - p.add_argument("features", help="The feature's name", action='append', nargs='+', choices=['all'] + ALL_FEATURES) + p.add_argument("features", help="The feature's name", action='append', nargs='*', choices=[[], 'all'] + ALL_FEATURES) p.add_argument('--prefix', help='The prefix of the cache key') + p.add_argument('--docker', help='Cache for docker image', action='store_true', default=False) args = p.parse_intermixed_args(sys.argv[1:]) features: List[str] = args.features[0] if 'all' in features: @@ -55,6 +68,10 @@ try: dt = strftime('%Y-%m', gmtime(now)) h = hash_file(i, args.prefix) d += f"{i}={dt}:{h}\n" + if args.docker: + dt = strftime('%Y-%m', gmtime(now)) + h = hashfile('Dockerfile') + d += f"docker={dt}:{h}\n" print(d) github_output = environ.get('GITHUB_OUTPUT', '') if github_output != '':