java -jar jenkins-cli.jar -s http://localhost:8080/ help | 도움말 표시 |
java -jar jenkins-cli.jar -s URL -auth user:token build JOB | 작업 빌드 |
java -jar jenkins-cli.jar -s URL list-jobs | 모든 작업 목록 |
java -jar jenkins-cli.jar -s URL get-job JOB | 작업 설정 가져오기 |
java -jar jenkins-cli.jar -s URL create-job JOB < config.xml | XML에서 작업 생성 |
java -jar jenkins-cli.jar -s URL delete-job JOB | 작업 삭제 |
java -jar jenkins-cli.jar -s URL disable-job JOB | 작업 비활성화 |
java -jar jenkins-cli.jar -s URL enable-job JOB | 작업 활성화 |
java -jar jenkins-cli.jar -s URL restart | Jenkins 재시작 |
java -jar jenkins-cli.jar -s URL safe-restart | 안전 재시작 |
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building...'
sh 'npm install'
sh 'npm run build'
}
}
stage('Test') {
steps {
echo 'Testing...'
sh 'npm test'
}
}
stage('Deploy') {
steps {
echo 'Deploying...'
sh './deploy.sh'
}
}
}
} // Any available agent
agent any
// No agent
agent none
// Specific label
agent {
label 'linux'
}
// Docker
agent {
docker {
image 'node:18'
args '-v /tmp:/tmp'
}
}
// Kubernetes
agent {
kubernetes {
yaml '''
spec:
containers:
- name: node
image: node:18
'''
}
} pipeline {
agent any
environment {
APP_NAME = 'my-app'
VERSION = '1.0.0'
CREDENTIALS = credentials('my-credentials')
}
stages {
stage('Build') {
environment {
STAGE_VAR = 'stage-specific'
}
steps {
echo "Building ${APP_NAME} v${VERSION}"
sh 'echo $CREDENTIALS_USR'
sh 'echo $CREDENTIALS_PSW'
}
}
}
} pipeline {
agent any
parameters {
string(name: 'VERSION', defaultValue: '1.0.0', description: 'Version to deploy')
booleanParam(name: 'RUN_TESTS', defaultValue: true, description: 'Run tests?')
choice(name: 'ENV', choices: ['dev', 'staging', 'prod'], description: 'Environment')
text(name: 'CONFIG', defaultValue: '', description: 'Configuration')
}
stages {
stage('Deploy') {
steps {
echo "Deploying ${params.VERSION} to ${params.ENV}"
}
}
}
} pipeline {
agent any
triggers {
// Poll SCM every 5 minutes
pollSCM('H/5 * * * *')
// Cron schedule
cron('H 4 * * 1-5')
// Upstream job
upstream(upstreamProjects: 'job1,job2', threshold: hudson.model.Result.SUCCESS)
// GitHub hook
githubPush()
}
stages {
stage('Build') {
steps {
echo 'Building...'
}
}
}
} pipeline {
agent any
stages {
stage('Deploy to Prod') {
when {
branch 'main'
environment name: 'DEPLOY', value: 'true'
}
steps {
echo 'Deploying to production'
}
}
stage('Test') {
when {
anyOf {
branch 'main'
branch 'develop'
}
}
steps {
sh 'npm test'
}
}
stage('Skip on Tag') {
when {
not {
tag '*'
}
}
steps {
echo 'Not a tag build'
}
}
}
} pipeline {
agent any
stages {
stage('Test') {
parallel {
stage('Unit Tests') {
steps {
sh 'npm run test:unit'
}
}
stage('Integration Tests') {
steps {
sh 'npm run test:integration'
}
}
stage('E2E Tests') {
steps {
sh 'npm run test:e2e'
}
}
}
}
}
} pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'npm run build'
}
}
}
post {
always {
echo 'This always runs'
cleanWs()
}
success {
echo 'Build succeeded!'
slackSend channel: '#ci', message: 'Build passed'
}
failure {
echo 'Build failed!'
mail to: 'team@example.com', subject: 'Build Failed'
}
unstable {
echo 'Build is unstable'
}
changed {
echo 'Status changed from last build'
}
}
} pipeline {
agent any
stages {
stage('Deploy') {
input {
message 'Deploy to production?'
ok 'Yes, deploy!'
submitter 'admin,deployers'
parameters {
string(name: 'VERSION', defaultValue: '1.0.0')
}
}
steps {
echo "Deploying version ${VERSION}"
}
}
}
} pipeline {
agent any
options {
timeout(time: 1, unit: 'HOURS')
retry(3)
timestamps()
buildDiscarder(logRotator(numToKeepStr: '10'))
}
stages {
stage('Deploy') {
options {
timeout(time: 5, unit: 'MINUTES')
}
steps {
retry(3) {
sh './deploy.sh'
}
}
}
}
} node {
stage('Checkout') {
checkout scm
}
stage('Build') {
sh 'npm install'
sh 'npm run build'
}
stage('Test') {
try {
sh 'npm test'
} catch (e) {
currentBuild.result = 'UNSTABLE'
}
}
stage('Deploy') {
if (env.BRANCH_NAME == 'main') {
sh './deploy.sh'
}
}
} // vars/buildPipeline.groovy
def call(Map config = [:]) {
pipeline {
agent any
stages {
stage('Build') {
steps {
sh "npm install"
sh "npm run build"
}
}
}
}
}
// Jenkinsfile
@Library('my-shared-library') _
buildPipeline(
name: 'my-app',
branch: env.BRANCH_NAME
) pipeline {
agent any
stages {
stage('Deploy') {
steps {
withCredentials([usernamePassword(
credentialsId: 'my-creds',
usernameVariable: 'USERNAME',
passwordVariable: 'PASSWORD'
)]) {
sh 'echo $USERNAME'
sh './deploy.sh'
}
}
}
}
} withCredentials([string(credentialsId: 'api-key', variable: 'API_KEY')]) {
sh 'curl -H "Authorization: Bearer $API_KEY" https://api.example.com'
} withCredentials([sshUserPrivateKey(
credentialsId: 'ssh-key',
keyFileVariable: 'SSH_KEY',
usernameVariable: 'SSH_USER'
)]) {
sh 'ssh -i $SSH_KEY $SSH_USER@server.com'
} docker.withRegistry('https://registry.example.com', 'docker-creds') {
def image = docker.build('my-app:latest')
image.push()
} checkout([$class: 'GitSCM',
branches: [[name: '*/main']],
extensions: [],
userRemoteConfigs: [[
credentialsId: 'github-creds',
url: 'https://github.com/org/repo.git'
]]
])
// Or simply
checkout scm stage('Docker') {
steps {
script {
def image = docker.build('my-app:${BUILD_NUMBER}')
docker.withRegistry('https://registry.hub.docker.com', 'docker-hub-creds') {
image.push()
image.push('latest')
}
}
}
} post {
success {
slackSend(
channel: '#ci-cd',
color: 'good',
message: "Build ${currentBuild.fullDisplayName} succeeded"
)
}
failure {
slackSend(
channel: '#ci-cd',
color: 'danger',
message: "Build ${currentBuild.fullDisplayName} failed"
)
}
} post {
failure {
emailext(
subject: "FAILED: ${currentBuild.fullDisplayName}",
body: "Build failed. Check console at ${BUILD_URL}",
recipientProviders: [developers(), requestor()]
)
}
}