diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index fb17b78..f8135af 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -28,15 +28,23 @@ jobs: run: | pip install -r requirements.txt - - name: Cache Folder - uses: actions/cache@v3 + - name: Store Cache Folder + uses: actions/cache/restore@v3 + id: restore-cache with: path: | caches - key: ${{ runner.os }}-files-${{ hashFiles('caches/**') }} + key: ${{ runner.os }}-files- restore-keys: | ${{ runner.os }}-files- + - name: Get Cache Hash + id: get-cache-hash + run: | + echo "CACHE_HASH=$CACHE_HASH" >> "$GITHUB_OUTPUT" + env: + CACHE_HASH: ${{ hashFiles('caches/caches_lockfile') }} + - name: Run Script run: | python main.py \ @@ -46,6 +54,7 @@ jobs: --github_token ${GITHUB_TOKEN} \ --cache_directory caches \ --runtime_xpi_directory xpis \ + --cache_lockfile caches_lockfile \ --previous_info_urls \ https://raw.githubusercontent.com/${GITHUB_REPOSITORY}/publish/addon_infos.json \ https://github.com/${GITHUB_REPOSITORY}/releases/latest/download/addon_infos.json @@ -61,3 +70,24 @@ jobs: user_email: 'github-actions[bot]@users.noreply.github.com' publish_dir: ./published + - name: Check Cache Need Update + id: check-cache-update + run: | + if [[ "$PRE_CACHE_HASH" != "$CUR_CACHE_HASH" ]]; then + echo "SAVE_CACHE=true" >> "$GITHUB_OUTPUT" + else + echo "SAVE_CACHE=false" >> "$GITHUB_OUTPUT" + fi + env: + PRE_CACHE_HASH: ${{ steps.get-cache-hash.outputs.CACHE_HASH }} + CUR_CACHE_HASH: ${{ hashFiles('caches/caches_lockfile') }} + + - name: Save Cache Folder + if: steps.check-cache-update.outputs.SAVE_CACHE == 'true' + uses: actions/cache/save@v3 + id: save-cache + with: + path: | + caches + key: ${{ runner.os }}-files-${{ hashFiles('caches/caches_lockfile') }} + diff --git a/main.py b/main.py index dcd443a..daa7cbf 100644 --- a/main.py +++ b/main.py @@ -6,6 +6,7 @@ import urllib.parse import requests import argparse import shutil +import hashlib from addon_keys import * from fallback_infos import fallback_if_need from moz_addons import addon_details @@ -262,12 +263,26 @@ def create_release(github_repository, **kwargs): print(f'create release failed: {e}') -def update_cache(cache_directory, runtime_xpi_directory): +def folder_filename_hash(dir): + filenames = sorted(os.listdir(dir)) + folder_hash = hashlib.new('sha256') + for filename in filenames: + hash_obj = hashlib.new('sha256') + hash_obj.update(filename.encode('utf-8')) + folder_hash.update(hash_obj.hexdigest().encode('utf-8')) + return folder_hash.hexdigest() + + +def update_cache(cache_directory, runtime_xpi_directory, cache_hash_filename): if not cache_directory or not runtime_xpi_directory or cache_directory == runtime_xpi_directory: return try: shutil.rmtree(cache_directory) shutil.move(runtime_xpi_directory, cache_directory) + folder_hash = folder_filename_hash(cache_directory) + with open(os.path.join(cache_directory, cache_hash_filename), 'w') as file: + file.write(folder_hash) + print(folder_hash) except Exception as e: print(f'update cache failed: {e}') @@ -280,6 +295,7 @@ if __name__ == '__main__': parser.add_argument('-o', '--output', nargs='?', type=str, default="addon_infos.json", help='output addon filepath') parser.add_argument('--cache_directory', nargs='?', default="caches", type=str, help='folder for caches') + parser.add_argument('--cache_lockfile', nargs='?', default="caches_lockfile", type=str, help='hashfile for caches') parser.add_argument('--runtime_xpi_directory', nargs='?', default="xpis", type=str, help='folder for download xpi') parser.add_argument('--previous_info_urls', nargs='+', default=[], help='previous published info json to fallback') @@ -310,4 +326,4 @@ if __name__ == '__main__': upload_file=args.output, github_token=args.github_token) - update_cache(args.cache_directory, args.runtime_xpi_directory) + update_cache(args.cache_directory, args.runtime_xpi_directory, args.cache_lockfile)