diff --git a/.woodpecker/build-pipeline.yml b/.woodpecker/build-pipeline.yml index ffa66e6..0a626e2 100644 --- a/.woodpecker/build-pipeline.yml +++ b/.woodpecker/build-pipeline.yml @@ -1,6 +1,6 @@ when: - event: push - branch: [ main ] # ← Les tags ne sont sur aucune "branch" + branch: [ main ] - event: pull_request - event: manual @@ -10,90 +10,91 @@ variables: - &curl_image curlimages/curl:latest steps: - # Étape 1: Nettoyage du projet, à ajuster + # Étape 1: Nettoyage du projet - name: clean_project image: *git_image commands: | rm -rf dist - mkdir *wdir - ls *wdir - rm -f *wdir/version.env + rm -f ${CI_WORKSPACE}/version.env + echo "✅ Nettoyage terminé" + depends_on: [ ] + # Étape 2: Analyser le message du commit - name: prepare image: *git_image commands: | - # Récupérer le message du commit COMMIT_MSG=$(git log -1 --pretty=%B) echo "📝 Message: $COMMIT_MSG" - # Déterminer le type de version if echo "$COMMIT_MSG" | grep -qE "^(major)(\(.*\))?!:"; then - echo "version_type=major" >> $CI_WORKSPACE/version.env + 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 "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 "version_type=patch" >> ${CI_WORKSPACE}/version.env echo "🟢 PATCH - Correction" else - echo "version_type=none" >> $CI_WORKSPACE/version.env - echo "⚠️ Aucun changement de version" + echo "version_type=none" >> ${CI_WORKSPACE}/version.env + echo "⚠️ Aucun changement de version" fi depends_on: [ clean_project ] - # Étape 4: Mettre à jour la version (seulement si nécessaire) + # Étape 3: Mettre à jour la version - name: update_version image: *node_image commands: | - ls -alsR database - mkdir -p database/src/sql npm ci --cache .npm --prefer-offline - source $CI_WORKSPACE/version.env + 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 + 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 "artifact=no-artifact" >> ${CI_WORKSPACE}/version.env echo "⏭️ Pas de mise à jour de version" fi depends_on: [ prepare ] - # Étape 1: Installation des dépendances + # Étape 4: Build de l'application - name: build image: *node_image - commands: - - npm ci --cache .npm --prefer-offline - - npm run build + commands: | + npm ci --cache .npm --prefer-offline + npm run build depends_on: [ update_version ] - - - # Étape 6: Package des artefacts + # Étape 5: Package des artefacts - name: package - image: *curl_image + image: *node_image commands: | - more $CI_WORKSPACE/version.env - source $CI_WORKSPACE/version.env + source ${CI_WORKSPACE}/version.env + if [ "$version_type" != "none" ]; then - WDIR=${CI_WORKSPACE}/artifacts/$NEW_VERSION - echo "create work directory: $WDIR" - mkdir -p $WDIR - ls -alsR - tar -czf $WDIR/db.tar.gz -C database . - echo "create final artifact: ${CI_WORKSPACE}/artifacts/$artifact" - tar -cf ${CI_WORKSPACE}/artifacts/$artifact -C $WDIR . - ls -als ${CI_WORKSPACE}/artifacts/$artifact + 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 - ls -als ${CI_WORKSPACE}/artifacts/ depends_on: [ build ] + # Étape 6: Publication dans Nexus - name: publish image: *curl_image environment: @@ -102,66 +103,92 @@ steps: NEXUS_PASSWORD: from_secret: nexus_password commands: | - source $CI_WORKSPACE/version.env - echo "📦 Publication de la version $NEW_VERSION : $artifact" - ls -alsR ${CI_WORKSPACE}/artifacts - - curl -v \ - -u ${NEXUS_USERNAME}:${NEXUS_PASSWORD} \ - -F "raw.directory=apps" \ - -F "raw.asset1=@${CI_WORKSPACE}/artifacts/$artifact" \ - -F "raw.asset1.filename=$artifact" \ - http://nexus/service/rest/v1/components?repository=domaine-passignat + source ${CI_WORKSPACE}/version.env + + if [ "$version_type" != "none" ]; then + ARTIFACT_PATH="${CI_WORKSPACE}/artifacts/${artifact}" + + echo "📦 Publication de la version ${NEW_VERSION} : ${artifact}" + + if [ ! -f "${ARTIFACT_PATH}" ]; then + echo "❌ Fichier introuvable: ${ARTIFACT_PATH}" + ls -la ${CI_WORKSPACE}/artifacts/ + exit 1 + fi + + curl -v \ + -u "${NEXUS_USERNAME}:${NEXUS_PASSWORD}" \ + -F "raw.directory=apps/${CI_REPO_NAME}/${NEW_VERSION}" \ + -F "raw.asset1=@${ARTIFACT_PATH}" \ + -F "raw.asset1.filename=${artifact}" \ + https://nexus.cicd.exygen.fr/service/rest/v1/components?repository=domaine-passignat + + echo "✅ Artefact publié" + else + echo "⏭️ Pas de publication (version_type=none)" + fi depends_on: [ package ] - # Étape 5: Commit et tag + + # Étape 7: Commit et tag (après publication) - name: commit_tag image: *git_image environment: GITEA_TOKEN: from_secret: gitea_token commands: | - # Charger les variables - more $CI_WORKSPACE/version.env - source $CI_WORKSPACE/version.env - echo "version type: $version_type" + source ${CI_WORKSPACE}/version.env if [ "$version_type" != "none" ]; then - echo "http://oauth2:$GITEA_TOKEN@gitea.cicd.exygen.fr/${CI_REPO_OWNER}/${CI_REPO_NAME}.git" - git remote set-url origin http://oauth2:$GITEA_TOKEN@gitea.cicd.exygen.fr/${CI_REPO_OWNER}/${CI_REPO_NAME}.git + 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" - # Ajouter le remote avec token + git add package.json ./database/src/sql/*.sql 2>/dev/null || true - # Ajouter le package.json modifié - git add ./database/src/sql/*.sql + # 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 - # Vérifier s'il y a des changements - # if ! git diff --cached --quiet; then - # Commiter la nouvelle version - git commit -m "Pipeline: update current version v$NEW_VERSION [skip ci]" - git push origin main - echo "✅ Commit new version" - - # Créer 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 created and pushed" + # 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: woodpeckerci/plugin-trigger:latest - settings: - token: + image: curlimages/curl:latest + environment: + WOODPECKER_TOKEN: from_secret: woodpecker_token - server: https://woodpecker.cicd.exygen.fr # ← Ajoute l'URL du serveur - repo: ${CI_REPO_OWNER}/${CI_REPO_NAME} - branch: main - event: deployment - deploy_id: ${CI_PIPELINE_NUMBER} # ← Peut être optionnel - deploy_to: production # ← Renommé en "environment" parfois - wait: true # ← Attend la fin du déploiement - depends_on: [ publish ] \ No newline at end of file + 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 ] \ No newline at end of file