name: Deploy service couchdb
run-name: ${{ github.actor }} deployed to build branch
on:
  push:
    branches:
      - main
    paths:
      - 'services/couchdb/**'
      - '.gitea/workflows/deploy-service-couchdb.yaml'

jobs:
  service couchdb:
    runs-on: ubuntu-latest

    container:
      volumes:
        - /www/hereconnect.condev.ru:/www/hereconnect.condev.ru

    steps:
      - uses: actions/checkout@v4

      - name: build docker image
        run: |
          docker build  --file services/couchdb/Dockerfile -t hereconnect/service-couchdb:${{env.GITHUB_SHA}} services/couchdb

      - name: start container
        env:
          COUCHDB_TAG: ${{env:GITHUB_SHA}}
          COUCHDB_USER: ${{secrets.COUCHDB_USER}}
          COUCHDB_PASSWORD: ${{secrets.COUCHDB_PASSWORD}}
        run: docker compose -p hereconnect up --pull never

      - name: rename to latest
        run: |
          export
          docker tag hereconnect/service-couchdb:${{env.GITHUB_SHA}} hereconnect/service-couchdb:latest

      - name: rollback on Failure
        if: failure()
        env:
          COUCHDB_TAG: latest
        run: docker compose -p hereconnect up --pull never

      - name: cleanup docker image
        if: always()
        run: docker rmi hereconnect/service-couchdb:${{env.GITHUB_SHA}}
