diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..5b54465 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,84 @@ +pipeline { + agent any + environment { + GITEA_REPO_PATH = "lucas/site-spationautes" + GITEA_API_URL = "https://gitea.lucasroyer.fr/api/v1" + DOCKER_HOST = "unix:///run/user/1001/docker.sock" + + TOOLBOX_PATH ="/home/lucas/services/static-sites/static-toolbox" + SOURCE_DIR = "html" + DEPLOY_ROOT = "/home/lucas/services/static-sites/site-spationautes" + } + stages { + stage('Check toolbox') { + steps { + sh """ + if ! docker image inspect static-toolbox >/dev/null 2>&1; then + echo "Missing toolbox, rebuild using Docker socket..." + # On crée l'image à la volée car Jenkins ne voit pas le script 'build' + echo "FROM node:25-alpine\nRUN npm install -g htmlhint\nWORKDIR /apps\nENTRYPOINT [\"htmlhint\"]" | docker build -t static-toolbox - + fi + """ + } + } + stage('Lint HTML') { + steps { + echo "Check HTML files..." + sh "docker run --rm --volumes-from jenkins -w \$(pwd) static-toolbox '${env.SOURCE_DIR}/**/*.html' --config .htmlhintrc" + } + } + + stage('Deploy') { + steps { + echo "Deploying via Docker mount..." + // Copy with docker alpine + sh """ + docker run --rm \ + --volumes-from jenkins \ + -v ${env.DEPLOY_ROOT}:/site-deploy \ + -w \$(pwd) \ + alpine:latest \ + sh -c " \ + mkdir -p /site-deploy/html-new && \ + cp -R html/* /site-deploy/html-new/ && \ + rm -rf /site-deploy/html-prod && \ + mv /site-deploy/html-new /site-deploy/html-prod \ + " + """ + + echo "Reloading Caddy..." + sh "docker exec caddy-reverse-proxy caddy reload --config /etc/caddy/Caddyfile" + } + } + } + post { + always { + script { + echo "Waiting for Gitea to be online..." + + echo "Send Gitea check..." + // Get and store SHA + def commitSha = sh(script: 'git rev-parse HEAD', returnStdout: true).trim() + + // Convert from Jenkins to Gitea API + def buildState = (currentBuild.currentResult == 'SUCCESS') ? 'success' : 'failure' + def buildDesc = (currentBuild.currentResult == 'SUCCESS') ? 'Build successful' : 'Build failed' + + // Send it to Gitea API with secret 'gitea-token' + withCredentials([string(credentialsId: 'gitea-token', variable: 'GITEA_TOKEN')]) { + // Use \$TOKEN to avoid jenkins to print token in logs + sh """ + curl -f -X POST "${GITEA_API_URL}/repos/${GITEA_REPO_PATH}/statuses/${commitSha}" \ + -H "Authorization: token \$GITEA_TOKEN" \ + -H "Content-Type: application/json" \ + -d '{"state": "${buildState}", "target_url": "${env.BUILD_URL}", "description": "${buildDesc}", "context": "jenkins-ci"}' + """ + } + } + echo "Clean unused image" + sh "docker image prune -f" + } + success { echo "Success !" } + failure { echo "Failed." } + } +}