Skip to content
Latchkey

Release workflow (highlightjs/highlight.js)

The Release workflow from highlightjs/highlight.js, explained and optimized by Latchkey.

F

CI health: F - at risk

Point runs-on at Latchkey and get caching, run de-duplication, job timeouts, SHA-pinned actions, self-healing for flaky steps, and up to 58% lower cost, applied automatically.

Grade your own workflow free or run it on Latchkey →
Source: highlightjs/highlight.js.github/workflows/release.ymlLicense BSD-3-ClauseView source

What it does

This is the Release workflow from the highlightjs/highlight.js repository, a real project running GitHub Actions. It is shown here with attribution under its BSD-3-Clause license.

Below, Latchkey shows a faster, safer version produced by its optimization engine.

The workflow

workflow (.yml)
name: Release

on:
  push:
    tags:
      - "*alpha*"
      - "*pre*"
      - "*beta*"
      - "1[0-9]+.[0-9]+.[0-9]+"

permissions:
  contents: read

jobs:
  prerelease:
    permissions:
      contents: write # for git push

    name: Release
    runs-on: ubuntu-latest
    steps:
      - name: Checkout highlight.js
        uses: actions/checkout@v7

      - name: Tag is ${{ github.ref }}.
        # we have to repeat ourselves here since the environment is not actually updated
        # until the next step executes, so we can't access $TAG_NAME yet
        run: |
          echo "TAG_NAME=${GITHUB_REF/refs\/tags\//}" >> $GITHUB_ENV
          echo "MAJOR_VERSION=$(echo ${GITHUB_REF/refs\/tags\//} | cut -d'.' -f1)" >> $GITHUB_ENV
      - name: Make sure we are pushing a tag...
        if: ${{ !contains(github.ref,'refs/tags/') }}
        run: false
        # run: echo "TAG_NAME=0.0.0-test0" >> $GITHUB_ENV

      - if: contains(github.ref, 'beta') || contains(github.ref, 'pre') || contains(github.ref, 'alpha')
        run: |
          echo "NPM_TAG=beta" >> $GITHUB_ENV
          echo "RELEASING=beta" >> $GITHUB_ENV

      - if: ${{ !(contains(github.ref, 'beta') || contains(github.ref, 'pre') || contains(github.ref, 'alpha')) }}
        run: |
          echo "NPM_TAG=latest" >> $GITHUB_ENV
          echo "RELEASING=stable" >> $GITHUB_ENV

      - name: match-tag-to-package-version
        uses: geritol/match-tag-to-package-version@0.2.0
        env:
          TAG_PREFIX: refs/tags/ # Optional, default prefix refs/tags/

      - name: Use Node.js 20.x
        uses: actions/setup-node@v6
        with:
          node-version: 20.x
      - name: Build Node.js package
        run: |
          npm install
          node ./tools/build.js -t node
          npm test

      - name: Publish highlight.js to NPM
        id: publish
        uses: JS-DevTools/npm-publish@v4
        with:
          check-version: true
          token: ${{ secrets.NPM_TOKEN }}
          package: ./build/package.json
          tag: ${{ env.NPM_TAG }}

      - if: steps.publish.outputs.type != 'none'
        run: |
          echo "Version changed: ${{ steps.publish.outputs.old-version }} => ${{ steps.publish.outputs.version }}"

        # if stable release
      - name: Stable Release
        if: env.RELEASING == 'stable'
        run: echo "BRANCH_NAME=${MAJOR_VERSION}-stable" >> $GITHUB_ENV
        # else (beta)
      - name: Beta Release
        if: env.RELEASING == 'beta'
        run: echo "BRANCH_NAME=main" >> $GITHUB_ENV
      - name: Confirm release is either stable or beta
        if: ${{ !(env.RELEASING == 'stable' || env.RELEASING == 'beta') }}
        run: |
          echo We seem to be releasing `${RELEASING}`.
          false

      - name: Checkout cdn-release
        uses: actions/checkout@v7
        with:
          repository: 'highlightjs/cdn-release'
          path: 'cdn-release'
          token: ${{ secrets.CDN_REPO_TOKEN }}
          ref: ${{ env.BRANCH_NAME }}

      - name: Build CDN package
        run: node ./tools/build.js -t cdn :common

      - name: Commmit & Push cdn-release ${{ env.TAG_NAME }}
        working-directory: ./cdn-release
        run: |
          rm -r ./build
          mv ../build/ ./build/
          mv ./build/DIGESTS.md .
          git config user.name github-actions
          git config user.email github-actions@github.com
          git add ./build/
          git add ./DIGESTS.md
          git commit -m'Update to version ${{ env.TAG_NAME }}'
          git tag ${TAG_NAME}
          git push -f --atomic origin ${BRANCH_NAME} ${TAG_NAME}

      - name: Publish cdn-assets to NPM
        id: publish_cdn
        uses: JS-DevTools/npm-publish@v4
        with:
          check-version: true
          token: ${{ secrets.NPM_TOKEN }}
          package: ./cdn-release/build/package.json
          tag: ${{ env.NPM_TAG }}

        # log.info('Updating CDN repo at %s' % settings.HLJS_CDN_SOURCE)
        # run(['nodejs', 'tools/build.js', '--target', 'cdn', ':common'])
        # os.chdir(settings.HLJS_CDN_SOURCE)
        # run(['git', 'pull', '-f'])
        # lines = run(['git', '--git-dir', os.path.join(settings.HLJS_CDN_SOURCE, '.git'), 'tag'])
        # build_dir = os.path.join(settings.HLJS_CDN_SOURCE, 'build')
        # if version in lines:
        #     log.info('Tag %s already exists in the local CDN repo' % version)
        # else:
        #     if os.path.exists(build_dir):
        #         shutil.rmtree(build_dir)
        #     shutil.move(os.path.join(settings.HLJS_SOURCE, 'build'), build_dir)
        #     run(['git', 'add', '.'])
        #     run(['git', 'commit', '-m', 'Update to version %s' % version])
        #     run(['git', 'tag', version])
        # run(['git', 'push'])
        # run(['git', 'push', '--tags'])
        # npm_publish(build_dir)
        # os.chdir(settings.HLJS_SOURCE)

The same workflow, on Latchkey

Estimated ~20% faster on cache hits, plus fewer wasted runs and a safer supply chain. Added and changed lines are highlighted.

name: Release
 
on:
  push:
    tags:
      - "*alpha*"
      - "*pre*"
      - "*beta*"
      - "1[0-9]+.[0-9]+.[0-9]+"
 
permissions:
  contents: read
 
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true
 
jobs:
  prerelease:
    timeout-minutes: 30
    permissions:
      contents: write # for git push
 
    name: Release
    runs-on: latchkey-small
    steps:
      - name: Checkout highlight.js
        uses: actions/checkout@v7
 
      - name: Tag is ${{ github.ref }}.
        # we have to repeat ourselves here since the environment is not actually updated
        # until the next step executes, so we can't access $TAG_NAME yet
        run: |
          echo "TAG_NAME=${GITHUB_REF/refs\/tags\//}" >> $GITHUB_ENV
          echo "MAJOR_VERSION=$(echo ${GITHUB_REF/refs\/tags\//} | cut -d'.' -f1)" >> $GITHUB_ENV
      - name: Make sure we are pushing a tag...
        if: ${{ !contains(github.ref,'refs/tags/') }}
        run: false
        # run: echo "TAG_NAME=0.0.0-test0" >> $GITHUB_ENV
 
      - if: contains(github.ref, 'beta') || contains(github.ref, 'pre') || contains(github.ref, 'alpha')
        run: |
          echo "NPM_TAG=beta" >> $GITHUB_ENV
          echo "RELEASING=beta" >> $GITHUB_ENV
 
      - if: ${{ !(contains(github.ref, 'beta') || contains(github.ref, 'pre') || contains(github.ref, 'alpha')) }}
        run: |
          echo "NPM_TAG=latest" >> $GITHUB_ENV
          echo "RELEASING=stable" >> $GITHUB_ENV
 
      - name: match-tag-to-package-version
        uses: geritol/match-tag-to-package-version@0.2.0
        env:
          TAG_PREFIX: refs/tags/ # Optional, default prefix refs/tags/
 
      - name: Use Node.js 20.x
        uses: actions/setup-node@v6
        with:
          cache: 'npm'
          node-version: 20.x
      - name: Build Node.js package
        run: |
          npm install
          node ./tools/build.js -t node
          npm test
 
      - name: Publish highlight.js to NPM
        id: publish
        uses: JS-DevTools/npm-publish@v4
        with:
          check-version: true
          token: ${{ secrets.NPM_TOKEN }}
          package: ./build/package.json
          tag: ${{ env.NPM_TAG }}
 
      - if: steps.publish.outputs.type != 'none'
        run: |
          echo "Version changed: ${{ steps.publish.outputs.old-version }} => ${{ steps.publish.outputs.version }}"
 
        # if stable release
      - name: Stable Release
        if: env.RELEASING == 'stable'
        run: echo "BRANCH_NAME=${MAJOR_VERSION}-stable" >> $GITHUB_ENV
        # else (beta)
      - name: Beta Release
        if: env.RELEASING == 'beta'
        run: echo "BRANCH_NAME=main" >> $GITHUB_ENV
      - name: Confirm release is either stable or beta
        if: ${{ !(env.RELEASING == 'stable' || env.RELEASING == 'beta') }}
        run: |
          echo We seem to be releasing `${RELEASING}`.
          false
 
      - name: Checkout cdn-release
        uses: actions/checkout@v7
        with:
          repository: 'highlightjs/cdn-release'
          path: 'cdn-release'
          token: ${{ secrets.CDN_REPO_TOKEN }}
          ref: ${{ env.BRANCH_NAME }}
 
      - name: Build CDN package
        run: node ./tools/build.js -t cdn :common
 
      - name: Commmit & Push cdn-release ${{ env.TAG_NAME }}
        working-directory: ./cdn-release
        run: |
          rm -r ./build
          mv ../build/ ./build/
          mv ./build/DIGESTS.md .
          git config user.name github-actions
          git config user.email github-actions@github.com
          git add ./build/
          git add ./DIGESTS.md
          git commit -m'Update to version ${{ env.TAG_NAME }}'
          git tag ${TAG_NAME}
          git push -f --atomic origin ${BRANCH_NAME} ${TAG_NAME}
 
      - name: Publish cdn-assets to NPM
        id: publish_cdn
        uses: JS-DevTools/npm-publish@v4
        with:
          check-version: true
          token: ${{ secrets.NPM_TOKEN }}
          package: ./cdn-release/build/package.json
          tag: ${{ env.NPM_TAG }}
 
        # log.info('Updating CDN repo at %s' % settings.HLJS_CDN_SOURCE)
        # run(['nodejs', 'tools/build.js', '--target', 'cdn', ':common'])
        # os.chdir(settings.HLJS_CDN_SOURCE)
        # run(['git', 'pull', '-f'])
        # lines = run(['git', '--git-dir', os.path.join(settings.HLJS_CDN_SOURCE, '.git'), 'tag'])
        # build_dir = os.path.join(settings.HLJS_CDN_SOURCE, 'build')
        # if version in lines:
        #     log.info('Tag %s already exists in the local CDN repo' % version)
        # else:
        #     if os.path.exists(build_dir):
        #         shutil.rmtree(build_dir)
        #     shutil.move(os.path.join(settings.HLJS_SOURCE, 'build'), build_dir)
        #     run(['git', 'add', '.'])
        #     run(['git', 'commit', '-m', 'Update to version %s' % version])
        #     run(['git', 'tag', version])
        # run(['git', 'push'])
        # run(['git', 'push', '--tags'])
        # npm_publish(build_dir)
        # os.chdir(settings.HLJS_SOURCE)
 

What changed

2 third-party actions are referenced by a movable tag. Pin them to the commit SHA (Latchkey resolves and applies this automatically) so a repointed tag cannot change what runs.

What Latchkey heals here

This workflow has steps that commonly fail on transient issues (network, registries, flaky browsers). On Latchkey managed runners they are detected, retried, and self-healed instead of failing your build:

This workflow runs 1 job per trigger. On Latchkey the same minutes cost up to 58% less than GitHub-hosted, with zero queue time.

Actions used in this workflow