GitHub Actions - uol-library/spacefinder-ui GitHub Wiki

Concatenating space data

Space data is stored in Spacefinder in a series of JSON files within the spaces directory. This is to enable editing of the space data using NetlifyCMS. The Spacefinder application loads data for the spaces as a single data file called spaces.json to optimise delivery of the data, so a GitHub action concat-spacefinder-data was created to concatenate the data in the JSON files within the spaces directory to refresh this master data file. The action is manually triggered, and uses the node.js script _scripts/concat.js which is run using the command npm run concat.

After the new data file has been generated, the action commits any changes to the repository. I'm currently investigating the use of the Git Auto Commit action to further automate this process and guard against the action failing when no changes are made to the data.

Opening Hours Updates

The Libraries in the University of Leeds publish a calendar of their opening hours in JSON format at:

https://library.leeds.ac.uk/opening-hours-calendar

This data feed is a JSON object containing opening hours which is updated regularly by library staff and drives the opening hours and calendar on the library website.

Data from this feed is used to update data for spaces within Spacefinder using a node.js script in _scripts/updateLibraryOpening.js. This script is called using npm run updateOpening and is used in GitHub Actions update-library-opening-hours-manual (manually triggered) and update-library-opening-hours-scheduled (triggered on a schedule at 4:30am every Monday).

The script looks for spaces whose space_type is set to "Library", then looks in the building field which contains either the library name or the "Worsley building" for the Health Sciences library. Existing opening hours are first compared with those in the existing data to see if they differ, and if they do, the opening hours are updated with data from the JSON feed.

The script will update opening times for the current week, starting on Monday. This is the reason why it is scheduled to run on Monday morning to update data in Spacefinder for the current week. The manually triggered action can be used in cases where opening hours are changed at short notice.

After the update, the action runs npm run concat which will concatenate the files and commit any changes to the repository (if any changes have been made).

Scheduled Tasks

Updates on space data can be scheduled by adding instructions to _data/crontab.json. This file contains jobs represented by JSON objects with a date (d-m-yyyy) and updates array.

Updates match spaces on whatever field is specified in the match property of an update, with the value given in the term property of the update. This way, multiple spaces can be matched to update them with the same information.

Updates can be carried out on a single field or multiple fields for each space matched - single fields use the properties field and value, multiple fields use the properties fields and values (which must be identically sized arrays).

{
    "jobs": [
        {
            "date": "13-7-2022",
            "updates": [
                {
                    "match": "title",
                    "term": "Worsley Building - 11.006 (LIDA cluster)",
                    "field": "published",
                    "value": true
                }
            ]
        }
    ]
}

This example will publish the space with the title "Worsley Building - 11.006 (LIDA cluster)" on the 13th July 2022.

These tasks are enabled using the Github Action cron-updates which calls the node.js script _scripts/cron.js, run using the command npm run crontab. This script loads the data file and performs any scheduled updates then runs npm run concat and commits any changes to the repository (if any changes have been made).

Publishing to an Azure Static Web App

You can create static web apps in Azure from Github Pages sites using Github Actions. The easiest way to do this is to set up the Web App in the Azure portal and point it to the repository in Github for the source. This will authenticate with Github (if neccessary) and place an action in the .github/workflows/ directory of your Github repository. After a few failed attempts at doing this with the site build step taking place in Azure, I modified the action so the build takes place in Github instead, with a solution poster here: https://github.com/Azure/static-web-apps/issues/253

The build steps are explicitly performed as part of the Github action, then the built site (in _site) is uploaded to the web app - where the build step is skipped. The updated workflow file looks like this:

name: Azure Static Web Apps CI/CD

on:
  push:
    branches:
      - main
  pull_request:
    types: [opened, synchronize, reopened, closed]
    branches:
      - main

jobs:
  build_and_deploy_job:
    if: github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.action != 'closed')
    runs-on: ubuntu-latest
    name: Build and Deploy Job
    steps:
      - uses: actions/checkout@v3
        with:
          ref: main
      # This part is taken from a discussion on https://github.com/Azure/static-web-apps/issues/253
      # It runs the jekyll build manually - then skip_app_build is set to true in builddeploy step
      - name: Set up Ruby
        uses: ruby/setup-ruby@v1
        with:
          ruby-version: 2.7
      - name: Install Bundler
        run: gem install bundler
      - name: Install dependencies
        run: bundle install --jobs 4 --retry 3
      - name: Jekyll build
        run: bundle exec jekyll build --config _config_prod.yml
      # end part from https://github.com/Azure/static-web-apps/issues/253
      - name: Build And Deploy
        id: builddeploy
        uses: Azure/static-web-apps-deploy@v1
        with:
          azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN_<AZURE_DOMAIN> }}
          repo_token: ${{ secrets.GITHUB_TOKEN }} # Used for Github integrations (i.e. PR comments)
          action: "upload"
          ###### Repository/Build Configurations - These values can be configured to match your app requirements. ######
          # For more information regarding Static Web App workflow configurations, please visit: https://aka.ms/swaworkflowconfig
          app_location: "_site" # App source code path
          api_location: "" # Api source code path - optional
          output_location: "" # Built app content directory - optional
          skip_app_build: true
          ###### End of Repository/Build Configurations ######

  close_pull_request_job:
    if: github.event_name == 'pull_request' && github.event.action == 'closed'
    runs-on: ubuntu-latest
    name: Close Pull Request Job
    steps:
      - name: Close Pull Request
        id: closepullrequest
        uses: Azure/static-web-apps-deploy@v1
        with:
          azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN_<AZURE_DOMAIN> }}
          action: "close"

A few things to note:

  • <AZURE_DOMAIN> is the subdomain of azurestaticapps.net assigned to your static web app in azure.
  • the build step uses the jekyll configuration file _config_prod.yml which contains the domain in the url property.

If the repository contains a file called staticwebapp.config.json it can be used to configure things like HTTP headers and Routes.