name: Deploy on: push: branches: - develop tags: - '*' jobs: build-and-deploy: runs-on: ubuntu-latest steps: - name: Checkout repo uses: actions/checkout@v4 - name: Get branch name id: branch_name uses: ./.gitea/actions/getTagSafeRef - name: Determine image tag id: image_tag run: | if [[ ${{ github.ref }} == refs/tags/* ]]; then IMAGE_TAG=${{ github.ref_name }} ENVIRONMENT=production elif [[ ${{ github.ref }} == refs/heads/develop ]]; then IMAGE_TAG="develop" ENVIRONMENT=develop fi echo "tag=$IMAGE_TAG" >> $GITHUB_OUTPUT echo "env=$ENVIRONMENT" >> $GITHUB_OUTPUT echo "Image tag will be: $IMAGE_TAG" - name: Build and push image id: docker_build uses: ./.gitea/actions/build with: tag: ${{ steps.image_tag.outputs.tag }} REGISTRY_ACCESS_TOKEN: ${{ secrets.REGISTRY_ACCESS_TOKEN }} REGISTRY_USER: ${{ gitea.actor }} - name: Determine URL id: url run: | if [[ ${{ github.ref_type }} == "tag" ]]; then URL="phoenix.klemp.local" elif [[ ${{ github.ref_name }} == "develop" ]]; then URL="develop.phoenix.klemp.local" fi echo "url=$URL" >> $GITHUB_OUTPUT echo "URL will be: $URL" - name: Create environment file run: | cat > .env << EOF IMAGE_URL='${{ steps.docker_build.outputs.image_url }}' URL='${{ steps.url.outputs.url }}' REGISTRY=git.klemp.dev PROJECT_NAME=${{ steps.branch_name.outputs.branch_name }} COMPOSE_PROJECT_NAME=${{ steps.branch_name.outputs.branch_name }} TOLGEE_API_URL=${{ vars.TOLGEE_API_URL }} TOLGEE_API_KEY=${{ secrets.TOLGEE_API_KEY }} TRANSLATION_CACHE_TTL=${{ vars.TRANSLATION_CACHE_TTL }} EOF - name: Remove old Docker context run: 'docker context rm deploy_target 2>/dev/null || true' - name: Create Docker context run: 'docker context create deploy_target --docker "host=ssh://${{ vars.ssh_user }}@${{ vars.ssh_host }}:${{ vars.ssh_port }}"' - name: Validate Docker context run: 'docker --context deploy_target info' - name: Stop existing deployment run: | DOCKER_CONTEXT=deploy_target docker compose -f docker-compose.yml -p ${{ steps.image_tag.outputs.env }} down --remove-orphans || true - name: Pull latest images run: | DOCKER_CONTEXT=deploy_target docker compose -f docker-compose.yml -p ${{ steps.image_tag.outputs.env }} pull - name: Deploy with Docker Compose id: deploy run: | DOCKER_CONTEXT=deploy_target docker compose -f docker-compose.yml -p ${{ steps.image_tag.outputs.env }} up -d echo "Waiting for services to start..." sleep 10 CONTAINERS=$(DOCKER_CONTEXT=deploy_target docker compose -f docker-compose.yml -p ${{ steps.image_tag.outputs.env }} ps --services | tr '\n' ',' | sed 's/,$//') echo "containers=$CONTAINERS" >> $GITHUB_OUTPUT echo "✅ Deployment completed successfully!" echo "🌐 URL: ${{ steps.url.outputs.url }}" echo "📦 Containers: $CONTAINERS" - name: Show deployment status run: | echo "=== Deployment Status ===" DOCKER_CONTEXT=deploy_target docker compose -f docker-compose.yml -p ${{ steps.image_tag.outputs.env }} ps echo "========================="