name: Manual Deploy

on:
  workflow_dispatch:
    inputs:
      network:
        default: 'Mainnet'
      checkpoint_sync:
        default: true

env:
  PROJECT_ID: zealous-zebra

jobs:

  deploy:
    name: Deploy one zebrad node
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2.3.4
      with:
        persist-credentials: false
        
    - name: Set project and image names
      run: |
        BRANCH_NAME=$(expr $GITHUB_REF : '.*/\(.*\)') && \
        BRANCH_NAME=${BRANCH_NAME,,} && \
        REPOSITORY=${GITHUB_REPOSITORY,,} && \
        echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV && \
        echo "SHORT_SHA=$(git rev-parse --short=7 $GITHUB_SHA)" >> $GITHUB_ENV && \
        echo "REPOSITORY=$REPOSITORY" >> $GITHUB_ENV

    - name: Set up gcloud
      uses: GoogleCloudPlatform/github-actions/setup-gcloud@master
      with:
        version: '295.0.0'
        project_id: ${{ env.PROJECT_ID }}
        service_account_key: ${{ secrets.GCLOUD_AUTH }}

    # Build and push image to Google Container Registry
    - name: Build
      # Tagging w/ the commit SHA blocks the :latest tag on GCR
      run: |
        gcloud builds submit \
          --config cloudbuild.yaml \
          --substitutions SHORT_SHA="$SHORT_SHA",BRANCH_NAME="$BRANCH_NAME",_CHECKPOINT_SYNC="${{ github.event.inputs.checkpoint_sync }}",_NETWORK="${{ github.event.inputs.network }}"

    # Run once: create firewall rule to allow incoming traffic to the node
    # - name: Create Zcash incoming traffic firewall rule
    #   run: |
    #     gcloud compute firewall-rules create "allow-zcash" \
    #     --target-tags zebrad \
    #     --allow tcp:8233,tcp:18233 \
    #     --source-ranges 0.0.0.0/0 \
    #     --description="Allow incoming Zcash traffic from anywhere" \

    # Creates Compute Engine virtual machine instance w/ zebrad container and disks
    - name: Create instance running zebrad container image
      run: |
        gcloud compute instances create-with-container "zebrad-$BRANCH_NAME-$SHORT_SHA" \
        --container-image "gcr.io/$PROJECT_ID/$REPOSITORY/$BRANCH_NAME:$SHORT_SHA" \
        --container-mount-disk mount-path='/zebrad-cache',name=zebrad-cache-$SHORT_SHA \
        --create-disk name=zebrad-cache-$SHORT_SHA,auto-delete=yes,size=100GB,type=pd-balanced \
        --machine-type n2-standard-4 \
        --service-account cos-vm@zealous-zebra.iam.gserviceaccount.com \
        --tags zebrad \
        --zone us-central1-a
