name: Build FFmpeg

on:
  push:
    paths-ignore:
      - '**.md'
      - 'LICENSE'
  workflow_dispatch:
    inputs:
      doRelease:
        description: 'Publish new release'
        type: boolean
        default: false
        required: false

env:
  DOCKER_BUILDKIT: 1

jobs:
  build_base:
    name: Build base image
    if: ${{ github.event.inputs.buildOnly != 'true' }}
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Install buildx
        uses: docker/setup-buildx-action@v2
        with:
          config: .github/buildkit.toml
      - name: Login to Docker
        uses: docker/login-action@v2
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ github.token }}
      - name: Image Name
        id: imagename
        run: echo "::set-output name=name::ghcr.io/${GITHUB_REPOSITORY,,}/base"
      - name: Stop Commands
        run: T="$(echo -n ${{ github.token }} | sha256sum | head -c 64)" && echo -e "::add-mask::${T}\n::stop-commands::${T}"
      - name: Build target base image
        uses: docker/build-push-action@v3
        with:
          context: images/base
          pull: true
          push: true
          tags: ${{ steps.imagename.outputs.name }}:latest
          cache-to: type=registry,mode=max,ref=${{ steps.imagename.outputs.name }}:cache
          cache-from: type=registry,ref=${{ steps.imagename.outputs.name }}:cache
  build_target_bases:
    name: Build target base image
    if: ${{ github.event.inputs.buildOnly != 'true' }}
    needs: build_base
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        target: [win64,linux64,linuxarm64]
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Install buildx
        uses: docker/setup-buildx-action@v2
        with:
          config: .github/buildkit.toml
      - name: Login to Docker
        uses: docker/login-action@v2
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ github.token }}
      - name: Image Name
        id: imagename
        run: echo "::set-output name=name::ghcr.io/${GITHUB_REPOSITORY,,}/base-${{ matrix.target }}"
      - name: Stop Commands
        run: T="$(echo -n ${{ github.token }} | sha256sum | head -c 64)" && echo -e "::add-mask::${T}\n::stop-commands::${T}"
      - name: Build target base image
        uses: docker/build-push-action@v3
        with:
          context: images/base-${{ matrix.target }}
          pull: true
          push: true
          tags: ${{ steps.imagename.outputs.name }}:latest
          cache-to: type=registry,mode=max,ref=${{ steps.imagename.outputs.name }}:cache
          cache-from: type=registry,ref=${{ steps.imagename.outputs.name }}:cache
  build_targets:
    name: Build target-variant image
    if: ${{ github.event.inputs.buildOnly != 'true' }}
    needs: build_target_bases
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        target: [win64,linux64,linuxarm64]
        variant: [gpl,lgpl,gpl 4.4,gpl 5.1,lgpl 4.4,lgpl 5.1,gpl-shared,lgpl-shared,gpl-shared 4.4,gpl-shared 5.1,lgpl-shared 4.4,lgpl-shared 5.1]
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Install buildx
        uses: docker/setup-buildx-action@v2
        with:
          config: .github/buildkit.toml
      - name: Login to Docker
        uses: docker/login-action@v2
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ github.token }}
      - name: Generate Dockerfile
        run: ./generate.sh ${{ matrix.target }} ${{ matrix.variant }}
      - name: Image Name
        id: imagename
        run: |
          IMG="ghcr.io/${GITHUB_REPOSITORY,,}/${{ matrix.target }}-${{ matrix.variant }}"
          echo "::set-output name=name::${IMG/ /-}"
      - name: Stop Commands
        run: T="$(echo -n ${{ github.token }} | sha256sum | head -c 64)" && echo -e "::add-mask::${T}\n::stop-commands::${T}"
      - name: Build target base image
        uses: docker/build-push-action@v3
        with:
          context: .
          pull: true
          push: true
          tags: ${{ steps.imagename.outputs.name }}:latest
          cache-to: type=registry,mode=max,ref=${{ steps.imagename.outputs.name }}:cache
          cache-from: type=registry,ref=${{ steps.imagename.outputs.name }}:cache
  build_ffmpeg:
    name: Build ffmpeg
    if: ${{ ( github.event.inputs.buildOnly == 'true' && !cancelled() ) || success() }}
    needs: build_targets
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        target: [win64,linux64,linuxarm64]
        variant: [gpl,lgpl,gpl 4.4,gpl 5.1,lgpl 4.4,lgpl 5.1,gpl-shared,lgpl-shared,gpl-shared 4.4,gpl-shared 5.1,lgpl-shared 4.4,lgpl-shared 5.1]
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Login to Docker
        uses: docker/login-action@v2
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ github.token }}
      - name: Build ffmpeg
        run: |
          T="$(echo -n ${{ github.token }} | sha256sum | head -c 64)" && echo -e "::add-mask::${T}\n::stop-commands::${T}"
          ./build.sh ${{ matrix.target }} ${{ matrix.variant }}
      - name: Upload artifacts
        uses: actions/upload-artifact@v3
        with:
          name: ffmpeg
          path: artifacts/*
  publish_release:
    name: Publish release
    if: ${{ !cancelled() && ( github.event_name == 'schedule' || github.event.inputs.doRelease == 'true' ) && needs.build_ffmpeg.result == 'success' }}
    needs: build_ffmpeg
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Download artifacts
        uses: actions/download-artifact@v3
        with:
          name: ffmpeg
          path: artifacts
      - name: Create release
        id: create_release
        run: |
          set -xe
          shopt -s nullglob
          RELDATE="$(date +'%Y-%m-%d %H:%M')"
          NAME="Auto-Build $RELDATE"
          TAGNAME="autobuild-$(date +'%Y-%m-%d-%H-%M')"
          hub release create $(for a in artifacts/*.{zip,tar.xz}; do echo -a $a; done) -m "$NAME" -t "master" "$TAGNAME"
          echo "::set-output name=tag_name::${TAGNAME}"
          echo "::set-output name=rel_date::${RELDATE}"
        env:
          GITHUB_TOKEN: ${{ github.token }}
      - name: Update Latest
        run: |
          set -xe
          shopt -s nullglob
          mkdir latest_artifacts
          ./util/repack_latest.sh latest_artifacts artifacts/*.{zip,tar.xz}
          NAME="Latest Auto-Build (${{ steps.create_release.outputs.rel_date }})"
          TAGNAME="latest"
          hub release delete "$TAGNAME" || true
          git push --delete origin "$TAGNAME" || true
          sleep 30
          hub release create $(for a in latest_artifacts/*; do echo -a $a; done) -m "$NAME" -t "master" "$TAGNAME"
        env:
          GITHUB_TOKEN: ${{ github.token }}
      - name: Update Wiki
        run: ./util/update_wiki.sh artifacts ${{ steps.create_release.outputs.tag_name }}
        env:
          GITHUB_TOKEN: ${{ github.token }}
