name: Master github actions

on:
  push:
    branches:
      - "master"

permissions:
  contents: write

jobs:
  build-release:
    runs-on: ubuntu-latest
    steps:
      - name: Get current date
        run: echo "irgsh_build_date=$(TZ='Asia/Jakarta' date +'%Y%m%d%H%M')" >> $GITHUB_ENV
      - uses: actions/checkout@v4
      - name: Install needed apt packages
        uses: awalsh128/cache-apt-pkgs-action@v1.6.0
        with:
          packages: gpg pbuilder debootstrap devscripts python3-apt reprepro make
          version: 1.0
      - uses: actions/setup-go@v4
        with:
          go-version: "1.13.14"
      - uses: actions/cache@v4
        with:
          path: |
            ~/.cache/go-build
            ~/go/pkg/mod
          key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
          restore-keys: |
            ${{ runner.os }}-go-
      - name: Build nightly release
        run: |
          echo ${{ steps.date.outputs.date }}-nightly-build > VERSION
          make release
      - uses: actions/upload-artifact@v4
        with:
          name: release.tar.gz
          path: target/
    outputs:
      irgsh_build_date: ${{ env.irgsh_build_date }}

  release:
    runs-on: ubuntu-latest
    needs: [build-release]
    steps:
      - uses: actions/download-artifact@v4
        with:
          name: release.tar.gz
      - name: Extract release
        run: |
          tar xvzf release.tar.gz
          cp $(find . -type f -name "irgsh-cli") .
      - name: Create github release
        uses: softprops/action-gh-release@v2.5.0
        with:
          name: ${{ needs.build-release.outputs.irgsh_build_date }} Nightly Release
          body: Nightly release
          draft: false
          prerelease: false
          tag_name: ${{ needs.build-release.outputs.irgsh_build_date }}-nightly-build
          files: |
            release.tar.gz
            irgsh-cli

  # deploy:
  #   runs-on: ubuntu-latest
  #   needs: [release]
  #   steps:
  #     - name: Deploy to irgsh server
  #       uses: kudaliar032/tendang-action@v1
  #       with:
  #         url: ${{ secrets.RANI_TENDANG_URL }}
  #         token: ${{ secrets.RANI_DEPLOYMENT_KEY }}
  #         name: ${{ secrets.RANI_DEPLOYMENT_NAME }}
