name: build and deploy portfolio

on:
  push:
    branches: [ "master" ]
    paths-ignore: ['Writerside/**']
  pull_request:
    branches: [ "master" ]
    paths-ignore: ['Writerside/**']

jobs:

  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3
    - name: 'Create env file'
      run: |
        touch .env
        echo BOT_TOKEN=${{ secrets.KLEINCORD_TOKEN }} >> .env
        echo ERROR_CHANNEL_ID=${{ secrets.ERROR_CHANNEL_ID }} >> .env
        echo LOG_CHANNEL_ID=${{ secrets.LOG_CHANNEL_ID }} >> .env
        echo ENVIRONMENT=Production >> .env
    - name: Docker login
      run: docker login gitea.kleinsense.nl -p ${{secrets.docker_password}} -u ${{secrets.docker_username}}
    - name: Build the Docker image
      run: docker build . --file Dockerfile --tag gitea.kleinsense.nl/dariusklein/kleincord:latest
    - name: Docker push
      run: docker push gitea.kleinsense.nl/dariusklein/kleincord:latest


  publish:
  
    needs: build
    
    runs-on: ubuntu-latest
    
    steps:
    - name: Docker stop
      run: docker stop darius-discord-bot  || 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/kleincord
    - name: Docker run
      run: docker run --restart=always -d --name darius-discord-bot gitea.kleinsense.nl/dariusklein/kleincord
