Presets / Jenkinsfile déclaratif + Docker
preset CI

Jenkinsfile déclaratif + Docker

jenkins  ·  CI

Pipeline déclaratif : checkout, test, build image, push registre.

jenkins declarative docker
pipeline {
  agent any
  environment {
    REGISTRY = 'registry.example.com'
    IMAGE    = "${REGISTRY}/myapp:${env.BUILD_NUMBER}"
  }
  options {
    timestamps()
    timeout(time: 30, unit: 'MINUTES')
  }
  stages {
    stage('Checkout') {
      steps { checkout scm }
    }
    stage('Test') {
      agent { docker { image 'python:3.12'; reuseNode true } }
      steps {
        sh 'pip install -r requirements.txt -r requirements-dev.txt'
        sh 'pytest --junitxml=report.xml'
      }
      post { always { junit 'report.xml' } }
    }
    stage('Build & Push') {
      when { branch 'main' }
      steps {
        script {
          docker.withRegistry("https://${REGISTRY}", 'registry-credentials') {
            def img = docker.build(IMAGE)
            img.push()
            img.push('latest')
          }
        }
      }
    }
  }
  post {
    failure { mail to: 'team@example.com', subject: "Build ${env.JOB_NAME} #${env.BUILD_NUMBER} KO", body: "${env.BUILD_URL}" }
  }
}