From fe7e4ae13f0f1805a7df5f9f6e777f715afae2fb Mon Sep 17 00:00:00 2001 From: leon-morival Date: Sun, 30 Nov 2025 16:44:40 +0100 Subject: [PATCH] feat: new ci/cd --- .gitlab-ci.yml | 57 ++++++++++++++++++++++++++++++++++++++++- Dockerfile | 25 +++++++++--------- docker-compose.prod.yml | 53 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 122 insertions(+), 13 deletions(-) create mode 100644 docker-compose.prod.yml diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 0c59a91..eed0803 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,10 +1,12 @@ stages: - test - build + - deploy variables: DOCKER_DRIVER: overlay2 +# 1️⃣ Tests Laravel (SQLite en mémoire) test: stage: test image: laravelsail/php84-composer @@ -24,7 +26,7 @@ test: script: - php artisan test -# 🐳 Build + push image dans le registry GitLab +# 2️⃣ Build + push image dans le registry GitLab docker-build-push: stage: build image: docker:24 @@ -50,3 +52,56 @@ docker-build-push: only: - main + +# 3️⃣ Déploiement : création / update de la stack Portainer via API +deploy-to-portainer: + stage: deploy + image: alpine:latest + needs: + - docker-build-push + only: + - main + + before_script: + - apk add --no-cache curl jq gettext + + script: + - echo "📦 Génération du docker-compose.rendered.yml avec les variables de prod..." + - envsubst < docker-compose.prod.yml > docker-compose.rendered.yml + + - | + echo "🔍 Recherche de la stack '$PORTAINER_STACK_NAME' dans Portainer..." + STACKS_JSON=$(curl -s -H "X-API-Key: $PORTAINER_API_KEY" "$PORTAINER_URL/api/stacks") + STACK_ID=$(echo "$STACKS_JSON" | jq ".[] | select(.Name == \"$PORTAINER_STACK_NAME\") | .Id") + + if [ -z "$STACK_ID" ]; then + echo "🆕 Aucune stack '$PORTAINER_STACK_NAME' trouvée, création en cours..." + + curl -s -X POST "$PORTAINER_URL/api/stacks?type=2&method=string&endpointId=$PORTAINER_ENDPOINT_ID" \ + -H "X-API-Key: $PORTAINER_API_KEY" \ + -H "Content-Type: application/json" \ + --data-binary @- <