name: GitHub Actions

on:
  push:
    branches: [ next, master, actions ]
  pull_request:
    branches: [ next ]

jobs:
  build:
    name: build and test
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        compiler: [gcc, clang]
    env:
      CC: ${{ matrix.compiler }}
      DOCKER_PASS: ${{ secrets.DOCKER_PASS }}
      DOCKER_EMAIL: ${{ secrets.DOCKER_EMAIL }}
      DOCKER_USER: ${{ secrets.DOCKER_USER }}
      GH_TOKEN: ${{ secrets.GH_TOKEN }}
      BALTO_TOKEN: ${{ secrets.BALTO_TOKEN }}

    steps:
    - uses: actions/checkout@v4
    - run: git fetch --prune --unshallow
    - name: construct container name
      run: |
        echo "BASENAME=i3wm/travis-base:$(date +'%Y-%m')-$(./travis/ha.sh travis/travis-base.Dockerfile)" >> $GITHUB_ENV
        echo "BASENAME_UBUNTU=i3wm/travis-base-ubuntu:$(date +'%Y-%m')-$(./travis/ha.sh travis/travis-base-ubuntu.Dockerfile)" >> $GITHUB_ENV
    - name: fetch or build Docker container
      run: |
        docker pull ${{ env.BASENAME }} || ./travis/docker-build-and-push.sh ${{ env.BASENAME }} travis/travis-base.Dockerfile
    - name: fetch or build extra Docker containers
      if: github.ref == 'refs/heads/next' && matrix.compiler == 'gcc'
      run: |
        docker pull ${{ env.BASENAME_UBUNTU }} || ./travis/docker-build-and-push.sh ${{ env.BASENAME_UBUNTU }} travis/travis-base-ubuntu.Dockerfile
    - name: build i3
      run: |
        docker run -v $PWD:/usr/src/i3/ -w /usr/src/i3 -e CC ${{ env.BASENAME }} /bin/sh -c 'rm -rf build; mkdir -p build && cd build && CFLAGS="-Wformat -Wformat-security -Wextra -Wno-unused-parameter -Wstrict-prototypes -Wmissing-prototypes -Werror -fno-common -D_FORTIFY_SOURCE=3" meson setup .. -Ddocs=true -Dmans=true -Db_sanitize=address --buildtype=debugoptimized && ninja -v'
    - name: Upload docs html for manual inspection
      uses: actions/upload-artifact@v4
      with:
        name: i3-docs
        path: |
          build/*.html
      if: matrix.compiler == 'gcc'
    - name: check spelling
      run: |
        docker run -v $PWD:/usr/src/i3/ -w /usr/src/i3 ${{ env.BASENAME }} ./travis/check-spelling.pl
    - name: run i3 tests
      run: |
        docker run -v $PWD:/usr/src/i3/ -w /usr/src/i3 -e CC ${{ env.BASENAME }} ./travis/run-tests.sh
    - name: Archive test logs
      uses: actions/upload-artifact@v4
      with:
        name: test-logs
        path: build/testsuite-*
      if: ${{ failure() }}
    - name: build dist tarball
      run: |
        docker run -v $PWD:/usr/src/i3/ -w /usr/src/i3 -e CC ${{ env.BASENAME }} /bin/sh -c 'rm -rf distbuild; mkdir distbuild && cd distbuild && meson setup .. -Ddocs=true -Dmans=true && meson dist --no-tests'
    - name: build Debian packages
      if: github.ref == 'refs/heads/next' && matrix.compiler == 'gcc'
      run: |
        echo "::group::Debian amd64"
        docker run -v $PWD:/usr/src/i3/ -w /usr/src/i3 ${{ env.BASENAME }} ./travis/debian-build.sh deb/debian-amd64/DIST
        echo "::endgroup::"
        echo "::group::Ubuntu amd64"
        docker run -v $PWD:/usr/src/i3/ -w /usr/src/i3 ${{ env.BASENAME_UBUNTU }} ./travis/debian-build.sh deb/ubuntu-amd64/DIST
        echo "::endgroup::"
    - name: push Debian packages to balto
      if: github.ref == 'refs/heads/next' && matrix.compiler == 'gcc'
      run: |
        travis/push-balto.sh
    - name: build docs
      if: github.ref == 'refs/heads/next' && matrix.compiler == 'gcc'
      run: |
        docker run -v $PWD:/usr/src/i3/ -w /usr/src/i3 ${{ env.BASENAME }} ./travis/docs.sh
    - name: push docs to GitHub pages
      if: github.ref == 'refs/heads/next' && matrix.compiler == 'gcc'
      run: |
        travis/deploy-github-pages.sh
  formatting:
    name: Check formatting
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - name: check & print release notes
      run: ./release-notes/generator.pl
    - name: Install dependencies
      run: |
        sudo apt-get install -y clang-format-15
    - name: Check formatting
      run: clang-format-15 --dry-run --Werror $(git ls-files '*.c' '*.h')
    - name: Verify safe wrapper functions are used
      run: ./travis/check-safe-wrappers.sh
