name: basket-api

on:
  push:
    branches:
    - master
    - dev
    - feature/github-actions
    paths:
    - src/BuildingBlocks/*
    - src/Services/Basket/*
    - .github/workflows/basket-api.yml
  
  pull_request:
    branches:
    - master
    - dev
env:
  REGISTRY_ENDPOINT: borjasanes
  REGISTRY_HOST: registry.hub.docker.com
  SERVICES: basket-api

jobs:
  BuildContainersForPR_Linux:
    runs-on: ubuntu-16.04
    if: ${{ github.event_name == 'pull_request' }}
    steps:
    - name: Compose build ${{ env.SERVICES }}
      run: docker-compose build -f src/docker-compose.yml ${{ env.SERVICES }}
      shell: bash
      env:
        TAG: ${{ env.branch }}
        REGISTRY: ${{ env.REGISTRY_ENDPOINT }}
  BuildContainersForPR_Windows:
    runs-on: windows-2019
    if:  ${{ false }}
    # if: ${{ github.event_name == 'pull_request' }}
    steps:
    - name: Compose build ${{ env.SERVICES  }}
      run: docker-compose build -f src/docker-compose.yml ${{ env.SERVICES }}
      shell: pwsh
      env:
        TAG: ${{ env.branch }}
        REGISTRY: ${{ env.REGISTRY_ENDPOINT }}
        PLATFORM: win
        NODE_IMAGE: stefanscherer/node-windows:10
  BuildLinux:
    runs-on: ubuntu-latest
    if: ${{ github.event_name != 'pull_request' }}
    steps:
    - name: 'Checkout Github Action'
      uses: actions/checkout@master

    - name: Login to Container Registry
      uses: docker/login-action@v1
      with:
        registry: ${{ env.REGISTRY_HOST }}
        username: ${{ secrets.USERNAME }}
        password: ${{ secrets.PASSWORD }}

    - name: Set branch name as env variable
      run: |
        currentbranch=$(echo ${GITHUB_REF##*/})
        echo "running on $currentbranch"
        echo "BRANCH=$currentbranch" >> $GITHUB_ENV
      shell: bash

    - name: Compose build ${{ env.SERVICES }}
      run: sudo -E docker-compose build ${{ env.SERVICES }}
      working-directory: ./src
      shell: bash
      env:
        TAG: ${{ env.BRANCH }}
        REGISTRY: ${{ env.REGISTRY_ENDPOINT }}

    - name: Compose push ${{ env.SERVICES }}
      run: sudo -E docker-compose push ${{ env.SERVICES }}
      working-directory: ./src
      shell: bash
      env:
        TAG: ${{ env.BRANCH }}
        REGISTRY: ${{ env.REGISTRY_ENDPOINT }}

  BuildWindows:
    runs-on: windows-2019
    if:  ${{ false }}
    # if: ${{ github.event_name != 'pull_request' }}
    steps:
    - name: Compose build ${{ env.SERVICES }}
      run: docker-compose build -f src/docker-compose.yml ${{ env.SERVICES }}
      shell: pwsh
      env:
        TAG: ${{ env.BRANCH }}
        REGISTRY: ${{ env.REGISTRY_ENDPOINT }}
    - name: Compose push ${{ env.SERVICES }}
      run: docker-compose push -f src/docker-compose.yml ${{ env.SERVICES }}'
      shell: pwsh
      env:
        TAG: ${{ env.BRANCH }}
        REGISTRY: ${{ env.REGISTRY_ENDPOINT }}