name: Build packages

on:
    push:
    pull_request:
    release:
        types:
            - published
            - prereleased

jobs:
    build:
        runs-on: ubuntu-latest
        
        steps:
        - uses: actions/checkout@v2
        - name: install docker
          run: |
            sudo apt-get remove docker docker-engine docker.io containerd runc
            sudo apt-get update
            sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
            curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
            sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
            sudo apt-get update
            sudo apt-get install docker-ce docker-ce-cli containerd.io
        - name: prepare docker
          run: |
            mkdir -p $HOME/.docker
            echo '{ "experimental": "enabled" }' | tee $HOME/.docker/config.json
            echo '{ "experimental": true, "storage-driver": "overlay2", "max-concurrent-downloads": 50, "max-concurrent-uploads": 50, "graph": "/mnt/docker" }' | sudo tee /etc/docker/daemon.json
            sudo systemctl restart docker
            docker info
            docker buildx create --use --name mybuild
        - name: build
          run: |
            make cross_build
            cd _packages && for var in $(ls); do sudo bash -c "echo $(sha256sum $var | awk '{print $1}') > $var.sha256"; done && cd -
        - uses: actions/upload-artifact@v1
          with:
            name: packages
            path: _packages/.

    build-on-mac:
        runs-on: macos-latest

        steps:
        - uses: actions/checkout@v2
        - uses: actions/setup-go@v1
          with:
            go-version: '1.13.10'
        - name: prepare
          run: |
              /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
              brew install curl zip unzip gnu-sed upx pkg-config zmq
              echo "/usr/local/bin:$PATH" >> ~/.bashrc
        - name: build
          run: |
            make pkg
            cd _packages && for var in $(ls); do openssl dgst -sha256 $var | awk '{print $2}' > $var.sha256; done && cd -
        - uses: actions/upload-artifact@v1
          with:
            name: packages-mac
            path: _packages/.

    build-docker-images:
        runs-on: ubuntu-latest
        
        steps:
        - uses: actions/checkout@v2
        - name: install docker
          run: |
            sudo apt-get remove docker docker-engine docker.io containerd runc
            sudo apt-get update
            sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
            curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
            sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
            sudo apt-get update
            sudo apt-get install docker-ce docker-ce-cli containerd.io
        - name: prepare docker
          run: |
            mkdir -p $HOME/.docker
            echo '{ "experimental": "enabled" }' | tee $HOME/.docker/config.json
            echo '{ "experimental": true, "storage-driver": "overlay2", "max-concurrent-downloads": 50, "max-concurrent-uploads": 50, "graph": "/mnt/docker" }' | sudo tee /etc/docker/daemon.json
            sudo systemctl restart docker
            docker version
            docker buildx create --use --name mybuild
        - name: build docker image
          run: make docker
        - name: build docker images
          if: github.event_name == 'release'
          run: |
            echo ${{ secrets.DockerHubPassword }} | docker login -u ${{ secrets.DockerHubUser }} --password-stdin
            make cross_docker
      
    release:
        runs-on: ubuntu-latest

        needs: [build, build-on-mac]

        steps:
        - uses: actions/checkout@v2
        - uses: actions/download-artifact@v1
          with:
            name: packages
            path: _packages
        - uses: actions/download-artifact@v1
          with:
            name: packages-mac
            path: _packages
        - name: check packages
          run: |
            cd _packages && for var in $( ls |grep -v sha256); do
              echo "$(cat $var.sha256) $var" | sha256sum -c || exit 1
            done
        - name: update github release
          if: github.event_name == 'release'
          run: |
            version=$(echo ${{ github.ref }} | sed -r  "s .*/.*/(.*) \1 g")
            for var in $(ls _packages) ; do
              .github/workflows/script/upload_github_release_asset.sh owner=emqx repo=kuiper tag=$version filename=_packages/$var github_api_token=$(echo ${{ secrets.AccessToken }})
            done
        - name: create invalidation for cloudfront
          if: github.event_name == 'release'
          run: |
            version=$(echo ${{ github.ref }} | sed -r  "s .*/.*/(.*) \1 g")
            curl "https://d1vvhvl2y92vvt.cloudfront.net/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
            unzip awscliv2.zip
            sudo ./aws/install
            aws configure set aws_access_key_id ${{ secrets.AwsAccessKeyId }}
            aws configure set aws_secret_access_key ${{ secrets.AwsSecretAccessKey }}
            aws configure set default.region us-west-2
            aws s3 rm --quiet --recursive s3://packages.emqx.io/kuiper/$version
            aws s3 cp --quiet --recursive ./_packages s3://packages.emqx.io/kuiper/$version
            aws cloudfront create-invalidation --distribution-id E3TYD0WSP4S14P --paths "/kuiper/$version/*"
        - name: update emqx.io
          if: github.event_name == 'release'
          run: |
            version=$(echo ${{ github.ref }} | sed -r  "s .*/.*/(.*) \1 g")
            curl -w %{http_code} --insecure -H ${{ secrets.EmqxHeader }} https://admin.emqx.io/api/v1/kuiper_github_release_callback?tag=$version
        - name: update helm packages
          if: github.event_name == 'release'
          run: |
            version=$(echo ${{ github.ref }} | sed -r  "s .*/.*/(.*) \1 g")
            curl -H "Authorization: token ${{ secrets.AccessToken }}" -H "Accept: application/vnd.github.everest-preview+json" -H "Content-Type: application/json" -X POST -d "{\"event_type\":\"update_repos_for_kuiper\",\"client_payload\":{\"version\": \"$version\"}}" https://api.github.com/repos/emqx/emqx-packages-docker/dispatches
        