Skip to content

Commit

Permalink
refactor: use RUNNER_TEMP for code.json download/check (#13)
Browse files Browse the repository at this point in the history
* refactor code.json download/check
* cache Python dependencies in CI
  • Loading branch information
wpbonelli authored Dec 16, 2022
1 parent 3f048bf commit 92fb40d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ jobs:
uses: actions/setup-python@v4
with:
python-version: 3.9
cache: 'pip'

- name: Install Python dependencies
run: pip install -r test/requirements.txt
Expand Down
28 changes: 16 additions & 12 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,31 +58,35 @@ runs:
echo "token=$token" >> $GITHUB_OUTPUT
- name: Check release
id: check-release
shell: bash
run: |
# get info for the executables repository's latest release
release_json=$(curl -H "Accept: application/vnd.github+json" -H "Authorization: Bearer $GH_TOKEN" "https://api.github.com/repos/MODFLOW-USGS/${{ inputs.repo }}/releases/latest") || echo "No release to check, skipping"
# get asset ID of the release's metadata file, if one exists
get_asset_id="
get_download_url="
import json
import sys
pattern = 'code.json'
release = json.load(sys.stdin, strict=False)
metadata = next(iter([a for a in release['assets'] if a['name'] == pattern]), None)
print(dict(metadata)['id'] if metadata else '')
print(dict(metadata)['browser_download_url'] if metadata else '')
"
asset_id=$(echo "$release_json" | python3 -c "$get_asset_id") || echo "No release to check, skipping"
download_url=$(echo "$release_json" | python3 -c "$get_download_url") || echo "No release to check, skipping"
code_json="$RUNNER_TEMP/code.json"
echo "code_json=$code_json" >> $GITHUB_OUTPUT
# asset_id is empty if metadata file asset wasn't found
if [ ${#asset_id} -gt 0 ]; then
curl -H "Accept: application/octet-stream" -H "Authorization: Bearer $GH_TOKEN" "https://api.github.com/repos/MODFLOW-USGS/${{ inputs.repo }}/releases/assets/$asset_id" >> code.json
echo "Found code.json for ${{ inputs.repo }} distribution with contents:"
cat code.json
# asset_id is empty if code.json asset wasn't found on the release
if [[ -n "$download_url" ]]; then
echo "Downloading code.json from $download_url to $code_json"
curl -L -H "Authorization: Bearer $GH_TOKEN" "$download_url" >> "$code_json"
echo "Found code.json for distribution '${{ inputs.repo }}' with contents:"
cat "$code_json"
else
# give hashFiles an empty file to hash
echo "No code.json found for ${{ inputs.repo }} distribution, creating empty file"
touch code.json
# give hashFiles an empty file
echo "No code.json found for distribution '${{ inputs.repo }}', creating empty file"
touch "$code_json"
fi
env:
GH_TOKEN: ${{ steps.set-github-token.outputs.token }}
Expand All @@ -99,7 +103,7 @@ runs:
uses: actions/cache@v3
with:
path: ${{ env.MODFLOW_BIN_PATH }}
key: modflow-${{ runner.os }}-${{ inputs.repo }}-${{ hashFiles('code.json') }}-${{ steps.get-date.outputs.date }}
key: modflow-${{ runner.os }}-${{ inputs.repo }}-${{ hashFiles(steps.check-release.outputs.code_json) }}-${{ steps.get-date.outputs.date }}

- name: Install executables
if: inputs.cache != 'true' || steps.cache-modflow.outputs.cache-hit != 'true'
Expand Down

0 comments on commit 92fb40d

Please sign in to comment.