Add latest map data #34
Workflow file for this run
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: build-map-data | |
| run-name: Add ${{ inputs.year || 'latest' }} map data | |
| on: | |
| schedule: | |
| # runs at noon UTC on the 1st of each month | |
| - cron: '0 12 1 * *' | |
| workflow_dispatch: | |
| inputs: | |
| year: | |
| description: "Shape file year" | |
| required: false | |
| default: '' | |
| jobs: | |
| download: | |
| runs-on: ubuntu-latest | |
| outputs: | |
| exit_code: ${{ steps.dl-shp.outputs.exit_code }} | |
| shp_year: ${{ steps.dl-shp.outputs.shp_year }} | |
| state_shp: ${{ steps.dl-shp.outputs.state_shp }} | |
| county_shp: ${{ steps.dl-shp.outputs.county_shp }} | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Setup Python | |
| uses: actions/setup-python@v5 | |
| with: | |
| python-version: '3.9' | |
| cache: 'pip' | |
| - name: Install Python dependencies | |
| run: pip install -r data-raw/scripts/requirements.txt | |
| - name: Download shapefiles | |
| id: dl-shp | |
| run: | | |
| python data-raw/scripts/shapefiles.py ${{ inputs.year }} | |
| echo "shp_year=${{ env.shp_year }}" >> "$GITHUB_OUTPUT" | |
| echo "state_shp=${{ env.state_shp }}" >> "$GITHUB_OUTPUT" | |
| echo "county_shp=${{ env.county_shp }}" >> "$GITHUB_OUTPUT" | |
| - name: Upload shapefiles | |
| if: steps.dl-shp.outputs.exit_code == '0' | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: shapefiles | |
| path: data-raw/shapefiles/${{ env.shp_year }} | |
| process: | |
| runs-on: ubuntu-latest | |
| needs: download | |
| if: needs.download.outputs.exit_code == '0' | |
| outputs: | |
| pr_url: ${{ steps.save-pr-info.outputs.pr_url }} | |
| pr_number: ${{ steps.save-pr-info.outputs.pr_number }} | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Download shapefiles | |
| uses: actions/download-artifact@v4 | |
| with: | |
| name: shapefiles | |
| path: data-raw/shapefiles/${{ needs.download.outputs.shp_year }} | |
| - name: Setup R | |
| uses: r-lib/actions/setup-r@v2 | |
| - name: Install R dependencies | |
| uses: r-lib/actions/setup-r-dependencies@v2 | |
| with: | |
| extra-packages: local::. | |
| - name: Modify shapefiles | |
| env: | |
| STATE_SHP: ${{ needs.download.outputs.state_shp }} | |
| COUNTY_SHP: ${{ needs.download.outputs.county_shp }} | |
| YEAR: ${{ needs.download.outputs.shp_year }} | |
| run: | | |
| input_dir <- file.path("data-raw", "shapefiles", Sys.getenv("YEAR")) | |
| output_dir <- file.path("inst", "extdata", Sys.getenv("YEAR")) | |
| usmapdata:::create_us_map( | |
| "states", | |
| file.path(input_dir, Sys.getenv("STATE_SHP")), | |
| output_dir, | |
| "us_states.gpkg" | |
| ) | |
| usmapdata:::create_us_map( | |
| "counties", | |
| file.path(input_dir, Sys.getenv("COUNTY_SHP")), | |
| output_dir, | |
| "us_counties.gpkg" | |
| ) | |
| shell: Rscript {0} | |
| - name: Import GPG signing key | |
| uses: crazy-max/ghaction-import-gpg@v6 | |
| with: | |
| gpg_private_key: ${{ secrets.GPG_PRIVATE_KEY }} | |
| passphrase: ${{ secrets.GPG_PASSPHRASE }} | |
| git_user_signingkey: true | |
| git_commit_gpgsign: true | |
| - name: Determine pull request parameters | |
| id: pr-params | |
| env: | |
| YEAR: ${{ needs.download.outputs.shp_year }} | |
| run: | | |
| echo "branch_name=data-update/$YEAR" >> "$GITHUB_OUTPUT" | |
| echo "pr_title=Add $YEAR map data" >> "$GITHUB_OUTPUT" | |
| - name: Render pull request body | |
| id: pr-body | |
| uses: chuhlomin/render-template@v1 | |
| with: | |
| template: data-raw/scripts/update-data-pr-body.md | |
| vars: | | |
| branch_name: ${{ steps.pr-params.outputs.branch_name }} | |
| - name: Open pull request | |
| id: open-pr | |
| uses: peter-evans/create-pull-request@v5 | |
| with: | |
| token: ${{ secrets.BOT_PAT }} | |
| author: ${{ secrets.BOT_USER }} | |
| committer: ${{ secrets.BOT_USER }} | |
| commit-message: "[automated] Add ${{ needs.download.outputs.shp_year }} map data based on available shapefiles" | |
| branch: ${{ steps.pr-params.outputs.branch_name }} | |
| title: ${{ steps.pr-params.outputs.pr_title }} | |
| body: ${{ steps.pr-body.outputs.result }} | |
| reviewers: pdil | |
| assignees: pdil | |
| labels: data update | |
| delete-branch: true | |
| - name: Save PR info | |
| id: save-pr-info | |
| run: | | |
| echo "pr_url=${{ steps.open-pr.outputs.pull-request-url }}" >> "$GITHUB_OUTPUT" | |
| echo "pr_number=${{ steps.open-pr.outputs.pull-request-number }}" >> "$GITHUB_OUTPUT" | |
| notify: | |
| runs-on: ubuntu-latest | |
| needs: [download, process] | |
| if: always() | |
| env: | |
| PUSHOVER_API_KEY: ${{ secrets.PUSHOVER_API_KEY }} | |
| PUSHOVER_USER_KEY: ${{ secrets.PUSHOVER_USER_KEY }} | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Setup Python | |
| uses: actions/setup-python@v5 | |
| with: | |
| python-version: '3.9' | |
| cache: 'pip' | |
| - name: Install Python dependencies | |
| run: pip install -r data-raw/scripts/requirements.txt | |
| - name: Send success notification | |
| if: needs.download.outputs.exit_code == '0' && needs.process.result == 'success' | |
| run: | | |
| python data-raw/scripts/pushover.py "✅ usmapdata has updated its data files, a PR review is needed: <a href=\"${{ needs.process.outputs.pr_url }}\">PR #${{ needs.process.outputs.pr_number }}</a>" | |
| - name: Send data not found notification | |
| if: needs.download.outputs.exit_code == '404' | |
| run: | | |
| python data-raw/scripts/pushover.py "⚠️ usmapdata failed to find map data files for ${{ needs.download.outputs.shp_year }}." "LOW" | |
| - name: Send failure notification | |
| if: needs.download.outputs.exit_code != '0' && needs.download.outputs.exit_code != '404' | |
| run: | | |
| python data-raw/scripts/pushover.py "❌ usmapdata failed to update map data files. (error: ${{ needs.download.outputs.exit_code }})" "LOW" | |