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'
      - 'docker-compose.yml'

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

    env:
      COUCHDB_USER: ${{secrets.COUCHDB_USER}}
      COUCHDB_PASSWORD: ${{secrets.COUCHDB_PASSWORD}}

    steps:
      - uses: actions/checkout@v4

      - name: build docker image
        run: COUCHDB_TAG=commit-${{env.GITHUB_SHA}} docker compose -p hereconnect build couchdb

      - name: start container
        run: COUCHDB_TAG=commit-${{env.GITHUB_SHA}} docker compose -p hereconnect up -d --pull never couchdb

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

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

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