Skip to content

Commit 12419cb

Browse files
ci: enhance currency data update workflow
Prevents creating pull requests when currency data for the current date already exists. Improves pull request titles, commit messages, and descriptions by including the update date. Ensures consistency of generated JSON data by sorting keys alphabetically. Adds `set -e` for script robustness and logs API response on HTTP errors for better debugging. Automatically deletes the feature branch upon merging a pull request.
1 parent 7255da1 commit 12419cb

1 file changed

Lines changed: 16 additions & 9 deletions

File tree

.github/workflows/update-currency-data.yml

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,19 @@ jobs:
2424
token: ${{ github.token }}
2525

2626
- name: Fetch and save currency data
27+
id: fetch
2728
env:
2829
API_KEY: ${{ secrets.CURRENCY_KEY }}
2930
API_URL: ${{ secrets.CURRENCY_URL }}
3031
run: |
32+
set -e
33+
3134
# Configuration
3235
BASE_CURRENCY="EUR"
3336
3437
# Get current date in UTC
3538
CURRENT_DATE=$(date -u +"%Y-%m-%d")
39+
echo "date=${CURRENT_DATE}" >> $GITHUB_OUTPUT
3640
YEAR=$(date -u +"%Y")
3741
MONTH=$(date -u +"%B")
3842
FILENAME=$(date -u +"%d-%m-%Y.json")
@@ -46,14 +50,13 @@ jobs:
4650
# Check if file already exists
4751
if [ -f "${FILE_PATH}" ]; then
4852
echo "Data for ${CURRENT_DATE} already exists"
53+
echo "has_changes=false" >> $GITHUB_OUTPUT
4954
exit 0
5055
fi
5156
52-
# Create directory structure if it doesn't exist
53-
if [ ! -d "${DATA_DIR}" ]; then
54-
echo "Creating directory: ${DATA_DIR}"
55-
mkdir -p "${DATA_DIR}"
56-
fi
57+
# Create directory structure
58+
mkdir -p "${DATA_DIR}"
59+
echo "Created directory: ${DATA_DIR}"
5760
5861
# Fetch data from API
5962
RESPONSE=$(curl -s -w "\n%{http_code}" "${API_URL}/${CURRENT_DATE}?access_key=${API_KEY}&base=${BASE_CURRENCY}")
@@ -65,6 +68,7 @@ jobs:
6568
# Check HTTP status
6669
if [ "${HTTP_CODE}" != "200" ]; then
6770
echo "Error: HTTP request failed with status ${HTTP_CODE}"
71+
echo "Response: ${BODY}"
6872
exit 1
6973
fi
7074
@@ -77,22 +81,25 @@ jobs:
7781
fi
7882
7983
# Extract and save only the rates data
80-
echo "${BODY}" | jq '.rates' > "${FILE_PATH}"
84+
echo "${BODY}" | jq -S '.rates' > "${FILE_PATH}"
8185
8286
echo "Successfully saved currency data to ${FILE_PATH}"
87+
echo "has_changes=true" >> $GITHUB_OUTPUT
8388
8489
- name: Create Pull Request
90+
if: steps.fetch.outputs.has_changes == 'true'
8591
id: cpr
8692
uses: peter-evans/create-pull-request@v6
8793
with:
8894
token: ${{ github.token }}
89-
commit-message: "chore: update currency data"
95+
commit-message: 'chore: update currency data for ${{ steps.fetch.outputs.date }}'
9096
branch: currency-update-${{ github.run_number }}
9197
delete-branch: true
92-
title: "Update currency data"
98+
title: 'Update currency data for ${{ steps.fetch.outputs.date }}'
9399
body: |
94100
Automated currency exchange rate update.
95101
102+
- Date: ${{ steps.fetch.outputs.date }}
96103
- Base Currency: EUR
97104
- Run: #${{ github.run_number }}
98105
@@ -105,4 +112,4 @@ jobs:
105112
GH_TOKEN: ${{ github.token }}
106113
PR_NUMBER: ${{ steps.cpr.outputs.pull-request-number }}
107114
run: |
108-
gh pr merge --auto --squash "$PR_NUMBER"
115+
gh pr merge --auto --squash --delete-branch "$PR_NUMBER"

0 commit comments

Comments
 (0)