name: Linux

on:
  pull_request:
    paths:
      - '**.c'
      - '**.h'
      - .github/workflows/linux.yml
      - .github/setup-linux.sh
      - .github/build.sh
      - .github/push-artifacts.sh
      - '**.am'
  push:

permissions:
  contents: read  #  to fetch code (actions/checkout)

jobs:
  build:
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v2
      - run: .github/setup-linux.sh
      - run: .github/build.sh dist
      - name: Upload test logs
        uses: actions/upload-artifact@v2
        if: failure()
        with:
          name: ubuntu-test-logs
          path:
            tests/*.log
      - uses: actions/cache@v2
        id: cache-build
        with:
          path: ./*
          key: ${{ runner.os }}-${{ github.sha }}
      - name: Upload build artifacts
        uses: actions/upload-artifact@v2
        with:
          name: opensc-build
          path:
            opensc*.tar.gz

  build-no-shared:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - run: .github/setup-linux.sh
      - run: .github/build.sh no-shared

  build-ix86:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - run: .github/setup-linux.sh ix86
      - run: .github/build.sh ix86

  build-mingw:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - run: .github/setup-linux.sh mingw
      - run: .github/build.sh mingw
      - name: Cache build artifacts
        uses: actions/upload-artifact@v2
        with:
          name: opensc-build-mingw
          path:
            win32/Output/OpenSC*.exe

  build-mingw32:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - run: .github/setup-linux.sh mingw32
      - run: .github/build.sh mingw32
      - name: Cache build artifacts
        uses: actions/upload-artifact@v2
        with:
          name: opensc-build-mingw32
          path:
            win32/Output/OpenSC*.exe

  test-piv:
    runs-on: ubuntu-20.04
    needs: [build]
    steps:
      - uses: actions/checkout@v2
      - uses: actions/cache@v2
        id: cache-build
        with:
          path: ./*
          key: ${{ runner.os }}-${{ github.sha }}
      - run: .github/setup-linux.sh piv
      - run: .github/test-piv.sh

  test-isoapplet:
    runs-on: ubuntu-20.04
    needs: [build]
    steps:
      - uses: actions/checkout@v2
      - uses: actions/cache@v2
        id: cache-build
        with:
          path: ./*
          key: ${{ runner.os }}-${{ github.sha }}
      - run: .github/setup-linux.sh isoapplet
      - run: .github/test-isoapplet.sh

  test-gidsapplet:
    runs-on: ubuntu-20.04
    needs: [build]
    steps:
      - uses: actions/checkout@v2
      - uses: actions/cache@v2
        id: cache-build
        with:
          path: ./*
          key: ${{ runner.os }}-${{ github.sha }}
      - run: .github/setup-linux.sh gidsapplet
      - run: .github/test-gidsapplet.sh

  test-openpgp:
    runs-on: ubuntu-20.04
    needs: [build]
    steps:
      - uses: actions/checkout@v2
      - uses: actions/cache@v2
        id: cache-build
        with:
          path: ./*
          key: ${{ runner.os }}-${{ github.sha }}
      - run: .github/setup-linux.sh openpgp
      - run: .github/test-openpgp.sh

  build-clang-tidy:
    runs-on: ubuntu-20.04
    needs: [build]
    steps:
      - uses: actions/checkout@v2
      - uses: actions/cache@v2
        id: cache-build
        with:
          path: ./*
          key: ${{ runner.os }}-${{ github.sha }}
      - run: .github/setup-linux.sh clang-tidy
      - run: .github/build.sh

  test-cac:
    runs-on: ubuntu-20.04
    needs: [build]
    steps:
      - uses: actions/checkout@v2
      - uses: actions/cache@v2
        id: cache-build
        with:
          path: ./*
          key: ${{ runner.os }}-${{ github.sha }}
      - run: .github/setup-linux.sh cac
      - run: .github/test-cac.sh

  test-oseid:
    runs-on: ubuntu-20.04
    needs: [build]
    steps:
      - uses: actions/checkout@v2
      - uses: actions/cache@v2
        id: cache-build
        with:
          path: ./*
          key: ${{ runner.os }}-${{ github.sha }}
      - run: .github/setup-linux.sh oseid
      - run: .github/test-oseid.sh

  ############################################
  ## Ubuntu 22.04 with OpenSSL 3.0 pipeline ##
  ############################################
  build-ubuntu-22:
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v3
      - run: .github/setup-linux.sh
      - run: .github/build.sh dist
      - uses: actions/upload-artifact@v3
        if: failure()
        with:
          name: ubuntu-22-test-logs
          path:
            tests/*.log
      - uses: actions/cache@v2
        id: cache-build
        if: ${{ success() }}
        with:
          path: ./*
          key: ${{ runner.os }}-22-${{ github.sha }}
      - name: Upload artifacts
        uses: actions/upload-artifact@v2
        if: ${{ success() }}
        with:
          name: opensc-build
          path:
            opensc*.tar.gz

  test-cac-ubuntu-22:
    runs-on: ubuntu-22.04
    needs: [build-ubuntu-22]
    steps:
      - uses: actions/checkout@v3
      - uses: actions/cache@v3
        id: cache-build
        with:
          path: ./*
          key: ${{ runner.os }}-22-${{ github.sha }}
      - run: .github/setup-linux.sh cac
      - run: .github/test-cac.sh

  test-oseid-ubuntu-22:
    runs-on: ubuntu-22.04
    needs: [build-ubuntu-22]
    steps:
      - uses: actions/checkout@v3
      - uses: actions/cache@v3
        id: cache-build
        with:
          path: ./*
          key: ${{ runner.os }}-22-${{ github.sha }}
      - run: .github/setup-linux.sh oseid
      - run: .github/test-oseid.sh

  #######################
  ## LibreSSL pipeline ##
  #######################
  build-libressl:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - run: .github/setup-linux.sh libressl
      - run: .github/build.sh dist libressl
      - uses: actions/upload-artifact@v3
        if: failure()
        with:
          name: libressl-logs
          path: |
            tests/test-suite.log
            config.log
      - uses: actions/cache@v2
        id: cache-build
        if: ${{ success() }}
        with:
          path: ./*
          key: ${{ runner.os }}-libressl-${{ github.sha }}

  test-cac-libressl:
    runs-on: ubuntu-latest
    needs: [build-libressl]
    steps:
      - uses: actions/checkout@v3
      - uses: actions/cache@v3
        id: cache-build
        with:
          path: ./*
          key: ${{ runner.os }}-libressl-${{ github.sha }}
      - run: .github/setup-linux.sh cac libressl
      - run: .github/test-cac.sh

  test-oseid-libressl:
    runs-on: ubuntu-latest
    needs: [build-libressl]
    steps:
      - uses: actions/checkout@v3
      - uses: actions/cache@v3
        id: cache-build
        with:
          path: ./*
          key: ${{ runner.os }}-libressl-${{ github.sha }}
      - run: .github/setup-linux.sh oseid libressl
      - run: .github/test-oseid.sh

  ####################
  ## Push artifacts ##
  ####################
  push-artifacts:
    runs-on: ubuntu-latest
    needs: [build, build-mingw]
    steps:
      - uses: actions/checkout@v2
      - uses: actions/cache@v2
        id: cache-build
        with:
          path: ./*
          key: ${{ runner.os }}-${{ github.sha }}
      - name: Pull mingw build artifacts
        uses: actions/download-artifact@v2
        with:
          name: opensc-build-mingw
      - run: git config --global user.email "builds@github.com"
      - run: git config --global user.name "Github Actions";
      - run: .github/push_artifacts.sh "Github Actions ${GITHUB_REF}"
        if: ${{ github.repository == 'OpenSC/OpenSC' }}
