Revision 7f9f4408196eaffbe2e3a58f27d48ab9d311140a authored by Niols on 25 November 2023, 12:25:54 UTC, committed by Niols on 25 November 2023, 12:25:54 UTC
1 parent 3cc4d60
Raw File
ci.yaml
---
name: CI

on:
  push:
    branches:
      - main
    tags:
      - "*"
  pull_request:

jobs:
  ############################################################################
  ## Comment on Pull Request

  comment-on-pull-request:
    runs-on: ubuntu-latest
    if: ${{ github.event_name == 'pull_request'
      && github.event.action == 'opened' }}

    steps:
      - name: Give Link to GitHub Pages for this Pull Request
        uses: peter-evans/create-or-update-comment@v3.1.0
        with:
          issue-number: ${{ github.event.number }}
          body: |
            Hey! I will publish the pages for this pull request here:

            - Repository: https://github.com/Niols/dev.scd.niols.fr/
            - Rendering: http://dev.scd.niols.fr/pr/${{ github.event.number }}/

            For each push, you will see something like:

            ![](https://dev.scd.niols.fr/github-actions-commit-link-example.png)

            Just wait for the deployment indicated by the green check mark and you are good to go!
          token: ${{ secrets.PR_COMMENT_TOKEN }}

  ############################################################################
  ## Setup Dependencies

  setup-dependencies:
    runs-on: ubuntu-latest

    steps:
      - name: Clone Repository
        uses: actions/checkout@v4

      - name: Setup Nix
        uses: ./.github/composites/setup-nix
        with:
          githubAccessToken: ${{ secrets.GITHUB_TOKEN }}
          cachixAuthToken: ${{ secrets.CACHIX_AUTH_TOKEN }}

      - name: Build environment
        run: nix develop --command true

  ############################################################################
  ## Build Website

  build-website:
    runs-on: ubuntu-latest
    needs: setup-dependencies

    steps:
      - name: Clone Repository
        uses: actions/checkout@v4

      - name: Setup Nix
        uses: ./.github/composites/setup-nix
        with:
          githubAccessToken: ${{ secrets.GITHUB_TOKEN }}
          cachixAuthToken: ${{ secrets.CACHIX_AUTH_TOKEN }}

      - name: Build Website
        run: nix develop --command make website

      - name: Upload Website as Artifact
        uses: actions/upload-artifact@v3.1.3
        with:
          name: website
          path: _build/website

  ############################################################################
  ## Build Documentation

  build-documentation:
    runs-on: ubuntu-latest

    steps:
      - name: Clone Repository
        uses: actions/checkout@v4

      - name: Install JSON Schema Documentation Generator
        run: |
          pip install json-schema-for-humans==0.41.3

      - name: Generate Documentation for Schemas
        run: |
          mkdir -p _build/doc/schema
          generate-schema-doc 'database/_schema/*.yaml' _build/doc/schema

      - name: Upload Documentation as Artifact
        uses: actions/upload-artifact@v3.1.3
        with:
          name: documentation
          path: _build/doc

  ############################################################################
  ## Test Schemas

  test-schemas:
    runs-on: ubuntu-latest

    steps:
      - name: Clone Repository
        uses: actions/checkout@v4

      - name: Install YAML to JSON Converter and JSON Schema Validator
        run: |
          wget https://github.com/mikefarah/yq/releases/download/v4.25.3/yq_linux_386 -qO yq
          chmod +x yq
          pip install jsonschema==4.7.2

      - name: Convert All Schemas and Database to JSON
        run: |
          find database -name '*.yaml' | while read file; do
            printf 'Converting `%s`.\n' "$file"
            ./yq --output-format json "$file" > "${file%.yaml}".json
          done

      - name: Run JSON Validator for all Schemas on the Database
        run: |
          for type in dance tune book; do
            for file in database/$type/*.json; do
              jsonschema --output pretty --instance "$file" database/_schema/$type.json
            done
          done

  ############################################################################
  ## Test Website

  test-website:
    runs-on: ubuntu-latest
    needs: setup-dependencies

    steps:
      - name: Clone Repository
        uses: actions/checkout@v4

      - name: Setup Nix
        uses: ./.github/composites/setup-nix
        with:
          githubAccessToken: ${{ secrets.GITHUB_TOKEN }}
          cachixAuthToken: ${{ secrets.CACHIX_AUTH_TOKEN }}

      - name: Build Test Website
        run: nix develop --command make test-website

      - name: Run Tests
        run: nix develop --command make tests

      - name: Upload Tests as Artifact
        if: ${{ always() }}
        ## NOTE: The tests are productive even when they fail. Therefore, it
        ## can be useful to upload the archive no matter what. In worst case,
        ## it will be empty.
        uses: actions/upload-artifact@v3.1.3
        with:
          name: tests
          path: _build/tests

  ############################################################################
  ## Run Flake Checks

  test-flake-checks:
    runs-on: ubuntu-latest
    needs: setup-dependencies

    steps:
      - name: Clone Repository
        uses: actions/checkout@v4

      - name: Setup Nix
        uses: ./.github/composites/setup-nix
        with:
          githubAccessToken: ${{ secrets.GITHUB_TOKEN }}
          cachixAuthToken: ${{ secrets.CACHIX_AUTH_TOKEN }}

      - name: Run Flake Checks
        run: nix flake check --print-build-logs

  ############################################################################
  ## Publish Website

  publish-website:
    runs-on: ubuntu-latest
    needs:
      - build-website
      - test-website
      - test-flake-checks

    if: ${{ github.ref == 'refs/heads/main' }}

    steps:
      - name: Download Website from Artifacts
        uses: actions/download-artifact@v3.0.2
        with:
          name: website
          path: ./website

      - name: Publish Website to GitHub Pages
        uses: peaceiris/actions-gh-pages@v3.9.2
        with:
          ## Publish `./website` with following commit message...
          publish_dir: ./website
          full_commit_message: |
            Bump to ${{ github.sha }}: ${{ github.event.head_commit.message }}
          ## ...to branch `pages` with the right access token...
          publish_branch: pages
          github_token: ${{ secrets.PAGES_TOKEN }}
          ## ...as the GitHub Actions bot...
          user_name: "github-actions[bot]"
          user_email: "github-actions[bot]@users.noreply.github.com"
          ## ...serving `scd.niols.fr`.
          cname: scd.niols.fr

  ############################################################################
  ## Publish Dev Website

  publish-dev-website:
    runs-on: ubuntu-latest
    needs:
      - build-website
      - build-documentation
      - test-website

    ## NOTE: We publish even if one of the previous steps failed; hopefully,
    ## publishing it will then give more debug information. We do not do the
    ## same for the actual website.
    if: always()

    steps:
      - name: Download Website from Artifacts
        if: needs.build-website.result == 'success'
        uses: actions/download-artifact@v3.0.2
        with:
          name: website
          path: ./website

      - name: Create Website Root if Building Website Failed
        if: needs.build-website.result != 'success'
        run: mkdir ./website

      - name: Download Documentation from Artifacts
        if: needs.build-documentation.result == 'success'
        uses: actions/download-artifact@v3.0.2
        with:
          name: documentation
          path: ./website/_doc/

      - name: Download Tests from Artifacts
        ## NOTE: No need to check for success, because the tests upload their
        ## archive no matter what happens.
        uses: actions/download-artifact@v3.0.2
        with:
          name: tests
          path: ./website/_tests/

      - name: Prepare Metadata for Publishing the Dev Website
        run: |
          if [ -n "$GITHUB_HEAD_REF" ]; then
            ## If it is a pull request...
            pr=${GITHUB_REF_NAME%/merge}
            dir=pr/$pr
            ptr="$GITHUB_REPOSITORY#$pr"
          else
            ## Otherwise, if it is a branch or a tag...
            dir=$GITHUB_REF_TYPE/$GITHUB_REF_NAME
            ptr="$GITHUB_REPOSITORY@$GITHUB_SHA"
          fi
          ## In any case, similar messages:
          msg="Bump \`$dir\` to follow $ptr"
          echo "destination_dir=$dir" >> $GITHUB_ENV
          echo "commit_message=$msg" >> $GITHUB_ENV

      - name: Publish Dev Website to GitHub Pages
        uses: peaceiris/actions-gh-pages@v3.9.2
        with:
          ## Publish `./website` with following commit message...
          publish_dir: ./website
          full_commit_message: ${{ env.commit_message }}
          ## ...to https://github.com/Niols/dev.scd.niols.fr on branch `pages`
          ## with the right access token...
          external_repository: niols/dev.scd.niols.fr
          publish_branch: pages
          personal_token: ${{ secrets.DEV_PAGES_TOKEN }}
          ## ...as the GitHub Actions bot...
          user_name: "github-actions[bot]"
          user_email: "github-actions[bot]@users.noreply.github.com"
          ## ...in its own sub-directory while keeping the rest intact...
          destination_dir: ${{ env.destination_dir }}
          keep_files: true
          ## ...serving `dev.scd.niols.fr`.
          cname: dev.scd.niols.fr

  ############################################################################
  ## Check all

  check-ci:
    runs-on: ubuntu-latest
    needs:
      - comment-on-pull-request
      - build-website
      - build-documentation
      - test-schemas
      - test-website
      - test-flake-checks
      - publish-website
      - publish-dev-website

    ## Run only if no dependency failed. We allow them to have been skipped.
    if: ${{ failure() != true }}

    steps:
      - name: Everything was Successful!
        run: true
back to top