diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml index d7a069b..49f5b81 100644 --- a/.gitea/workflows/deploy.yml +++ b/.gitea/workflows/deploy.yml @@ -8,21 +8,28 @@ on: - '*' jobs: - build: + 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 @@ -33,14 +40,6 @@ jobs: REGISTRY_ACCESS_TOKEN: ${{ secrets.REGISTRY_ACCESS_TOKEN }} REGISTRY_USER: ${{ gitea.actor }} - - name: Create environment file - run: | - cat > .env << EOF - TOLGEE_API_URL=${{ vars.TOLGEE_API_URL }} - TOLGEE_API_KEY=${{ secrets.TOLGEE_API_KEY }} - TRANSLATION_CACHE_TTL=${{ vars.TRANSLATION_CACHE_TTL }} - EOF - - name: Determine URL id: url run: | @@ -52,13 +51,53 @@ jobs: echo "url=$URL" >> $GITHUB_OUTPUT echo "URL will be: $URL" - - name: Deploy application - uses: ./.gitea/actions/deploy - with: - project_name: ${{ steps.branch_name.outputs.branch_name }} - url: '${{ steps.url.outputs.url }}' - image_url: ${{ steps.docker_build.outputs.image_url }} - deployment_key: ${{ secrets.SSH_DEPLOYMENT_KEY }} - ssh_host: ${{ vars.ssh_host }} - ssh_port: ${{ vars.ssh_port }} - ssh_user: ${{ vars.ssh_user }} + - 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 "========================="