Update Currency Data #52
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Update Currency Data | |
| on: | |
| # Run every day at 00:30 UTC to fetch previous day's finalized rates | |
| # Best practice: Fetch yesterday's data after it's fully settled | |
| # Timezone: UTC (GitHub Actions always runs in UTC) | |
| # Avoids high-load periods (top of hour) for better reliability | |
| schedule: | |
| - cron: '30 0 * * *' | |
| # Allow manual trigger for testing | |
| workflow_dispatch: | |
| permissions: | |
| contents: write | |
| pull-requests: write | |
| jobs: | |
| update-currency-data: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| token: ${{ github.token }} | |
| - name: Fetch and save currency data | |
| id: fetch | |
| env: | |
| API_KEY: ${{ secrets.CURRENCY_KEY }} | |
| API_URL: ${{ secrets.CURRENCY_URL }} | |
| run: | | |
| set -e | |
| # Configuration | |
| BASE_CURRENCY="EUR" | |
| # Get yesterday's date to fetch finalized rates | |
| # Using 'date -d' for yesterday ensures we get fully settled forex data | |
| CURRENT_DATE=$(date -u -d 'yesterday' +"%Y-%m-%d") | |
| echo "date=${CURRENT_DATE}" >> $GITHUB_OUTPUT | |
| YEAR=$(date -u -d 'yesterday' +"%Y") | |
| MONTH=$(date -u -d 'yesterday' +"%B") | |
| FILENAME=$(date -u -d 'yesterday' +"%d-%m-%Y.json") | |
| echo "Timezone: UTC (GitHub Actions)" | |
| echo "Fetching data for: ${CURRENT_DATE} (previous day's finalized rates)" | |
| # Construct directory path | |
| DATA_DIR="src/Data/${BASE_CURRENCY}/${YEAR}/${MONTH}" | |
| FILE_PATH="${DATA_DIR}/${FILENAME}" | |
| echo "Fetching currency data for ${CURRENT_DATE}..." | |
| # Check if file already exists | |
| if [ -f "${FILE_PATH}" ]; then | |
| echo "Data for ${CURRENT_DATE} already exists" | |
| echo "has_changes=false" >> $GITHUB_OUTPUT | |
| exit 0 | |
| fi | |
| # Create directory structure | |
| mkdir -p "${DATA_DIR}" | |
| echo "Created directory: ${DATA_DIR}" | |
| # Fetch data from API | |
| RESPONSE=$(curl -s -w "\n%{http_code}" "${API_URL}/${CURRENT_DATE}?access_key=${API_KEY}&base=${BASE_CURRENCY}") | |
| # Extract HTTP status code | |
| HTTP_CODE=$(echo "${RESPONSE}" | tail -n1) | |
| BODY=$(echo "${RESPONSE}" | sed '$d') | |
| # Check HTTP status | |
| if [ "${HTTP_CODE}" != "200" ]; then | |
| echo "Error: HTTP request failed with status ${HTTP_CODE}" | |
| echo "Response: ${BODY}" | |
| exit 1 | |
| fi | |
| # Check if API request was successful | |
| SUCCESS=$(echo "${BODY}" | jq -r '.success // false') | |
| if [ "${SUCCESS}" != "true" ]; then | |
| ERROR_INFO=$(echo "${BODY}" | jq -r '.error.info // "Unknown error"') | |
| echo "Error: API returned - ${ERROR_INFO}" | |
| exit 1 | |
| fi | |
| # Extract and save only the rates data | |
| echo "${BODY}" | jq -S '.rates' > "${FILE_PATH}" | |
| echo "Successfully saved currency data to ${FILE_PATH}" | |
| echo "has_changes=true" >> $GITHUB_OUTPUT | |
| - name: Create Pull Request | |
| if: steps.fetch.outputs.has_changes == 'true' | |
| id: cpr | |
| uses: peter-evans/create-pull-request@v6 | |
| with: | |
| token: ${{ github.token }} | |
| commit-message: 'chore: update currency data for ${{ steps.fetch.outputs.date }}' | |
| branch: currency-update-${{ github.run_number }} | |
| delete-branch: true | |
| title: 'Update currency data for ${{ steps.fetch.outputs.date }}' | |
| body: | | |
| Automated currency exchange rate update. | |
| - Date: ${{ steps.fetch.outputs.date }} | |
| - Base Currency: EUR | |
| - Run: #${{ github.run_number }} | |
| Auto-generated by GitHub Actions | |
| labels: automated | |
| - name: Enable auto-merge for PR | |
| if: steps.cpr.outputs.pull-request-number != '' | |
| env: | |
| GH_TOKEN: ${{ github.token }} | |
| PR_NUMBER: ${{ steps.cpr.outputs.pull-request-number }} | |
| run: | | |
| gh pr merge --auto --rebase --delete-branch "$PR_NUMBER" |