name: "Release"

# @see https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#onpushpull_requestbranchestags
on:
  push:
    tags:
      - v4*

jobs:
  test:
    name: test
    runs-on: ubuntu-20.04

    steps:
      ##################################################################################################################
      ##################################################################################################################
      ##################################################################################################################
      # Git checkout
      - name: Checkout repository
        uses: actions/checkout@v2

      # The github.ref is, for example, refs/tags/v4.0.145 or refs/tags/v4.0-r8
      # Generate variables like:
      #   SRS_TAG=v4.0-r8
      #   SRS_TAG=v4.0.145
      #   SRS_VERSION=4.0.145
      #   SRS_VERSION=4.0-r8
      #   SRS_MAJOR=4
      # @see https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-environment-variable
      - name: Generate varaiables
        run: |
          SRS_TAG=$(echo ${{ github.ref }}| awk -F '/' '{print $3}')
          echo "SRS_TAG=$SRS_TAG" >> $GITHUB_ENV
          SRS_VERSION=$(echo ${SRS_TAG}| sed 's/^v//g')
          echo "SRS_VERSION=$SRS_VERSION" >> $GITHUB_ENV
          SRS_MAJOR=$(echo $SRS_TAG| cut -c 2)
          echo "SRS_MAJOR=$SRS_MAJOR" >> $GITHUB_ENV

      # Tests
      - name: Build test image
        run: docker build --tag srs:test -f trunk/Dockerfile.test .
      # For utest
      - name: Run SRS utest
        run: docker run --rm srs:test bash -c 'make utest && ./objs/srs_utest'
      # For regression-test
      - name: Run SRS regression-test
        run: |
          docker run --rm srs:test bash -c 'make && ./objs/srs -c conf/regression-test.conf && \
          cd 3rdparty/srs-bench && make && ./objs/srs_test -test.v'

  docker-srs:
    name: docker-srs
    runs-on: ubuntu-20.04
    needs:
      - test

    steps:
      ##################################################################################################################
      ##################################################################################################################
      ##################################################################################################################
      # Git checkout
      - name: Checkout repository
        uses: actions/checkout@v2

      # See https://github.com/crazy-max/ghaction-docker-buildx#moved-to-docker-organization
      # https://github.com/docker/setup-qemu-action
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v1
      # https://github.com/docker/setup-buildx-action
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1

      # The github.ref is, for example, refs/tags/v4.0.145 or refs/tags/v4.0-r8
      # Generate variables like:
      #   SRS_TAG=v4.0-r8
      #   SRS_TAG=v4.0.145
      #   SRS_VERSION=4.0.145
      #   SRS_VERSION=4.0-r8
      #   SRS_MAJOR=4
      # @see https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-environment-variable
      - name: Generate varaiables
        run: |
          SRS_TAG=$(echo ${{ github.ref }}| awk -F '/' '{print $3}')
          echo "SRS_TAG=$SRS_TAG" >> $GITHUB_ENV
          SRS_VERSION=$(echo ${SRS_TAG}| sed 's/^v//g')
          echo "SRS_VERSION=$SRS_VERSION" >> $GITHUB_ENV
          SRS_MAJOR=$(echo $SRS_TAG| cut -c 2)
          echo "SRS_MAJOR=$SRS_MAJOR" >> $GITHUB_ENV

      ##################################################################################################################
      ##################################################################################################################
      ##################################################################################################################
      # Create main images for Docker
      - name: Login to docker hub
        uses: docker/login-action@v1
        with:
          username: "${{ secrets.DOCKER_USERNAME }}"
          password: "${{ secrets.DOCKER_PASSWORD }}"
      - name: Build and push images to Docker hub
        env:
          PACKAGER: ${{ secrets.SRS_PACKAGER_DOCKER }}
        run: |
          echo "Release ossrs/srs:$SRS_TAG"
          docker buildx build --platform linux/arm/v7,linux/arm64/v8,linux/amd64 \
            --output "type=image,push=true" \
            -t ossrs/srs:$SRS_TAG --build-arg SRS_AUTO_PACKAGER=$PACKAGER -f trunk/Dockerfile .

      # Docker alias images
      - name: Docker alias images for ossrs/srs
        uses: akhilerm/tag-push-action@v2.0.0
        with:
          src: ossrs/srs:${{ env.SRS_TAG }}
          dst: |
            ossrs/srs:${{ env.SRS_VERSION }}
            ossrs/srs:${{ env.SRS_MAJOR }}
            ossrs/srs:v${{ env.SRS_MAJOR }}
            ossrs/srs:latest

      # Aliyun ACR
      - name: Login aliyun hub
        uses: docker/login-action@v1
        with:
          registry: registry.cn-hangzhou.aliyuncs.com
          username: "${{ secrets.ACR_USERNAME }}"
          password: "${{ secrets.ACR_PASSWORD }}"
      - name: Push to Aliyun registry for ossrs/srs
        uses: akhilerm/tag-push-action@v2.0.0
        with:
          src: ossrs/srs:${{ env.SRS_TAG }}
          dst: |
            registry.cn-hangzhou.aliyuncs.com/ossrs/srs:${{ env.SRS_TAG }}
            registry.cn-hangzhou.aliyuncs.com/ossrs/srs:${{ env.SRS_VERSION }}
            registry.cn-hangzhou.aliyuncs.com/ossrs/srs:${{ env.SRS_MAJOR }}
            registry.cn-hangzhou.aliyuncs.com/ossrs/srs:v${{ env.SRS_MAJOR }}
            registry.cn-hangzhou.aliyuncs.com/ossrs/srs:latest

  docker-droplet:
    name: docker-droplet
    runs-on: ubuntu-20.04
    needs:
      - test

    steps:
      ##################################################################################################################
      ##################################################################################################################
      ##################################################################################################################
      # Git checkout
      - name: Checkout repository
        uses: actions/checkout@v2

      # See https://github.com/crazy-max/ghaction-docker-buildx#moved-to-docker-organization
      # https://github.com/docker/setup-qemu-action
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v1
      # https://github.com/docker/setup-buildx-action
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1

      # The github.ref is, for example, refs/tags/v4.0.145 or refs/tags/v4.0-r8
      # Generate variables like:
      #   SRS_TAG=v4.0-r8
      #   SRS_TAG=v4.0.145
      #   SRS_VERSION=4.0.145
      #   SRS_VERSION=4.0-r8
      #   SRS_MAJOR=4
      # @see https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-environment-variable
      - name: Generate varaiables
        run: |
          SRS_TAG=$(echo ${{ github.ref }}| awk -F '/' '{print $3}')
          echo "SRS_TAG=$SRS_TAG" >> $GITHUB_ENV
          SRS_VERSION=$(echo ${SRS_TAG}| sed 's/^v//g')
          echo "SRS_VERSION=$SRS_VERSION" >> $GITHUB_ENV
          SRS_MAJOR=$(echo $SRS_TAG| cut -c 2)
          echo "SRS_MAJOR=$SRS_MAJOR" >> $GITHUB_ENV

      ##################################################################################################################
      ##################################################################################################################
      ##################################################################################################################
      # Create main images for Docker
      - name: Login to docker hub
        uses: docker/login-action@v1
        with:
          username: "${{ secrets.DOCKER_USERNAME }}"
          password: "${{ secrets.DOCKER_PASSWORD }}"
      - name: Build and push images to Docker hub
        env:
          PACKAGER_DROPLET: ${{ secrets.SRS_PACKAGER_DROPLET }}
        run: |
          echo "Release ossrs/droplet:$SRS_TAG"
          docker buildx build --platform linux/arm/v7,linux/arm64/v8,linux/amd64 \
            --output "type=image,push=true" \
            -t ossrs/droplet:$SRS_TAG --build-arg SRS_AUTO_PACKAGER=$PACKAGER_DROPLET -f trunk/Dockerfile .

      # Docker alias images
      - name: Docker alias images for ossrs/droplet
        uses: akhilerm/tag-push-action@v2.0.0
        with:
          src: ossrs/droplet:${{ env.SRS_TAG }}
          dst: |
            ossrs/droplet:${{ env.SRS_VERSION }}
            ossrs/droplet:${{ env.SRS_MAJOR }}
            ossrs/droplet:v${{ env.SRS_MAJOR }}
            ossrs/droplet:latest

      # Aliyun ACR
      - name: Login aliyun hub
        uses: docker/login-action@v1
        with:
          registry: registry.cn-hangzhou.aliyuncs.com
          username: "${{ secrets.ACR_USERNAME }}"
          password: "${{ secrets.ACR_PASSWORD }}"
      - name: Push to Aliyun registry for ossrs/droplet
        uses: akhilerm/tag-push-action@v2.0.0
        with:
          src: ossrs/droplet:${{ env.SRS_TAG }}
          dst: |
            registry.cn-hangzhou.aliyuncs.com/ossrs/droplet:${{ env.SRS_TAG }}
            registry.cn-hangzhou.aliyuncs.com/ossrs/droplet:${{ env.SRS_VERSION }}
            registry.cn-hangzhou.aliyuncs.com/ossrs/droplet:${{ env.SRS_MAJOR }}
            registry.cn-hangzhou.aliyuncs.com/ossrs/droplet:v${{ env.SRS_MAJOR }}
            registry.cn-hangzhou.aliyuncs.com/ossrs/droplet:latest

  docker-lighthouse:
    name: docker-lighthouse
    runs-on: ubuntu-20.04
    needs:
      - test

    steps:
      ##################################################################################################################
      ##################################################################################################################
      ##################################################################################################################
      # Git checkout
      - name: Checkout repository
        uses: actions/checkout@v2

      # See https://github.com/crazy-max/ghaction-docker-buildx#moved-to-docker-organization
      # https://github.com/docker/setup-qemu-action
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v1
      # https://github.com/docker/setup-buildx-action
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1

      # The github.ref is, for example, refs/tags/v4.0.145 or refs/tags/v4.0-r8
      # Generate variables like:
      #   SRS_TAG=v4.0-r8
      #   SRS_TAG=v4.0.145
      #   SRS_VERSION=4.0.145
      #   SRS_VERSION=4.0-r8
      #   SRS_MAJOR=4
      # @see https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-environment-variable
      - name: Generate varaiables
        run: |
          SRS_TAG=$(echo ${{ github.ref }}| awk -F '/' '{print $3}')
          echo "SRS_TAG=$SRS_TAG" >> $GITHUB_ENV
          SRS_VERSION=$(echo ${SRS_TAG}| sed 's/^v//g')
          echo "SRS_VERSION=$SRS_VERSION" >> $GITHUB_ENV
          SRS_MAJOR=$(echo $SRS_TAG| cut -c 2)
          echo "SRS_MAJOR=$SRS_MAJOR" >> $GITHUB_ENV

      ##################################################################################################################
      ##################################################################################################################
      ##################################################################################################################
      # Create main images for Docker
      - name: Login to docker hub
        uses: docker/login-action@v1
        with:
          username: "${{ secrets.DOCKER_USERNAME }}"
          password: "${{ secrets.DOCKER_PASSWORD }}"
      - name: Build and push images to Docker hub
        env:
          PACKAGER_LIGHTHOUSE: ${{ secrets.SRS_PACKAGER_LIGHTHOUSE }}
        run: |
          echo "Release ossrs/lighthouse:$SRS_TAG"
          docker buildx build --platform linux/arm/v7,linux/arm64/v8,linux/amd64 \
            --output "type=image,push=true" \
            -t ossrs/lighthouse:$SRS_TAG --build-arg SRS_AUTO_PACKAGER=$PACKAGER_LIGHTHOUSE -f trunk/Dockerfile .

      # Docker alias images
      - name: Docker alias images for ossrs/lighthouse
        uses: akhilerm/tag-push-action@v2.0.0
        with:
          src: ossrs/lighthouse:${{ env.SRS_TAG }}
          dst: |
            ossrs/lighthouse:${{ env.SRS_VERSION }}
            ossrs/lighthouse:${{ env.SRS_MAJOR }}
            ossrs/lighthouse:v${{ env.SRS_MAJOR }}
            ossrs/lighthouse:latest

      # Aliyun ACR
      - name: Login aliyun hub
        uses: docker/login-action@v1
        with:
          registry: registry.cn-hangzhou.aliyuncs.com
          username: "${{ secrets.ACR_USERNAME }}"
          password: "${{ secrets.ACR_PASSWORD }}"
      - name: Push to Aliyun registry for ossrs/lighthouse
        uses: akhilerm/tag-push-action@v2.0.0
        with:
          src: ossrs/lighthouse:${{ env.SRS_TAG }}
          dst: |
            registry.cn-hangzhou.aliyuncs.com/ossrs/lighthouse:${{ env.SRS_TAG }}
            registry.cn-hangzhou.aliyuncs.com/ossrs/lighthouse:${{ env.SRS_VERSION }}
            registry.cn-hangzhou.aliyuncs.com/ossrs/lighthouse:${{ env.SRS_MAJOR }}
            registry.cn-hangzhou.aliyuncs.com/ossrs/lighthouse:v${{ env.SRS_MAJOR }}
            registry.cn-hangzhou.aliyuncs.com/ossrs/lighthouse:latest

  update:
    name: update
    runs-on: ubuntu-20.04
    needs:
      - test
      - docker-srs
      - docker-lighthouse
      - docker-droplet

    steps:
      ##################################################################################################################
      ##################################################################################################################
      ##################################################################################################################
      # Git checkout
      - name: Checkout repository
        uses: actions/checkout@v2

      # The github.ref is, for example, refs/tags/v4.0.145 or refs/tags/v4.0-r8
      # Generate variables like:
      #   SRS_TAG=v4.0-r8
      #   SRS_TAG=v4.0.145
      #   SRS_VERSION=4.0.145
      #   SRS_VERSION=4.0-r8
      #   SRS_MAJOR=4
      # @see https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-environment-variable
      - name: Generate varaiables
        run: |
          SRS_TAG=$(echo ${{ github.ref }}| awk -F '/' '{print $3}')
          echo "SRS_TAG=$SRS_TAG" >> $GITHUB_ENV
          SRS_VERSION=$(echo ${SRS_TAG}| sed 's/^v//g')
          echo "SRS_VERSION=$SRS_VERSION" >> $GITHUB_ENV
          SRS_MAJOR=$(echo $SRS_TAG| cut -c 2)
          echo "SRS_MAJOR=$SRS_MAJOR" >> $GITHUB_ENV

      ##################################################################################################################
      ##################################################################################################################
      ##################################################################################################################
      # Generate variables like:
      #   SRS_LH_OSSRS_NET=1.2.3.4
      - name: Build variables for lh.ossrs.net
        run: |
          SRS_LH_OSSRS_NET=$(dig +short lh.ossrs.net)
          SRS_R_OSSRS_NET=$(dig +short r.ossrs.net)
          echo "SRS_LH_OSSRS_NET=$SRS_LH_OSSRS_NET" >> $GITHUB_ENV
          echo "SRS_R_OSSRS_NET=$SRS_R_OSSRS_NET" >> $GITHUB_ENV

      - name: Release to lh.ossrs.net
        uses: appleboy/ssh-action@master
        with:
          host: ${{ env.SRS_LH_OSSRS_NET }}
          username: root
          key: ${{ secrets.DIGITALOCEAN_SSHKEY }}
          port: 22
          envs: SRS_MAJOR
          timeout: 60s
          command_timeout: 30m
          script: |
            docker pull registry.cn-hangzhou.aliyuncs.com/ossrs/srs:$SRS_MAJOR
            docker rm -f srs-server
            #
            # Cleanup old docker images.
            for image in $(docker images |grep '<none>' |awk '{print $3}'); do
              docker rmi -f $image
              echo "Remove image $image, r0=$?"
            done

      - name: Release to r.ossrs.net
        uses: appleboy/ssh-action@master
        with:
          host: ${{ env.SRS_R_OSSRS_NET }}
          username: root
          key: ${{ secrets.DIGITALOCEAN_SSHKEY }}
          port: 22
          envs: SRS_MAJOR
          timeout: 60s
          command_timeout: 30m
          script: |
            docker pull ossrs/srs:$SRS_MAJOR
            docker rm -f srs-server
            #
            # Cleanup old docker images.
            for image in $(docker images |grep '<none>' |awk '{print $3}'); do
              docker rmi -f $image
              echo "Remove image $image, r0=$?"
            done

  release:
    name: release
    runs-on: ubuntu-20.04
    needs:
      - update

    steps:
      ##################################################################################################################
      ##################################################################################################################
      ##################################################################################################################
      # Git checkout
      - name: Checkout repository
        uses: actions/checkout@v2

      # The github.ref is, for example, refs/tags/v4.0.145 or refs/tags/v4.0-r8
      # Generate variables like:
      #   SRS_TAG=v4.0-r8
      #   SRS_TAG=v4.0.145
      #   SRS_VERSION=4.0.145
      #   SRS_VERSION=4.0-r8
      #   SRS_MAJOR=4
      # @see https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-environment-variable
      - name: Generate varaiables
        run: |
          SRS_TAG=$(echo ${{ github.ref }}| awk -F '/' '{print $3}')
          echo "SRS_TAG=$SRS_TAG" >> $GITHUB_ENV
          SRS_VERSION=$(echo ${SRS_TAG}| sed 's/^v//g')
          echo "SRS_VERSION=$SRS_VERSION" >> $GITHUB_ENV
          SRS_MAJOR=$(echo $SRS_TAG| cut -c 2)
          echo "SRS_MAJOR=$SRS_MAJOR" >> $GITHUB_ENV

      ##################################################################################################################
      ##################################################################################################################
      ##################################################################################################################
      # Create source tar for release
      # Generate variables like:
      #   SRS_SOURCE_TAR=srs-server-4.0.145.tar.gz
      #   SRS_SOURCE_MD5=83e38700a80a26e30b2df054e69956e5
      - name: Create source tar.gz
        run: |
          DEST_DIR=srs-server-$SRS_VERSION && mkdir -p $DEST_DIR &&
          cp README.md $DEST_DIR && cp LICENSE $DEST_DIR && cp -R trunk $DEST_DIR/trunk &&
          (cd $DEST_DIR/trunk/3rdparty && rm -rf *.zip openssl-*.gz srs-bench) &&
          tar zcf ${DEST_DIR}.tar.gz ${DEST_DIR} && du -sh ${DEST_DIR}* && rm -rf ${DEST_DIR} &&
          echo "SRS_SOURCE_TAR=${DEST_DIR}.tar.gz" >> $GITHUB_ENV &&
          echo "SRS_SOURCE_MD5=$(md5sum ${DEST_DIR}.tar.gz| awk '{print $1}')" >> $GITHUB_ENV

      # Create package tar for release
      # Generate variables like:
      #   SRS_PACKAGE_ZIP=SRS-CentOS7-x86_64-4.0.145.zip
      #   SRS_PACKAGE_MD5=3880a26e30b283edf05700a4e69956e5
      - name: Create package zip
        env:
          PACKAGER: ${{ secrets.SRS_PACKAGER_BINARY }}
        run: |
          docker build --tag srs:pkg --build-arg version=$SRS_VERSION --build-arg SRS_AUTO_PACKAGER=$PACKAGER -f trunk/Dockerfile.pkg . &&
          SRS_PACKAGE_ZIP=SRS-CentOS7-x86_64-$SRS_VERSION.zip &&
          docker run --rm -v $(pwd):/output srs:pkg cp objs/$SRS_PACKAGE_ZIP /output/ &&
          du -sh $SRS_PACKAGE_ZIP &&
          echo "SRS_PACKAGE_ZIP=$SRS_PACKAGE_ZIP" >> $GITHUB_ENV &&
          echo "SRS_PACKAGE_MD5=$(md5sum $SRS_PACKAGE_ZIP| awk '{print $1}')" >> $GITHUB_ENV

      # Create release.
      - name: Create release
        id: create_release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ github.ref }}
          release_name: Release ${{ github.ref }}
          body: |
            ${{ github.event.head_commit.message }}
            ## Resource
            * Source: ${{ env.SRS_SOURCE_MD5 }} [${{ env.SRS_SOURCE_TAR }}](https://github.com/ossrs/srs/releases/download/${{ env.SRS_TAG }}/${{ env.SRS_SOURCE_TAR }})
            * Binary: ${{ env.SRS_PACKAGE_MD5 }} [${{ env.SRS_PACKAGE_ZIP }}](https://github.com/ossrs/srs/releases/download/${{ env.SRS_TAG }}/${{ env.SRS_PACKAGE_ZIP }})
            ## Docker
            * China: [docker pull registry.cn-hangzhou.aliyuncs.com/ossrs/srs:latest](https://github.com/ossrs/srs/wiki/v4_CN_Home#docker)
            * China: [docker pull registry.cn-hangzhou.aliyuncs.com/ossrs/srs:${{ env.SRS_MAJOR }}](https://github.com/ossrs/srs/wiki/v4_CN_Home#docker)
            * China: [docker pull registry.cn-hangzhou.aliyuncs.com/ossrs/srs:${{ env.SRS_TAG }}](https://github.com/ossrs/srs/wiki/v4_CN_Home#docker)
            * Global: [docker pull ossrs/srs:latest](https://github.com/ossrs/srs/wiki/v4_EN_Home#docker)
            * Global: [docker pull ossrs/srs:${{ env.SRS_MAJOR }}](https://github.com/ossrs/srs/wiki/v4_EN_Home#docker)
            * Global: [docker pull ossrs/srs:${{ env.SRS_TAG }}](https://github.com/ossrs/srs/wiki/v4_EN_Home#docker)
            ## Doc
            * [快速入门](https://github.com/ossrs/srs/wiki/v4_CN_Home#getting-started) or [Getting Started](https://github.com/ossrs/srs/wiki/v4_EN_Home#getting-started)
            * [中文Wiki首页](https://github.com/ossrs/srs/wiki/v4_CN_Home) or [Wiki home](https://github.com/ossrs/srs/wiki/v4_EN_Home)
            * [FAQ](https://github.com/ossrs/srs/issues/2716), [Features](https://github.com/ossrs/srs/blob/4.0release/trunk/doc/Features.md#features) or [ChangeLogs](https://github.com/ossrs/srs/blob/4.0release/trunk/doc/CHANGELOG.md#changelog)
          draft: false
          prerelease: false

      # Upload release source files
      - name: Upload Release Assets Source
        id: upload-release-assets-source
        uses: dwenegar/upload-release-assets@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          release_id: ${{ steps.create_release.outputs.id }}
          assets_path: ${{ env.SRS_SOURCE_TAR }}

      # Upload release package files
      - name: Upload Release Assets Package
        id: upload-release-assets-package
        uses: dwenegar/upload-release-assets@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          release_id: ${{ steps.create_release.outputs.id }}
          assets_path: ${{ env.SRS_PACKAGE_ZIP }}

  done:
    name: done
    runs-on: ubuntu-20.04
    needs:
      - release

    steps:
      - run: echo 'All done'

