name: Build Stage

on:
  push:
    branches:
      - master

jobs:
  godot:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        platform: [linux, win32, win64]
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Download Export Template
        env:
          PLATFORM: ${{ matrix.platform }}
        run: |
          mkdir -p client/templates
          wget http://godot-enet.cloudsumu.com.s3-website-us-east-1.amazonaws.com/export-templates/debug/$PLATFORM -O client/templates/$PLATFORM
      - name: Configure
        env:
          PLATFORM: ${{ matrix.platform }}
        run: |
          if [ "$PLATFORM" = "win32" ] || [ "$PLATFORM" = "win64" ] ; then
            echo "::set-env name=EXPORT_SUFFIX::.exe"
          fi
      - name: Build
        id: build
        uses: josephbmanley/build-godot-action@v1.4.0
        with:
          name: defend-together${{ env.EXPORT_SUFFIX }}
          preset: ${{ matrix.platform }}
          projectDir: client
          debugMode: 'true'
      - name: Upload Artifact
        uses: actions/upload-artifact@v2
        with:
          name: Client - ${{ matrix.platform }}
          path: ${{ github.workspace }}/${{ steps.build.outputs.build }}
  docker:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v1
      - name: Push Tag to Docker Hub
        uses: opspresso/action-docker@master
        with:
          args: --docker
        env:
          USERNAME: ${{ secrets.DOCKER_USERNAME }}
          PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
          BUILD_PATH: "server"
          DOCKERFILE: "server/Dockerfile"
          IMAGE_NAME: "josephbmanley/defend-together"
          TAG_NAME: "stage"
          LATEST: "false"
  cloudformation:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout Repo
      uses: actions/checkout@v2.1.0
      with:
        submodules: 'true'
    - name: Ship to S3
      uses: jakejarvis/s3-sync-action@master
      with:
        args: --follow-symlinks --delete
      env:
        SOURCE_DIR: infrastructure/cloudformation
        AWS_REGION: "us-east-2"
        DEST_DIR: stage/cloudformation
        AWS_S3_BUCKET: dt-deployment-bucket
        AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
        AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
    - name: Configure AWS Credentials
      uses: aws-actions/configure-aws-credentials@v1
      with:
        aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
        aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        aws-region: us-east-2
    - name: Deploy to AWS CloudFormation
      uses: aws-actions/aws-cloudformation-github-deploy@v1
      with:
        name: dt-infrastructure-stage
        template: infrastructure/cloudformation/dt/top.yaml
        capabilities: "CAPABILITY_NAMED_IAM,CAPABILITY_IAM"
        parameter-overrides: VpcId=${{ secrets.VPC_ID }},SubDomain=stage,Domain=${{ secrets.DOMAIN }},environment=stage,DockerTag=stage,release=stage,PublicSubnets=${{ secrets.SUBNET_IDS }}