name: Publish

on:
  workflow_dispatch:
  push:
    tags:
      - 'v*.*.*'

jobs:
  cli:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - uses: pnpm/action-setup@v2
      - uses: actions/setup-node@v3
        with:
          cache: 'pnpm'
          node-version-file: '.nvmrc'
          registry-url: 'https://registry.npmjs.org'

      - run: |
          pnpm install --frozen-lockfile
          pnpm run build

      - run: npm publish
        working-directory: ./packages/cli
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

  docker:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: docker/setup-qemu-action@v2
      - uses: docker/setup-buildx-action@v2
        with:
          install: true
      - name: Docker Labels
        id: meta
        uses: docker/metadata-action@v4
        with:
          images: cupcakearmy/cryptgeon
          tags: |
            type=semver,pattern={{version}}
            type=semver,pattern={{major}}.{{minor}}
            type=semver,pattern={{major}}
      - name: Login to DockerHub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      - name: Build and push
        uses: docker/build-push-action@v4
        with:
          platforms: linux/amd64,linux/arm64
          push: true
          tags: ${{ steps.meta.outputs.tags }}