devlato/async-wait-until

View on GitHub
.github/workflows/release.yml

Summary

Maintainability
Test Coverage
name: Release

on:
  push:
    branches:
      - master
    paths-ignore:
      - CHANGELOG.md

jobs:
  lint:
    name: Lint
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          set-safe-directory: 'true'
      - name: Install dependencies
        run: npm ci --ignore-scripts
      - name: Lint the codebase
        run: npm run lint:ci
  test_and_report_coverage:
    name: Coverage
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          set-safe-directory: 'true'
      - name: Install dependencies
        run: npm ci --ignore-scripts
      - name: Run tests
        run: npm run test:ci
      - name: Upload coverage
        uses: actions/upload-artifact@v4
        with:
          name: test_coverage
          path: coverage
  build_code:
    name: Build code
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          set-safe-directory: 'true'
      - name: Install dependencies
        run: npm ci --ignore-scripts
      - name: Build code
        run: npm run build:ci
      - name: Upload the dist folder
        uses: actions/upload-artifact@v4
        with:
          name: dist
          path: dist
  integration_tests:
    name: Run integration tests
    runs-on: ubuntu-latest
    needs: build_code
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          set-safe-directory: 'true'
      - name: Install dependencies
        run: npm ci --ignore-scripts
      - name: Download dist/ folder
        uses: actions/download-artifact@v4
        with:
          name: dist
          path: dist
      - name: Run integration tests
        run: npm run test:integration:ci
  build_docs:
    name: Build Docs
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          set-safe-directory: 'true'
      - name: Install dependencies
        run: npm ci --ignore-scripts
      - name: Build storybook
        run: npm run docs:ci
      - name: Upload the docs
        uses: actions/upload-artifact@v4
        with:
          name: docs
          path: docs
  changelog:
    name: Generate new changelog
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          set-safe-directory: 'true'
          ssh-key: '${{ secrets.SSH_PRIVATE_KEY }}'
          persist-credentials: 'true'
          fetch-tags: 'true'
          fetch-depth: 0
      - name: Generate changelog
        run: |
          changelog=""

          # Get all tags in ascending order
          tags=($( git tag --sort=version:refname ))

          # Loop through tags to generate the changelog
          for ((i=0; i<${#tags[@]}; i++)); do
            current_tag="${tags[$i]}"
            previous_tag=""
            tag_log=""

            if [ -z "${current_tag}" ]; then
              continue
            fi
            
            # Determine the previous tag if not the first tag
            if (( i > 0 )); then
              previous_tag="${tags[$((i-1))]}"
            fi
            
            # Generate the header for the current tag
            tag_log="### ${current_tag}\n"
            
            # Get the commits between the current tag and the previous tag
            if [ -n "$previous_tag" ]; then
              commits=$(git log --format="%s (%an) [%h]" "${previous_tag}..${current_tag}")
            else
              # If no previous tag, include all commits up to the first tag
              commits=$(git log --format="%s (%an) [%h]" "${current_tag}")
            fi
            
            # Add commits to the changelog, one per line
            while IFS= read -r commit; do
              tag_log+="- ${commit}\n"
            done <<< "${commits}"

            changelog="${tag_log}\n${changelog}"
          done

          changelog="# Changelog\n\n${changelog}"

          echo -e "${changelog}"

          # You can now use the variable $changelog elsewhere in your script
          printf '%b' "${changelog}" > CHANGELOG.md
      - name: Upload new changelog
        uses: actions/upload-artifact@v4
        with:
          name: changelog
          path: CHANGELOG.md
      - name: Commit and push the new changelog
        run: |
          if [ -z $(git status -uno --porcelain) ]; then
            printf 'Changelogs are identical, nothing to commit\n'
          else
            printf 'Committing the updated changelog\n'
            git config --local user.name "github-actions[bot]"
            git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"

            git add CHANGELOG.md
            git commit -am "Update the project changelog"
          fi
      - name: Push changes
        uses: ad-m/github-push-action@v0.8.0
        with:
          ssh: true
          branch: '${{ github.ref }}'
  maybe_tag:
    name: Maybe tag the release
    runs-on: ubuntu-latest
    needs: [lint, test_and_report_coverage, build_code, build_docs, integration_tests, changelog]
    outputs:
      tag_name: '${{ steps.tagging.outputs.tagname }}'
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          set-safe-directory: 'true'
      - name: Install dependencies
        run: npm ci --ignore-scripts
      - name: Maybe generate tag
        uses: Klemensas/action-autotag@stable
        with:
          GITHUB_TOKEN: '${{ secrets.GITHUB_TOKEN }}'
          tag_prefix: ''
          tag_suffix: ''
          changelog_structure: "**{{messageHeadline}}** {{author}}\n"
  trigger_publish:
    name: Maybe trigger publish
    runs-on: ubuntu-latest
    needs: [maybe_tag]
    if: ${{ needs.maybe_tag.outputs.tagname != '' }}
    steps:
      - name: Download the tag file
        uses: actions/download-artifact@v4
        with:
          name: tag_lock
          path: tag_lock
      - name: Checkout
        uses: actions/checkout@v4
        with:
          set-safe-directory: 'true'
          fetch-tags: 'true'
      - name: Trigger the publish workflow
        run: gh workflow run publish.yml --ref ${{ needs.maybe_tag.outputs.tagname }}
        env:
          GITHUB_TOKEN: '${{ secrets.GITHUB_TOKEN }}'