name: Release CI

on:
  push:
    branches: [master]
  workflow_dispatch:
    inputs:
      version:
        description: 'release: major|minor|patch'
        required: true
        default: patch

jobs:
  release:
    runs-on: ubuntu-latest
    name: release

    container:
      image: ghcr.io/synthetixio/docker-node/ubuntu:16.13
      credentials:
        username: synthetixio
        password: ${{ secrets.GH_PACKAGES_READ_ONLY }}

    steps:
      - name: Checkout
        uses: actions/checkout@ec3a7ce113134d7a93b817d10a8272cb61118579 # pin@v2
        with:
          fetch-depth: 0
          ref: master

      - name: Set npm cache directory
        run: npm config set cache .npm-cache --global
        continue-on-error: true

      - uses: actions/cache@937d24475381cd9c75ae6db12cb4e79714b926ed # pin@v2
        with:
          path: |
            .npm-cache
            node_modules
          key: ${{ runner.os }}-ubuntu-node-16-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-ubuntu-node-16-
        continue-on-error: true

      # fixes permission issues on docker+npm@7
      - name: Chown workspace
        run: chown -R $(whoami) .

      - name: Install dependencies
        run: npm install --prefer-offline --no-audit

      - name: git config
        run: |
          git config user.name $GIT_USER
          git config user.email $GIT_EMAIL
          mkdir -p ~/.gnupg/
          printf $GPG_KEY | base64 -d > ~/.gnupg/private.key
          gpg --import ~/.gnupg/private.key
          git config commit.gpgsign true
          git config user.signingkey $GIT_SIGNING_KEY
        env:
          GIT_USER: ${{ secrets.GIT_USER }}
          GIT_EMAIL: ${{ secrets.GIT_EMAIL }}
          GPG_KEY: ${{ secrets.GPG_KEY }}
          GIT_SIGNING_KEY: ${{ secrets.GIT_SIGNING_KEY }}

      - name: npm config
        run: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > .npmrc
        env:
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

      - name: Set current npm package version
        id: extract-version
        run: |
          PACKAGE_VERSION=$(cat package.json | grep version | head -1 | awk -F: '{ print $2 }' | sed 's/[",]//g' | tr -d '[[:space:]]')
          echo ::set-output name=current-version::$PACKAGE_VERSION

      - name: Release ${{ github.event.inputs.version }}
        if: github.event_name == 'workflow_dispatch'
        run: |
          git reset --hard
          npm run release:${{ github.event.inputs.version }} -- --ci
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
          GIT_USER: ${{ secrets.GIT_USER }}
          GIT_EMAIL: ${{ secrets.GIT_EMAIL }}
          GPG_KEY: ${{ secrets.GPG_KEY }}
          GIT_SIGNING_KEY: ${{ secrets.GIT_SIGNING_KEY }}

      - name: Release patch:beta
        if: github.event_name == 'push'
        run: |
          git reset --hard

          echo ${{ steps.extract-version.outputs.current-version }}
          if echo ${{ steps.extract-version.outputs.current-version }} | grep -q "beta"; then
            npm run release -- --ci --preRelease
          else
            npm run release:patch -- --ci --preRelease=beta
          fi
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
          GIT_USER: ${{ secrets.GIT_USER }}
          GIT_EMAIL: ${{ secrets.GIT_EMAIL }}
          GPG_KEY: ${{ secrets.GPG_KEY }}
          GIT_SIGNING_KEY: ${{ secrets.GIT_SIGNING_KEY }}
