diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml index a03c277..b7ddce2 100644 --- a/.gitea/workflows/deploy.yml +++ b/.gitea/workflows/deploy.yml @@ -1,48 +1,71 @@ -name: Build & Deploy +name: Build and Deploy on: push: branches: [main] + paths: + - "**" + - "Dockerfile" + - "docker-compose.yml" workflow_dispatch: +env: + REGISTRY: git.mancinas.io + REPO_NAME: ascii-art-app + jobs: - build-and-deploy: - runs-on: ubuntu-latest + build: + name: Build Image + runs-on: docker + container: + image: docker:27-dind + options: --privileged + permissions: + contents: read + packages: write steps: - - name: Checkout code - uses: actions/checkout@v4 - - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v3 - - - name: Login to Gitea Registry - uses: docker/login-action@v3 + - uses: actions/checkout@v4 + - uses: docker/setup-buildx-action@v3 + - uses: docker/login-action@v3 with: - registry: git.mancinas.io - username: ${{ secrets.REGISTRY_USER }} - password: ${{ secrets.REGISTRY_TOKEN }} - - - name: Build and push Docker image - uses: docker/build-push-action@v5 + registry: ${{ env.REGISTRY }} + username: ${{ secrets.REGISTRY_USERNAME }} + password: ${{ secrets.REGISTRY_PASSWORD }} + - id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ env.REGISTRY }}/${{ github.repository_owner }}/${{ env.REPO_NAME }} + tags: | + type=ref,event=branch + type=raw,value=latest,enable={{is_default_branch}} + - uses: docker/build-push-action@v5 with: context: . + file: Dockerfile push: true - tags: git.mancinas.io/rmancinas/ascii-art-app:latest - cache-from: type=gha - cache-to: type=gha,mode=max + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + platforms: linux/amd64 - - name: Deploy to Portainer via API - run: | - # Get current service spec - SERVICE_URL="${{ secrets.PORTAINER_API }}/endpoints/2/docker/services/ascii-art-app_ascii-art-app" - SPEC=$(curl -sk "$SERVICE_URL" -H "Authorization: Bearer ${{ secrets.PORTAINER_TOKEN }}") - - # Update version to force redeploy - VERSION=$(echo "$SPEC" | jq -r '.Version.Index') - UPDATED_SPEC=$(echo "$SPEC" | jq '.Spec | .TaskTemplate.ForceUpdate += 1') - - # Redeploy - curl -sk -X POST "$SERVICE_URL/update?version=$VERSION" \ - -H "Authorization: Bearer ${{ secrets.PORTAINER_TOKEN }}" \ - -H "Content-Type: application/json" \ - -d "$UPDATED_SPEC" + deploy: + name: Deploy to Portainer + needs: [build] + runs-on: docker + container: + image: node:18-alpine + if: github.ref == 'refs/heads/main' && (github.event_name == 'push' || github.event_name == 'workflow_dispatch') + steps: + - uses: actions/checkout@v4 + - uses: cssnr/portainer-stack-deploy-action@v1 + with: + url: ${{ secrets.PORTAINER_URL }} + token: ${{ secrets.PORTAINER_API_KEY }} + name: ${{ secrets.PORTAINER_STACK_NAME }} + file: docker-compose.yml + type: file + pull_image: true + endpoint_id: ${{ secrets.PORTAINER_ENDPOINT_ID }} + env_data: | + { + "IMAGE": "${{ env.REGISTRY }}/${{ github.repository_owner }}/${{ env.REPO_NAME }}:latest" + } diff --git a/docker-compose.yml b/docker-compose.yml index 5991777..78eb87f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,7 +2,7 @@ version: '3.8' services: ascii-art-app: - image: git.mancinas.io/rmancinas/ascii-art-app:latest + image: ${IMAGE:-git.mancinas.io/rmancinas/ascii-art-app:latest} deploy: replicas: 1 restart_policy: