Update ERP gantry rates #1070
This file contains 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 ERP gantry rates | |
on: | |
schedule: | |
# https://crontab.guru/#30_16_*_*_* | |
# Runs every day at 12.30am (SGT) | |
- cron: "30 16 * * *" | |
workflow_dispatch: | |
env: | |
NODE_VERSION: "16" | |
jobs: | |
fetch-rates: | |
name: "Fetch and process rates" | |
runs-on: ubuntu-latest | |
timeout-minutes: 10 | |
steps: | |
- uses: actions/checkout@v2 | |
- uses: actions/setup-node@v2 | |
with: | |
node-version: ${{ env.NODE_VERSION }} | |
- run: npm ci | |
- name: Fetch rates and update effective date env var | |
env: | |
DATAMALL_ACCOUNT_KEY: ${{ secrets.DATAMALL_ACCOUNT_KEY }} | |
NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }} | |
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }} | |
run: | | |
EFFECTIVE_DATE=$(npm run fetch:rates --silent) | |
npx netlify-cli env:set REACT_APP_RATES_EFFECTIVE_DATE $EFFECTIVE_DATE | |
# Update the last check date env var and trigger a new deploy | |
- name: Update last check and trigger deploy | |
env: | |
NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }} | |
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }} | |
run: | | |
npx netlify-cli env:set REACT_APP_LAST_CHECK_DATE $(npx -y ts-node -e 'console.log(Date.now())') | |
curl -X POST -d {} https://api.netlify.com/build_hooks/${{ secrets.NETLIFY_BUILD_HOOK_ID }} | |
# Using this based on this guide: | |
# https://github.com/peter-evans/create-pull-request/blob/main/docs/concepts-guidelines.md#authenticating-with-github-app-generated-tokens | |
- uses: tibdex/github-app-token@v1 | |
id: generate-token | |
with: | |
app_id: ${{ secrets.APP_ID }} | |
private_key: ${{ secrets.APP_PRIVATE_KEY }} | |
- run: git status | |
# https://stackoverflow.com/a/3879077 | |
# https://stackoverflow.com/q/26568952 | |
- name: Toggle style type in netlify.toml if there are new rates | |
run: | | |
git diff-index --quiet HEAD -- || sed -i 's/REACT_APP_MAPBOX_STYLE_TYPE = "A"/REACT_APP_MAPBOX_STYLE_TYPE = "Z"/;s/REACT_APP_MAPBOX_STYLE_TYPE = "B"/REACT_APP_MAPBOX_STYLE_TYPE = "A"/;s/REACT_APP_MAPBOX_STYLE_TYPE = "Z"/REACT_APP_MAPBOX_STYLE_TYPE = "B"/;' netlify.toml | |
- name: Create Pull Request | |
uses: peter-evans/create-pull-request@v3 | |
with: | |
token: ${{ steps.generate-token.outputs.token }} | |
add-paths: | | |
public/data | |
netlify.toml | |
commit-message: "chore: update rates" | |
branch: "chore/update-rates" | |
delete-branch: true | |
branch-suffix: timestamp | |
title: "chore: update rates" | |
body: | | |
New ERP rates have been detected! | |
labels: data-update | |
reviewers: sebastianquek |