name: Release Server

on:
  release:
    types:
      - created

jobs:
  godot:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        platform: [linux]
    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: Build
        id: build
        uses: josephbmanley/build-godot-action@v1.4.0
        with:
          name: defend-together
          preset: ${{ matrix.platform }}
          projectDir: client
          debugMode: 'true'
          package: 'true'
      - name: Push to Itch
        uses: josephbmanley/butler-publish-itchio-action@v1.0.1
        env:
          BUTLER_CREDENTIALS: ${{ secrets.BUTLER_CREDENTIALS }}
          CHANNEL: ${{ matrix.platform }}
          ITCH_GAME: example-project
          ITCH_USER: username
          PACKAGE: ${{ github.workspace }}/${{ steps.build.outputs.build }}
  docker:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v1
      - id: get_tag
        name: Get Tag
        env:
          GITHUB_HEAD_REF: $${{ github.head_ref }}
          GITHUB_BASE_REF: ${{ github.base_ref }}
        run: |
          TAG=$(jq --raw-output '.release.tag_name' $GITHUB_EVENT_PATH)
          echo ::set-output name=TAG::$TAG
      - 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: ${{ steps.get_tag.outputs.TAG }}
          LATEST: "false"
      - name: Push Latest 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: ${{ steps.get_tag.outputs.TAG }}
          LATEST: "true"
      - name: Push Tag to GitHub Package
        uses: opspresso/action-docker@master
        with:
          args: --docker
        env:
          USERNAME: ${{ github.actor }}
          PASSWORD: ${{ secrets.GITHUB_TOKEN }}
          REGISTRY: "docker.pkg.github.com"
          BUILD_PATH: "server"
          DOCKERFILE: "server/Dockerfile"
          IMAGE_NAME: "defend-together"
          TAG_NAME: ${{ steps.get_tag.outputs.TAG }}
          LATEST: "false"
      - name: Push Latest to GitHub Package
        uses: opspresso/action-docker@master
        with:
          args: --docker
        env:
          USERNAME: ${{ github.actor }}
          PASSWORD: ${{ secrets.GITHUB_TOKEN }}
          REGISTRY: "docker.pkg.github.com"
          BUILD_PATH: "server"
          DOCKERFILE: "server/Dockerfile"
          IMAGE_NAME: "defend-together"
          TAG_NAME: ${{ steps.get_tag.outputs.TAG }}
          LATEST: "true"
  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-1"
        DEST_DIR: dt/production/cloudformation
        AWS_S3_BUCKET: sumu-stacks
        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-1
    - name: Deploy to AWS CloudFormation
      uses: aws-actions/aws-cloudformation-github-deploy@v1
      with:
        name: dt-infrastructure
        template: infrastructure/cloudformation/dt/top.yaml
        capabilities: "CAPABILITY_NAMED_IAM,CAPABILITY_IAM"
        parameter-overrides: VpcId=${{ secrets.VPC_ID }},Domain=${{ secrets.DOMAIN }},PublicSubnets=${{ secrets.SUBNET_IDS }}