Skip to content

Commit

Permalink
ci(workflow): add cache buster and exit on no updates
Browse files Browse the repository at this point in the history
  • Loading branch information
dannysteenman committed Aug 23, 2024
1 parent a0b2538 commit 48e2ed8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 15 deletions.
15 changes: 9 additions & 6 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
name: Fetch CloudFormation resource specification and update this VSCode extension.
on:
workflow_dispatch:
schedule:
- cron: "10 11 * * 5" # At 11:10 on Friday.
jobs:
build:
runs-on: ubuntu-latest
Expand All @@ -22,9 +17,17 @@ jobs:
sudo apt install python3-setuptools
python3 -m pip install --upgrade pip && pip3 install -r src/requirements.txt
- name: Check if AWS updated the CloudFormation resource specification
id: check_spec
run: |
echo "Check if the cfn resource spec has been updated"
python3 src/check-cfn-resource-spec-hash.py
python3 src/check-cfn-resource-spec-hash.py || echo "::set-output name=spec_updated::false"
continue-on-error: true

- name: Stop if no changes
if: steps.check_spec.outputs.spec_updated == 'false'
run: |
echo "No updates to the CloudFormation resource spec. Exiting workflow."
exit 0
- name: Update the yaml cfn resource type snippets
run: |
echo "Update the yaml cfn resource type snippets"
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
node_modules
*.vsix
.mypy_cache
.python-version

# Ignore everything in the test folder
/test/*
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ If you have a feature request or an issue, please let me know on [Github](https:

## Author

Danny Steenman
[Danny Steenman](https://towardsthecloud.com/about)

[![](https://img.shields.io/badge/LinkedIn-0077B5?style=for-the-badge&logo=linkedin&logoColor=white)](https://www.linkedin.com/in/dannysteenman)
[![](https://img.shields.io/badge/X-000000?style=for-the-badge&logo=x&logoColor=white)](https://twitter.com/dannysteenman)
Expand Down
22 changes: 14 additions & 8 deletions src/check-cfn-resource-spec-hash.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
import hashlib
from datetime import datetime, timezone

import requests


def get_resource_spec():
# Add a cache buster using a timezone-aware datetime object
cfn_resource_spec_url = "https://d1uauaxba7bl26.cloudfront.net/latest/gzip/CloudFormationResourceSpecification.json"
# Load the source data and hash it
response = requests.get(cfn_resource_spec_url)

current_time = datetime.now(timezone.utc).timestamp()
response = requests.get(cfn_resource_spec_url, params={"nocache": current_time})
response.raise_for_status()
return response


def calculate_hash(content):
return hashlib.sha256(content).hexdigest()


# Load the current hash
with open("src/current-cfn-spec-hash", "r+") as file:
current_hash = file.read()
# Load the source data and hash it
new_hash = hashlib.md5(get_resource_spec().content).hexdigest()
current_hash = file.read().strip()
new_hash = calculate_hash(get_resource_spec().content)

if new_hash == current_hash:
print(f"The new hash: {new_hash} matches with our current hash: {current_hash}.")
print("The snippets are up-to-date, stopping the pipeline.")
exit(1)
else:
print(
f"Found an update in the cfn-resource-specification, let's update the hash to: {new_hash}"
" and continue with updating the cfn resource snippets!"
f"Found an update in the cfn-resource-specification, let's update the hash to: {new_hash} "
"and continue with updating the cfn resource snippets!"
)
file.seek(0)
file.write(new_hash)
file.truncate()

0 comments on commit 48e2ed8

Please sign in to comment.