feat: new ci/cd
This commit is contained in:
parent
ccb616ac7f
commit
fe7e4ae13f
|
|
@ -1,10 +1,12 @@
|
||||||
stages:
|
stages:
|
||||||
- test
|
- test
|
||||||
- build
|
- build
|
||||||
|
- deploy
|
||||||
|
|
||||||
variables:
|
variables:
|
||||||
DOCKER_DRIVER: overlay2
|
DOCKER_DRIVER: overlay2
|
||||||
|
|
||||||
|
# 1️⃣ Tests Laravel (SQLite en mémoire)
|
||||||
test:
|
test:
|
||||||
stage: test
|
stage: test
|
||||||
image: laravelsail/php84-composer
|
image: laravelsail/php84-composer
|
||||||
|
|
@ -24,7 +26,7 @@ test:
|
||||||
script:
|
script:
|
||||||
- php artisan test
|
- php artisan test
|
||||||
|
|
||||||
# 🐳 Build + push image dans le registry GitLab
|
# 2️⃣ Build + push image dans le registry GitLab
|
||||||
docker-build-push:
|
docker-build-push:
|
||||||
stage: build
|
stage: build
|
||||||
image: docker:24
|
image: docker:24
|
||||||
|
|
@ -50,3 +52,56 @@ docker-build-push:
|
||||||
|
|
||||||
only:
|
only:
|
||||||
- main
|
- 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
|
||||||
|
|
|
||||||
25
Dockerfile
25
Dockerfile
|
|
@ -1,33 +1,34 @@
|
||||||
|
# -----------------------------
|
||||||
|
# Étape 1 : build (composer)
|
||||||
|
# -----------------------------
|
||||||
FROM laravelsail/php84-composer AS build
|
FROM laravelsail/php84-composer AS build
|
||||||
|
|
||||||
WORKDIR /var/www/html
|
WORKDIR /var/www/html
|
||||||
|
|
||||||
# Copier le code
|
# On copie d'abord les fichiers composer pour profiter du cache Docker
|
||||||
COPY . .
|
COPY composer.json composer.lock ./
|
||||||
|
|
||||||
# Installer les dépendances PHP (prod)
|
# Installation des dépendances PHP en mode production
|
||||||
RUN composer install --no-dev --no-interaction --prefer-dist --optimize-autoloader
|
RUN composer install --no-dev --no-interaction --prefer-dist --optimize-autoloader
|
||||||
|
|
||||||
# Si tu as du front (Vite), tu pourrais ajouter :
|
# On copie le reste du projet (code Laravel, config, etc.)
|
||||||
# RUN npm ci && npm run build
|
COPY . .
|
||||||
|
|
||||||
# -----------------------------
|
# -----------------------------
|
||||||
# Étape 2 : runtime
|
# Étape 2 : image runtime
|
||||||
# -----------------------------
|
# -----------------------------
|
||||||
FROM laravelsail/php84-composer
|
FROM laravelsail/php84-composer
|
||||||
|
|
||||||
WORKDIR /var/www/html
|
WORKDIR /var/www/html
|
||||||
|
|
||||||
# Copier les fichiers depuis l’étape build
|
# On récupère tout ce qui a été préparé dans l'étape build
|
||||||
COPY --from=build /var/www/html /var/www/html
|
COPY --from=build /var/www/html /var/www/html
|
||||||
|
|
||||||
# (Optionnel) Tu peux retirer les fichiers inutiles ici (tests, etc.)
|
# On désactive l'ENTRYPOINT par défaut de l'image de base
|
||||||
|
|
||||||
# ⚠️ On désactive l'ENTRYPOINT de l'image de base
|
|
||||||
ENTRYPOINT []
|
ENTRYPOINT []
|
||||||
|
|
||||||
# Port HTTP exposé dans le container
|
# On expose le port HTTP du serveur Laravel
|
||||||
EXPOSE 80
|
EXPOSE 80
|
||||||
|
|
||||||
# Lancer Laravel via php artisan serve
|
# Commande de démarrage : serveur Laravel
|
||||||
CMD ["php", "artisan", "serve", "--host=0.0.0.0", "--port=80"]
|
CMD ["php", "artisan", "serve", "--host=0.0.0.0", "--port=80"]
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,53 @@
|
||||||
|
services:
|
||||||
|
treasure-api:
|
||||||
|
image: registry.gitlab.com/treasure-hunt4/treasure-api:latest
|
||||||
|
restart: unless-stopped
|
||||||
|
depends_on:
|
||||||
|
- treasure-api-migrate
|
||||||
|
ports:
|
||||||
|
- "8000:80"
|
||||||
|
environment:
|
||||||
|
APP_ENV: production
|
||||||
|
APP_KEY: ${APP_KEY}
|
||||||
|
APP_DEBUG: "false"
|
||||||
|
|
||||||
|
DB_CONNECTION: mysql
|
||||||
|
DB_HOST: mysql
|
||||||
|
DB_PORT: 3306
|
||||||
|
DB_DATABASE: ${DB_DATABASE}
|
||||||
|
DB_USERNAME: ${DB_USERNAME}
|
||||||
|
DB_PASSWORD: ${DB_PASSWORD}
|
||||||
|
|
||||||
|
treasure-api-migrate:
|
||||||
|
image: registry.gitlab.com/treasure-hunt4/treasure-api:latest
|
||||||
|
restart: "no"
|
||||||
|
depends_on:
|
||||||
|
- mysql
|
||||||
|
environment:
|
||||||
|
APP_ENV: production
|
||||||
|
APP_KEY: ${APP_KEY}
|
||||||
|
|
||||||
|
DB_CONNECTION: mysql
|
||||||
|
DB_HOST: mysql
|
||||||
|
DB_PORT: 3306
|
||||||
|
DB_DATABASE: ${DB_DATABASE}
|
||||||
|
DB_USERNAME: ${DB_USERNAME}
|
||||||
|
DB_PASSWORD: ${DB_PASSWORD}
|
||||||
|
|
||||||
|
command: ["php", "artisan", "migrate", "--force", "--no-interaction"]
|
||||||
|
|
||||||
|
mysql:
|
||||||
|
image: mysql:8.0
|
||||||
|
restart: unless-stopped
|
||||||
|
environment:
|
||||||
|
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
|
||||||
|
MYSQL_DATABASE: ${DB_DATABASE}
|
||||||
|
MYSQL_USER: ${DB_USERNAME}
|
||||||
|
MYSQL_PASSWORD: ${DB_PASSWORD}
|
||||||
|
volumes:
|
||||||
|
- mysql-data:/var/lib/mysql
|
||||||
|
ports:
|
||||||
|
- "3307:3306" # optionnel, juste si tu veux accéder à MySQL depuis l'extérieur
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
mysql-data:
|
||||||
Loading…
Reference in New Issue