1. Declarative vs Scripted Pipeline
Dalam Jenkins, terdapat dua gaya penulisan Jenkinsfile:
- Declarative Pipeline: Format yang disarankan untuk pemula hingga advanced. Memiliki struktur kaku yang rapi, mudah dibaca, dan dilengkapi pengecekan sintaksis ketat.
- Scripted Pipeline: Menggunakan sintaksis Groovy penuh tanpa batasan struktur kaku. Sangat fleksibel untuk logika logika kompleks tetapi lebih berisiko error jika tidak dikelola dengan baik.
2. Anatomy Berkas Declarative Jenkinsfile
Jenkinsfile (Declarative)
pipeline {
agent any
environment {
APP_NAME = 'my-payment-service'
BUILD_VERSION = "1.0.${BUILD_NUMBER}"
}
stages {
stage('Checkout') {
steps {
echo "Fetching code for ${APP_NAME} version ${BUILD_VERSION}..."
git branch: 'main', url: 'https://github.com/myorg/payment-service.git'
}
}
stage('Compile & Test') {
steps {
echo 'Compiling Java Application...'
sh './gradlew test'
}
}
stage('Package') {
steps {
sh './gradlew bootJar'
}
}
stage('Deploy Staging') {
steps {
echo 'Deploying to Staging Environment...'
sh 'ansible-playbook -i staging-hosts deploy.yml'
}
}
}
post {
always {
cleanWs()
}
success {
echo "Pipeline succeeded!"
}
failure {
echo "Pipeline failed!"
}
}
}