name: Deploy
on:
  push:
    branches:
      - main
      - "v*.*"
    tags:
      - "v*.*.*"
jobs:
  assets:
    outputs:
      sha: ${{ steps.push_assets.outputs.commit_hash }}
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - run: |
          . versions
          echo "elixir=$elixir" >> $GITHUB_ENV
          echo "otp=$otp" >> $GITHUB_ENV
      - name: Install Erlang & Elixir
        uses: erlef/setup-beam@v1
        with:
          otp-version: ${{ env.otp }}
          elixir-version: ${{ env.elixir }}
      - name: Cache Mix
        uses: actions/cache@v3
        with:
          path: |
            deps
            _build
          key: ${{ runner.os }}-mix-${{ env.elixir }}-${{ env.otp }}-${{ hashFiles('**/mix.lock') }}
          restore-keys: |
            ${{ runner.os }}-mix-${{ env.elixir }}-${{ env.otp }}-
      # Note: we need to get Phoenix and LV because package.json points to them directly
      - name: Install mix dependencies
        run: mix deps.get
      - name: Install Node
        uses: actions/setup-node@v3
        with:
          node-version: "18.x"
      - name: Cache npm dependencies
        uses: actions/cache@v3
        with:
          path: ~/.npm
          key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-node-
      - name: Clean generated assets
        run: rm -rf static/assets
      - name: Install npm dependencies
        run: npm ci --prefix assets
      - name: Build assets
        run: npm run deploy --prefix assets
      - name: Push updated assets
        id: push_assets
        uses: stefanzweifel/git-auto-commit-action@v4
        with:
          commit_message: Update assets
          file_pattern: static
  docker:
    if: github.ref_type == 'tag' || github.ref == 'refs/heads/main'
    # The assets job may push new commit, so we wait for it
    needs: [assets]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
        with:
          # The output is set only if there was a commit, otherwise
          # we pass an empty ref and and the default is used
          ref: ${{ needs.assets.outputs.sha }}
      - run: |
          . versions
          echo "elixir=$elixir" >> $GITHUB_ENV
          echo "otp=$otp" >> $GITHUB_ENV
          echo "debian=$debian" >> $GITHUB_ENV
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2
      - name: Login to GitHub Container Registry
        uses: docker/login-action@v2
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - name: Docker meta
        id: meta
        uses: docker/metadata-action@v3
        with:
          images: ghcr.io/livebook-dev/livebook
          tags: |
            type=semver,pattern={{version}}
            type=edge,branch=main
      - name: Build and push
        uses: docker/build-push-action@v4
        with:
          context: .
          platforms: linux/amd64,linux/arm64
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          cache-from: type=gha
          cache-to: type=gha,mode=max
          build-args: |
            BASE_IMAGE=hexpm/elixir:${{ env.elixir }}-erlang-${{ env.otp }}-debian-${{ env.debian }}

  create_draft_release:
    if: github.ref_type == 'tag'
    permissions:
      contents: write
    runs-on: ubuntu-20.04
    env:
      GITHUB_TOKEN: ${{ secrets.GH_RELEASE_TOKEN }}
    steps:
      - name: Create draft release
        run: |
          gh release create \
            --repo ${{ github.repository }} \
            --title ${{ github.ref_name }} \
            --draft \
            ${{ github.ref_name }}

      - name: Trigger desktop builds
        run: |
          gh workflow run -R livebook-dev/livebook_cd build.yml -f ref_name=${{ github.ref_name }}
