name: Docker

on:
  push:
    branches:
    paths:
    - '.dockerignore'
    - '.github/workflows/docker.yml'
    - 'Dockerfile'
    - 'LICENSE'
    - 'ghc-parser/**'
    - 'html/**'
    - 'ihaskell-display/**'
    - 'ihaskell.cabal'
    - 'ipython-kernel/**'
    - 'jupyterlab-ihaskell/**'
    - 'main/**'
    - 'src/**'
    - 'stack.yaml'
  pull_request:
    paths:
    - '.dockerignore'
    - '.github/workflows/docker.yml'
    - 'Dockerfile'
    - 'LICENSE'
    - 'ghc-parser/**'
    - 'html/**'
    - 'ihaskell-display/**'
    - 'ihaskell.cabal'
    - 'ipython-kernel/**'
    - 'jupyterlab-ihaskell/**'
    - 'main/**'
    - 'src/**'
    - 'stack.yaml'

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  build:
    if: (github.event_name != 'pull_request' && ! github.event.pull_request.head.repo.fork) || (github.event_name == 'pull_request' && github.event.pull_request.head.repo.fork)
    runs-on: ubuntu-latest

    strategy:
      fail-fast: false

    steps:
    - uses: actions/checkout@v3

    - uses: haskell-actions/setup@v2
      name: Setup Haskell Stack
      with:
        enable-stack: true
        stack-version: "latest"

    - name: Check Dockerfile GHC version matches
      run: |
        set -e
        STACK_GHC_VERSION=$(stack exec -- ghc --version | awk '{ print $NF }')
        DOCKER_GHC_VERSION=$(sed -n 's/ARG GHC_VERSION=\(.*\)/\1/p' Dockerfile)
        if [[ ${STACK_GHC_VERSION} != ${DOCKER_GHC_VERSION} ]]; then
          echo 'GHC_VERSION in Dockerfile does not match stack resolver'
          echo "GHC_VERSION should be ${STACK_GHC_VERSION}"
          exit 1
        fi

    - uses: elgohr/Publish-Docker-Github-Action@v5
      with:
        name: gibiansky/ihaskell
        username: ${{ secrets.DOCKER_USERNAME }}
        password: ${{ secrets.DOCKER_PASSWORD }}
        snapshot: true
        no_push: ${{ github.event_name == 'pull_request' }}