name: build and deploy portfolio

on:
  push:
    branches: [ "master" ]
  pull_request:
    branches: [ "master" ]

jobs:

  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3
    - name: 'Create env file'
      run: |
        touch .env
        echo POSTGRES_HOST=${{ secrets.POSTGRES_HOST }} >> .env
        echo POSTGRES_PORT=${{ secrets.POSTGRES_PORT }} >> .env
        echo POSTGRES_USER=${{ secrets.POSTGRES_USER }} >> .env
        echo POSTGRES_DB=${{ secrets.POSTGRES_DB }} >> .env
        echo POSTGRES_PASSWORD=${{ secrets.POSTGRES_PASSWORD }} >> .env
        echo SSL_MODE=${{ secrets.SSL_MODE }} >> .env
    - name: Build the Docker image
      run: docker compose build
    - name: Docker push
      run: |
        docker push docker.dariusklein.nl/portfolio:latest
        docker push docker.dariusklein.nl/portfolio-docs:latest

  publish:
  
    needs: build
    
    runs-on: self-hosted
    
    steps:
    - uses: actions/checkout@v3
      with:
        sparse-checkout: |
          docker-compose.yml
    - name: copy file
      run: mv docker-compose.yml /home/darius/portfolio
    - name: Docker login
      run: docker login docker.dariusklein.nl -u Darius -p ${{ secrets.DOCKER_PASSWORD }}
    - name: Docker pull
      run: |
        cd /home/darius/portfolio
        docker compose pull
    - name: Docker run
      run: |
        cd /home/darius/portfolio
        docker compose up -d
