name: GitHub Actions CI
on:
  push:
    branches: master
  pull_request:
env:
  HOMEBREW_DEVELOPER: 1
  HOMEBREW_NO_AUTO_UPDATE: 1
  HOMEBREW_GITHUB_ACTIONS_BIG_SUR_TESTING: 1 # TODO: remove when Big Sur is released.
jobs:
  tests:
    if: github.repository == 'Homebrew/brew'
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, macOS-latest, macOS-11.0]
        include:
          - os: ubuntu-latest
            core-tap: 'linuxbrew-core'
          - os: macOS-latest
            core-tap: 'homebrew-core'
          - os: macOS-11.0
            core-tap: 'homebrew-core'
    steps:
    - name: Set up Homebrew
      id: set-up-homebrew
      uses: Homebrew/actions/setup-homebrew@master

    - name: Configure Git user
      uses: Homebrew/actions/git-user-config@master
      with:
        username: BrewTestBot

    - run: brew test-bot --only-cleanup-before

    - name: Set up Xcode
      if: matrix.os == 'macOS-11.0'
      run: sudo xcode-select --switch /Applications/Xcode_12.2.app/Contents/Developer

    - run: brew config

    # Can't cache this because we need to check that it doesn't fail the
    # "uncommitted RubyGems" step with a cold cache.
    - name: Install Bundler RubyGems
      run: brew install-bundler-gems

    - name: Check for uncommitted RubyGems
      run: git diff --stat --exit-code Library/Homebrew/vendor/bundle/ruby

    - run: brew doctor

    - name: Set up Homebrew official command taps
      run: |
        # Setup taps needed for 'brew tests' and 'brew man'
        cd "$(brew --repo)"

        if [ "$RUNNER_OS" = "macOS" ]; then
          brew update-reset Library/Taps/homebrew/homebrew-bundle Library/Taps/homebrew/homebrew-cask Library/Taps/homebrew/homebrew-services
        else
          brew update-reset Library/Taps/homebrew/homebrew-services
        fi

    - name: Run brew style on Homebrew/brew
      run: brew style --display-cop-names

    - name: Run brew man
      run: brew man --fail-if-changed

    - name: Install brew tests dependencies
      if: matrix.os != 'ubuntu-latest'
      run: |
        brew install subversion
        Library/Homebrew/shims/scm/svn --homebrew=print-path
        which svn
        which svnadmin

    - name: Run brew tests
      run: |
        # brew tests doesn't like world writable directories
        if [[ "$RUNNER_OS" = "Linux" ]]; then
            sudo chmod -R g-w,o-w /home/linuxbrew/.linuxbrew/Homebrew
        fi

        # don't bother running all tests on both platforms (for speed)
        if [ "$RUNNER_OS" = "Linux" ]; then
          brew tests --no-compat --online
          brew tests --generic --online
          brew tests --online
        else
          brew tests --online --coverage
        fi
      env:
        HOMEBREW_GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}

        # set variables for coverage reporting
        HOMEBREW_CODECOV_TOKEN: 3ea0364c-80ce-47a3-9fba-93a940d4b5d7

        # These cannot be queried at the macOS level on GitHub Actions.
        HOMEBREW_LANGUAGES: en-GB

    - name: Run brew update-tests
      run: |
        brew update-test
        brew update-test --to-tag
        brew update-test --commit=HEAD
      if: github.event_name == 'pull_request'

    - name: Run brew readall on all taps
      run: brew readall --aliases

    - name: Run brew style on ${{ matrix.core-tap }}
      run: brew style --display-cop-names homebrew/core

    - name: Run brew style on official taps
      run: brew style --display-cop-names homebrew/bundle homebrew/services homebrew/test-bot

    - name: Run brew cask style on all taps
      if: matrix.os != 'ubuntu-latest'
      run: |
        brew tap homebrew/cask
        brew tap homebrew/cask-drivers
        brew tap homebrew/cask-fonts
        brew tap homebrew/cask-versions

        brew cask style

    - name: Run brew audit --skip-style on all taps
      run: brew audit --skip-style

    - run: brew typecheck

    - name: Run vale for docs linting
      run: |
        brew install vale
        vale docs/

    - name: Lint Dockerfile
      if: matrix.os == 'ubuntu-latest'
      run: |
        brew install hadolint
        hadolint Dockerfile

    - name: Build Docker image
      if: matrix.os == 'ubuntu-latest'
      run: docker build -t brew --build-arg=version=16.04 .

    - name: Run brew test-bot --only-formulae --test-default-formula
      run: |
        if [ "$RUNNER_OS" = "Linux" ]; then
          docker run --rm brew brew test-bot --only-formulae --test-default-formula
        else
          brew test-bot --only-formulae --test-default-formula
        fi

    - name: Deploy the Docker image to GitHub Packages and Docker Hub
      if: matrix.os == 'ubuntu-latest' && github.ref == 'refs/heads/master'
      run: |
        echo ${{secrets.HOMEBREW_GITHUB_PACKAGES_API_TOKEN}} | docker login ghcr.io -u BrewTestBot --password-stdin
        docker tag brew "ghcr.io/homebrew/ubuntu16.04:master"
        docker push "ghcr.io/homebrew/ubuntu16.04:master"
        echo ${{secrets.DOCKER_TOKEN}} | docker login -u brewtestbot --password-stdin
        docker tag brew "homebrew/ubuntu16.04:master"
        docker push "homebrew/ubuntu16.04:master"
