108 lines
3.1 KiB
YAML
108 lines
3.1 KiB
YAML
stages:
|
||
- test
|
||
- build
|
||
- deploy
|
||
|
||
variables:
|
||
DOCKER_DRIVER: overlay2
|
||
|
||
# 1️⃣ Tests Laravel (SQLite en mémoire)
|
||
test:
|
||
stage: test
|
||
image: laravelsail/php84-composer
|
||
|
||
variables:
|
||
APP_ENV: testing
|
||
APP_KEY: base64:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
|
||
DB_CONNECTION: sqlite
|
||
DB_DATABASE: ":memory:"
|
||
|
||
before_script:
|
||
- apt-get update && apt-get install -y git zip unzip
|
||
- composer install --no-interaction --prefer-dist
|
||
# on génère un .env de base pour que Laravel soit content
|
||
- cp .env.example .env || touch .env
|
||
|
||
script:
|
||
- php artisan test
|
||
|
||
# 2️⃣ Build + push image dans le registry GitLab
|
||
docker-build-push:
|
||
stage: build
|
||
image: docker:24
|
||
services:
|
||
- docker:24-dind
|
||
|
||
variables:
|
||
DOCKER_TLS_CERTDIR: "/certs"
|
||
IMAGE_TAG: "$CI_REGISTRY_IMAGE:${CI_COMMIT_SHORT_SHA}"
|
||
|
||
before_script:
|
||
# Login au registry GitLab
|
||
- echo "$CI_REGISTRY_PASSWORD" | docker login -u "$CI_REGISTRY_USER" --password-stdin "$CI_REGISTRY"
|
||
|
||
script:
|
||
# Build de l'image à partir du Dockerfile de la racine
|
||
- docker build -t "$IMAGE_TAG" .
|
||
# Tag 'latest' en plus (optionnel mais pratique)
|
||
- docker tag "$IMAGE_TAG" "$CI_REGISTRY_IMAGE:latest"
|
||
# Push des tags
|
||
- docker push "$IMAGE_TAG"
|
||
- docker push "$CI_REGISTRY_IMAGE:latest"
|
||
|
||
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 @- <<EOF
|
||
{
|
||
"name": "$PORTAINER_STACK_NAME",
|
||
"stackFileContent": "$(cat docker-compose.rendered.yml | sed 's/"/\\"/g' | tr -d '\n')",
|
||
"prune": true
|
||
}
|
||
EOF
|
||
|
||
echo "✅ Stack créée."
|
||
else
|
||
echo "♻️ Stack '$PORTAINER_STACK_NAME' trouvée (ID=$STACK_ID), mise à jour..."
|
||
|
||
curl -s -X PUT "$PORTAINER_URL/api/stacks/$STACK_ID?endpointId=$PORTAINER_ENDPOINT_ID" \
|
||
-H "X-API-Key: $PORTAINER_API_KEY" \
|
||
-H "Content-Type: application/json" \
|
||
--data-binary @- <<EOF
|
||
{
|
||
"id": $STACK_ID,
|
||
"stackFileContent": "$(cat docker-compose.rendered.yml | sed 's/"/\\"/g' | tr -d '\n')",
|
||
"prune": true
|
||
}
|
||
EOF
|
||
|
||
echo "✅ Stack mise à jour."
|
||
fi
|