Files
test-cicd/.woodpecker/build-pipeline.yml
2026-05-02 12:34:58 +02:00

176 lines
5.9 KiB
YAML
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
when:
- event: push
branch: [ main ]
- event: pull_request
- event: manual
variables:
- &node_image node:18-alpine
- &git_image alpine/git
- &curl_image curlimages/curl:latest
steps:
# Étape 1: Nettoyage du projet
- name: clean_project
image: *git_image
commands: |
rm -rf dist
rm -f ${CI_WORKSPACE}/version.env
echo "✅ Nettoyage terminé"
depends_on: [ ]
# Étape 2: Analyser le message du commit
- name: prepare
image: *git_image
commands: |
COMMIT_MSG=$(git log -1 --pretty=%B)
echo "📝 Message: $COMMIT_MSG"
if echo "$COMMIT_MSG" | grep -qE "^(major)(\(.*\))?!:"; then
echo "version_type=major" >> ${CI_WORKSPACE}/version.env
echo "🔴 MAJOR - Breaking change"
elif echo "$COMMIT_MSG" | grep -qE "^(minor)(\(.*\))?:"; then
echo "version_type=minor" >> ${CI_WORKSPACE}/version.env
echo "🟡 MINOR - Nouvelle fonctionnalité"
elif echo "$COMMIT_MSG" | grep -qE "^(patch)(\(.*\))?:"; then
echo "version_type=patch" >> ${CI_WORKSPACE}/version.env
echo "🟢 PATCH - Correction"
else
echo "version_type=none" >> ${CI_WORKSPACE}/version.env
echo "⚠️ Aucun changement de version"
fi
depends_on: [ clean_project ]
# Étape 3: Mettre à jour la version
- name: update_version
image: *node_image
commands: |
npm ci --cache .npm --prefer-offline
source ${CI_WORKSPACE}/version.env
if [ "$version_type" != "none" ]; then
CURRENT_VERSION=$(node -p "require('./package.json').version")
echo "📦 Version actuelle: $CURRENT_VERSION"
npm run update:$version_type
NEW_VERSION=$(node -p "require('./package.json').version")
echo "🆕 Nouvelle version: $NEW_VERSION"
echo "NEW_VERSION=$NEW_VERSION" >> ${CI_WORKSPACE}/version.env
echo "CURRENT_VERSION=$CURRENT_VERSION" >> ${CI_WORKSPACE}/version.env
echo "artifact=${CI_REPO_NAME}-$NEW_VERSION.tar" >> ${CI_WORKSPACE}/version.env
else
echo "artifact=no-artifact" >> ${CI_WORKSPACE}/version.env
echo "⏭️ Pas de mise à jour de version"
fi
depends_on: [ prepare ]
# Étape 4: Build de l'application
- name: build
image: *node_image
commands: |
npm ci --cache .npm --prefer-offline
npm run build
depends_on: [ update_version ]
# Étape 5: Package des artefacts
- name: package
image: *node_image
commands: |
source ${CI_WORKSPACE}/version.env
if [ "$version_type" != "none" ]; then
mkdir -p ${CI_WORKSPACE}/artifacts/${NEW_VERSION}
# Créer les archives individuelles
tar -czf ${CI_WORKSPACE}/artifacts/${NEW_VERSION}/front.tar.gz -C frontend/dist . 2>/dev/null || echo "⚠️ frontend/dist vide"
tar -czf ${CI_WORKSPACE}/artifacts/${NEW_VERSION}/back.tar.gz -C backend/dist . 2>/dev/null || echo "⚠️ backend/dist vide"
tar -czf ${CI_WORKSPACE}/artifacts/${NEW_VERSION}/db.tar.gz -C database src *.json 2>/dev/null || echo "⚠️ database vide"
# Créer l'artefact final
cd ${CI_WORKSPACE}/artifacts/${NEW_VERSION}
tar -cf ${CI_WORKSPACE}/artifacts/${artifact} .
echo "✅ Artefact créé: ${CI_WORKSPACE}/artifacts/${artifact}"
ls -la ${CI_WORKSPACE}/artifacts/${artifact}
fi
depends_on: [ build ]
# Étape 6: Publication dans Nexus
- name: publish
image: rockdrilla/woodpecker-sonatype-nexus
settings:
url: http://nexus.cicd.exygen.fr/
auth.base64:
from_secret: YWRtaW46UGFzc3dvcmQxIQ==
upload:
- repository: domaine-passignat
paths:
- ${CI_WORKSPACE}/artifacts/${artifact}
depends_on: [ package ]
# Étape 7: Commit et tag (après publication)
- name: commit_tag
image: *git_image
environment:
GITEA_TOKEN:
from_secret: gitea_token
commands: |
source ${CI_WORKSPACE}/version.env
if [ "$version_type" != "none" ]; then
git remote set-url origin http://oauth2:${GITEA_TOKEN}@gitea.cicd.exygen.fr/${CI_REPO_OWNER}/${CI_REPO_NAME}.git
git config user.email "woodpecker@exygen.fr"
git config user.name "Woodpecker CI"
git add package.json ./database/src/sql/*.sql 2>/dev/null || true
# Commiter uniquement s'il y a des changements
if ! git diff --cached --quiet; then
git commit -m "chore: release v${NEW_VERSION} [skip ci]"
git push origin main
echo "✅ Commit créé"
else
echo " Aucun changement à commiter"
fi
# Créer et pousser le tag
git tag -a "v${NEW_VERSION}" -m "Release version v${NEW_VERSION}"
git push origin "v${NEW_VERSION}"
echo "🏷️ Tag v${NEW_VERSION} créé et poussé"
else
echo "⚠️ Aucun changement à commiter"
fi
depends_on: [ publish ]
# Étape 8: Déclencher le déploiement
- name: trigger-deployment
image: curlimages/curl:latest
environment:
WOODPECKER_TOKEN:
from_secret: woodpecker_token
commands: |
source ${CI_WORKSPACE}/version.env
if [ "$version_type" != "none" ]; then
echo "🚀 Déclenchement du déploiement pour la version ${NEW_VERSION}"
curl -X POST \
"https://woodpecker.cicd.exygen.fr/api/repos/${CI_REPO_OWNER}/${CI_REPO_NAME}/pipelines" \
-H "Authorization: Bearer ${WOODPECKER_TOKEN}" \
-H "Content-Type: application/json" \
-d '{
"branch": "main",
"event": "deployment",
"deploy_to": "production",
"deploy_id": "'${CI_PIPELINE_NUMBER}'",
"ref": "refs/tags/v'${NEW_VERSION}'"
}'
echo "✅ Événement deployment déclenché"
else
echo "⏭️ Pas de déclenchement (version_type=none)"
fi
depends_on: [ commit_tag ]