name: heroku-deploy-stop-start-destroy

on:
  workflow_dispatch:
    inputs:
      actions:
        description: "action: deploy/stop/start/destroy"
        default: "deploy"
        required: true
      heroku-region:
        description: "us 或者 eu. 如果你的app已经创建，切换到不同的region，需要先输入 actions 为 destroy 删除app"
        default: "us"
        required: false
      heroku-app-name:
        description: "可选，如果输入，则会覆盖 Secrets 里面配置的 APP_NAME。"
        default: ""
        required: false
      env:
        description: "environment: Input the name of Environment. If left blank, the main secrets setting will be used by default."
        default: ""
        required: false

jobs:
  jobenv:
    runs-on: ubuntu-latest
    environment: ${{ github.event.inputs.env }}
    env: # Or as an environment variable
      APP_NAME: ${{ secrets.APP_NAME }}
    outputs:
      actions: ${{ github.event.inputs.actions || 'deploy'}}
      test111: ${{ github.event.inputs.env }} # todo
    steps:
      - id: isDeployHeroku # TODO check atcion doc for usage
        # if: ${{ github.event.inputs.actions  == 'start'}}
        run: echo "test output"

  output-jobenv:
    runs-on: ubuntu-latest
    needs: jobenv
    env:
      APP_NAME: ${{ secrets.APP_NAME }}
      HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }}
      EMAIL: ${{ secrets.EMAIL }}
      HEROKU_V2RAY_UUID: ${{ secrets.HEROKU_V2RAY_UUID }}
      HEROKU_TUNNEL_TOKEN: ${{ secrets.HEROKU_TUNNEL_TOKEN }}
    steps:
      - run: |
          echo "actions: ${{ github.event.inputs.actions }}"
          echo "heroku-region: ${{ github.event.inputs.heroku-region }}"
          echo "heroku-app-name: ${{ github.event.inputs.heroku-app-name }}"
          echo "env: ${{ github.event.inputs.env }}"
      - id: APP_NAME_is_empty
        if: ${{ env.APP_NAME == ''}}
        run: echo APP_NAME is empty
      - id: HEROKU_API_KEY_is_empty
        if: ${{ env.HEROKU_API_KEY == ''}}
        run: echo HEROKU_API_KEY is empty
      - id: EMAIL_is_empty
        if: ${{ env.EMAIL == ''}}
        run: echo EMAIL is empty
      - id: HEROKU_V2RAY_UUID_is_empty
        if: ${{ env.HEROKU_V2RAY_UUID == ''}}
        run: echo HEROKU_V2RAY_UUID is empty
      - id: HEROKU_TUNNEL_TOKEN_is_empty
        if: ${{ env.HEROKU_TUNNEL_TOKEN == ''}}
        run: echo HEROKU_TUNNEL_TOKEN is empty
      # - run: echo ${{needs.jobenv.outputs.herokuAppName}} | sed 's/./& /g'


  heroku-deploy:
    needs: jobenv
    if: ${{ needs.jobenv.outputs.actions == 'deploy' || needs.jobenv.outputs.actions == ''}}
    runs-on: ubuntu-latest
    environment: ${{ github.event.inputs.env }}
    env: # Or as an environment variable
      APP_NAME: ${{ github.event.inputs.heroku-app-name || secrets.APP_NAME }}
    steps:
      - uses: actions/checkout@v2
      - uses: akhileshns/heroku-deploy@v3.12.12 # This is the action
        with:
          heroku_api_key: ${{secrets.HEROKU_API_KEY}}
          heroku_app_name: ${{ env.APP_NAME }} #Must be unique in Heroku
          heroku_email: ${{secrets.EMAIL}}
          usedocker: true
          region: ${{github.event.inputs.heroku-region || 'us'}}
          # docker_build_args: |
          #   HD_UUID
        env:
          # 这是为了heroku，注意前缀有个HD，这是为了告诉heroku-deploy，这些变量需要传入到 container 中。
          HD_UUID: ${{ secrets.HEROKU_V2RAY_UUID }} # UUID for v2ray user, 为了安全，一定要放入 github action token 中
          HD_TUNNEL_TOKEN: ${{ secrets.HEROKU_TUNNEL_TOKEN }} # token for cloudflared tunnel
          NODE_ENV: production
          SECRET_KEY: ${{ secrets.MY_SECRET_KEY }}
  stop-start-destroy:
    needs: jobenv
    if: ${{ needs.jobenv.outputs.actions  == 'start' || needs.jobenv.outputs.actions  == 'stop' || needs.jobenv.outputs.actions  == 'destroy' }}
    runs-on: ubuntu-latest
    environment: ${{ github.event.inputs.env }}
    env: # Or as an environment variable
      APP_NAME: ${{ github.event.inputs.heroku-app-name || secrets.APP_NAME }}
    steps:
      - uses: actions/checkout@v2
      - uses: akhileshns/heroku-deploy@v3.7.8 # This is the action
        with:
          heroku_api_key: ${{secrets.HEROKU_API_KEY}}
          heroku_email: ${{secrets.EMAIL}}
          justlogin: true
      - run: |
          echo action is ${{ needs.jobenv.outputs.actions }}
          if [[ ${{ needs.jobenv.outputs.actions }} == 'stop' ]]; then heroku ps:scale web=0 -a ${{env.APP_NAME}} && echo "stop"; fi
          if [[ ${{ needs.jobenv.outputs.actions }} == 'start' ]]; then heroku ps:scale web=1 -a ${{env.APP_NAME}} && echo "start"; fi
          heroku ps -a ${{env.APP_NAME}}
          if [[ ${{ needs.jobenv.outputs.actions }} == 'destroy' ]]; then heroku apps:destroy -a ${{env.APP_NAME}} --confirm ${{env.APP_NAME}} && echo "destroy app"; fi
