name: Deploy
run-name: ${{ gitea.actor }} build image and push to container registry

on:
  push:
    branches:
      - 'staging'

jobs:
  CreateImage:
    runs-on: [squizstaging]
    uses: http://gitea.pena/PenaDevops/actions.git/.gitea/workflows/build-image.yml@v1.1.6-p
    with:
      runner: squizstaging
    secrets:
      REGISTRY_USER: ${{ secrets.REGISTRY_USER }}
      REGISTRY_PASSWORD: ${{ secrets.REGISTRY_PASSWORD }}
  ValidateConfig:
    runs-on: [squizstaging]
    uses: https://gitea.pena/PenaDevops/actions.git/.gitea/workflows/validate_config.yml@v1.2.1
    needs: CreateImage
    with:
      runner: hubstaging
    secrets:
      REGISTRY_USER: ${{ secrets.REGISTRY_USER }}
      REGISTRY_PASSWORD: ${{ secrets.REGISTRY_PASSWORD }}
  MigrateDatabase:
    runs-on: [squizstaging]
    needs: ValidateConfig
    container:
      image: gitea.pena/penadevops/container-images/node-golint:main
    steps:
      - name: Check out repository code
        uses: https://gitea.pena/PenaDevops/actions.git/checkout@v1
      - name: download packs
        run: |
          go mod download
          go list -f '{{.Dir}}' -m gitea.pena/SQuiz/common
          ls $(go list -f '{{.Dir}}' -m gitea.pena/SQuiz/common)
          grep -E '^POSTGRES_URL=' "./deployments/${{ github.ref_name }}/config.env" | cut -d '=' -f2- | sed -E 's~host=([^ ]+) port=([^ ]+) user=([^ ]+) password=([^ ]+) dbname=([^ ]+) sslmode=([^ ]+)~postgresql://\3:\4@\1:\2/\5?sslmode=\6~'  | sed 's~\"~~g'
      - name: migrate
        run: ./tools/migrate -verbose -source "file:/$(go list -f '{{.Dir}}' -m gitea.pena/SQuiz/common | sed 's|^/workspace/SQuiz/worker||')/dal/schema/" -database "$(grep -E '^POSTGRES_URL=' "./deployments/${{ github.ref_name }}/config.env" | cut -d '=' -f2- | sed -E 's~host=([^ ]+) port=([^ ]+) user=([^ ]+) password=([^ ]+) dbname=([^ ]+) sslmode=([^ ]+)~postgresql://\3:\4@\1:\2/\5?sslmode=\6~'  | sed 's~\"~~g')" up
  DeployService:
    runs-on: [squizstaging]
    needs: MigrateDatabase
    uses: http://gitea.pena/PenaDevops/actions.git/.gitea/workflows/deploy.yml@v1.1.4-p7
    with:
      runner: squizstaging
      actionid: ${{ gitea.run_id }}
    

