name: Build packages

# Controls when the action will run. Triggers the workflow on push or pull request 
# events but only for the master branch
on:
  push:
    branches: [ master, maint-1.4 ]
  pull_request:
    branches: [ master, maint-1.4 ]
  release:
    types: [ published ]

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
    make-rhel-pkg:
        name: Make RHEL package
        runs-on: ubuntu-latest
        container: centos:latest
        steps:
            - name: Prepare
              run: |
                yum update -y
                yum install -y make rpmdevtools yum-utils

            - name: Clone repo
              uses: actions/checkout@v2

            - name: Make package
              run: |
                yum-builddep --assumeyes packaging/rpm/*.spec
                make -C packaging/rpm pkg-local "DESTDIR=${GITHUB_WORKSPACE}/pkgs/"

            - name: Publish package
              uses: actions/upload-artifact@v1.0.0
              with:
                name: rhel-pkg
                path: pkgs

    make-alpine-pkg:
        name: Make Alpine package
        runs-on: ubuntu-latest
        container: alpine:latest
        steps:
            - name: Prepare
              run: |
                apk update
                apk upgrade
                apk add make
                apk add alpine-sdk
                apk add go
                apk add su-exec

                adduser -G abuild -D jobber
                su jobber -c "abuild-keygen -an"
                find "/home/jobber/.abuild" -name '*.rsa.pub' -exec mv {} /etc/apk/keys/ ';'
                chown root:root /etc/apk/keys/*

            - name: Clone repo
              uses: actions/checkout@v2

            - name: Make package
              run: |
                chmod a+w . packaging/alpine
                su jobber -c "make -C packaging/alpine pkg-local DESTDIR=${GITHUB_WORKSPACE}/pkgs/"

            - name: Publish package
              uses: actions/upload-artifact@v1.0.0
              with:
                name: alpine-pkg
                path: pkgs

    make-debian-pkg:
        name: Make Debian package
        runs-on: ubuntu-latest
        steps:
            - name: Prepare
              run: |
                sudo apt-get update
                sudo apt-get install -y dpkg-dev debhelper dh-systemd

            - name: Clone repo
              uses: actions/checkout@v2

            - name: Build package
              run: make -C "${GITHUB_WORKSPACE}/packaging/debian" pkg-local "DESTDIR=${GITHUB_WORKSPACE}/pkgs/"

            - name: Publish package
              uses: actions/upload-artifact@v1.0.0
              with:
                name: debian-pkg
                path: pkgs

    test-on-macos:
        name: Test on macOS
        runs-on: macos-latest
        steps:
            - name: Prepare
              run: |
                python --version
                brew install socat
                sudo pip install robotframework pyyaml
                sudo sysadminctl -addUser normuser -home /Users/normuser
                sudo createhomedir -c

            - name: Clone repo
              uses: actions/checkout@v2

            - name: Install Jobber
              run: |
                make check build
                sudo make install DESTDIR=/
                PLIST=packaging/darwin/launchd.plist
                sudo chown root:admin "${PLIST}"
                sudo launchctl load "${PLIST}"
                sudo launchctl start info.nekonya.jobber

            - name: Test
              run: |
                if sudo robot --include test --pythonpath platform_tests/keywords platform_tests/suites; then
                    echo "::set-env name=PASSED_TESTS::true"
                else
                    echo "::set-env name=PASSED_TESTS::false"
                fi
                mkdir test-report
                mv *.html test-report/

            - name: Publish test report
              uses: actions/upload-artifact@v1.0.0
              with:
                name: mac-test-report
                path: test-report

            - name: SucceedOrFail
              run: test "${PASSED_TESTS}" = true

    test-on-debian:
        name: Test on Debian
        needs: make-debian-pkg
        runs-on: ubuntu-latest
        steps:
            - name: Prepare
              run: |
                sudo apt-get update
                sudo apt-get install -y python-pip socat
                sudo pip install robotframework pyyaml
                sudo useradd normuser -m

            - name: Download package
              uses: actions/download-artifact@v1
              with:
                name: debian-pkg

            - name: Install package
              run: sudo dpkg -i "$(find ${GITHUB_WORKSPACE} -name *.deb)"

            - name: Clone repo
              uses: actions/checkout@v2

            - name: Test package
              run: |
                if sudo robot --include test --pythonpath platform_tests/keywords platform_tests/suites; then
                    echo "::set-env name=PASSED_TESTS::true"
                else
                    echo "::set-env name=PASSED_TESTS::false"
                fi
                mkdir test-report
                mv *.html test-report/

            - name: Publish test report
              uses: actions/upload-artifact@v1.0.0
              with:
                name: debian-test-report
                path: test-report

            - name: SucceedOrFail
              run: test "${PASSED_TESTS}" = true