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 JWT_SECRET=${{secrets.JWT_SECRET}} >> .env
          echo POSTGRES_HOST=192.168.1.200 >> .env
          echo POSTGRES_PORT=5099 >> .env
          echo POSTGRES_USER=postgres >> .env
          echo POSTGRES_DB=portfolio >> .env
          echo POSTGRES_PASSWORD=${{secrets.POSTGRES_PASSWORD}} >> .env
          echo SSL_MODE=disable >> .env
          echo ENVIRONMENT=docker >> .env
      - name: Build the Docker image
        run: docker compose build
      - name: Docker login
        run: docker login gitea.kleinsense.nl -p ${{secrets.docker_password}} -u ${{secrets.docker_username}}
      - name: Docker push
        run: docker push gitea.kleinsense.nl/dariusklein/portfolio:latest
      - name: Docker push
        run: docker push gitea.kleinsense.nl/dariusklein/portfolio-docs:latest

  publish-docs:

    needs: build

    runs-on: ubuntu-latest

    steps:
      - name: Docker stop
        run: docker stop darius-portfolio-docs || true
      - name: Docker remove
        run: docker rm darius-portfolio-docs || true
      - name: Docker login
        run: docker login gitea.kleinsense.nl -p ${{secrets.docker_password}}  -u ${{secrets.docker_username}}
      - name: Docker pull
        run: docker pull gitea.kleinsense.nl/dariusklein/portfolio-docs:latest
      - name: Docker run
        run: docker run --restart=always -dit -p 4002:80 --name darius-portfolio-docs gitea.kleinsense.nl/dariusklein/portfolio-docs:latest

  publish-portfolio:

    needs: build

    runs-on: ubuntu-latest

    steps:
      - name: Docker stop
        run: docker stop darius-portfolio-server || true
      - name: Docker remove
        run: docker rm darius-portfolio-server || true
      - name: Docker login
        run: docker login gitea.kleinsense.nl -p ${{secrets.docker_password}}  -u ${{secrets.docker_username}}
      - name: Docker pull
        run: docker pull gitea.kleinsense.nl/dariusklein/portfolio:latest
      - name: Docker run
        run: docker run --restart=always -dit -p 4000:4000 -p 4001:4001 --name darius-portfolio-server gitea.kleinsense.nl/dariusklein/portfolio:latest
