name: Continuous Deployment

on:
  push:
    branches:
      - main

env:
  NODE_OPTIONS: --max_old_space_size=6144

jobs:
  release-canary:
    runs-on: ubuntu-20.04
    name: Release Canary
    if: "!startsWith(github.event.head_commit.message, 'chore(release): v')"
    steps:
      - name: Checkout credo-ts
        uses: actions/checkout@v4
        with:
          # pulls all commits (needed for lerna to correctly version)
          fetch-depth: 0

      # setup dependencies
      - name: Setup Libindy
        uses: ./.github/actions/setup-libindy

      - name: Setup NodeJS
        uses: actions/setup-node@v4
        with:
          node-version: 18
          cache: 'yarn'
          registry-url: 'https://registry.npmjs.org/'

      - name: Install dependencies
        run: yarn install --frozen-lockfile

      # On push to main, release unstable version
      - name: Release Unstable
        run: |
          export NEXT_VERSION_BUMP=$(./node_modules/.bin/ts-node ./scripts/get-next-bump.ts)
          yarn lerna publish --loglevel=verbose --canary $NEXT_VERSION_BUMP --exact --force-publish --yes --no-verify-access --dist-tag alpha
        env:
          NPM_TOKEN: ${{ secrets.NPM_PUBLISH }}
          NODE_AUTH_TOKEN: ${{ secrets.NPM_PUBLISH }}

      - name: Get version number
        id: get-version
        run: |
          LAST_RELEASED_VERSION=$(npm view @aries-framework/core@alpha version)

          echo version="${LAST_RELEASED_VERSION}" >> "$GITHUB_OUTPUT"

      - name: Setup git user
        run: |
          git config --global user.name "github-actions[bot]"
          git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"

      - name: Set git tag
        run: |
          git tag v${{ steps.get-version.outputs.version }}
          git push origin v${{ steps.get-version.outputs.version }} --no-verify

  release-stable:
    runs-on: ubuntu-20.04
    name: Create Stable Release
    # Only run if the last pushed commit is a release commit
    if: "startsWith(github.event.head_commit.message, 'chore(release): v')"
    steps:
      - name: Checkout aries-framework-javascript
        uses: actions/checkout@v4

      # setup dependencies
      - name: Setup Libindy
        uses: ./.github/actions/setup-libindy

      - name: Setup NodeJS
        uses: actions/setup-node@v4
        with:
          node-version: 18
          cache: 'yarn'
          registry-url: 'https://registry.npmjs.org/'

      - name: Install dependencies
        run: yarn install --frozen-lockfile

      - name: Get updated version
        id: new-version
        run: |
          NEW_VERSION=$(node -p "require('./lerna.json').version")
          echo $NEW_VERSION

          echo version="${NEW_VERSION}" >> "$GITHUB_OUTPUT"

      - name: Create Tag
        uses: mathieudutour/github-tag-action@v6.1
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          custom_tag: ${{ steps.new-version.outputs.version }}

      - name: Create Release
        uses: softprops/action-gh-release@v1
        with:
          tag_name: v${{ steps.new-version.outputs.version }}
          body: |
            Release v${{ steps.new-version.outputs.version }}

            You can find the changelog in the [CHANGELOG.md](https://github.com/${{ github.repository }}/blob/main/CHANGELOG.md) file.

      - name: Release to NPM
        run: yarn lerna publish from-package --loglevel=verbose --yes --no-verify-access
        env:
          NPM_TOKEN: ${{ secrets.NPM_PUBLISH }}
          NODE_AUTH_TOKEN: ${{ secrets.NPM_PUBLISH }}
