name: Workflow

on:
  push:
    branches:
      - main
      - '[0-9]+.x'
    tags:
      - '*'

jobs:
  build:
    name: Lint, format, test and build
    runs-on: ubuntu-latest

    steps:
      - name: Checkout 🛎️
        uses: actions/checkout@v3

      - name: Use Node LTS ✨
        uses: actions/setup-node@v3
        with:
          node-version: lts/*
          cache: yarn

      - name: Install dependencies 📦️
        run: yarn install --immutable

      - name: Lint 💅
        run: yarn lint

      - name: Format 🔍
        run: yarn format:check

      - name: Test 🧪
        run: yarn test --passWithNoTests

      - name: Build 🔨
        run: yarn build

  build-container:
    name: Build container
    runs-on: ubuntu-latest

    env:
      # Consider replacing Docker repository below, e.g. like this:
      # IMAGE_NAME: ${{ secrets.CONTAINER_REPOSITORY }}/${{ inputs.name }}
      IMAGE_NAME: bjerkio/${{ github.event.repository.name }}
      IS_DEFAULT_BRANCH:
        ${{ github.ref == format('refs/heads/{0}',
        github.event.repository.default_branch) }}

    steps:
      - name: Checkout 🛎️
        uses: actions/checkout@v3

      - name: Docker meta 🏷
        id: meta
        uses: docker/metadata-action@v4
        with:
          images: ${{ env.IMAGE_NAME }}
          # For better caching, images can be tagged with «latest» tag
          # when the branch is the default branch.
          tags: |
            type=raw,value=latest,enable=${{ env.IS_DEFAULT_BRANCH }}
            type=sha,prefix={{branch}}-,priority=1001

      - name: Set up Docker Buildx 🔨
        id: buildx
        uses: docker/setup-buildx-action@v2

      - name: Build Docker 🛠
        uses: docker/build-push-action@v4
        with:
          context: ./
          file: ./Dockerfile
          # push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          # Uncomment the line below to enable caching
          # cache-from: type=registry,ref=${{ env.IMAGE_NAME }}:latest
