1. docker-hub https://hub.docker.com/r/jenkins/jenkins
$ sudo docker pull jenkins/jenkins:jdk11
Example
1. Example 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
pipeline { agent any environment { BUILD_VERSION = "${TAG == '' ? currentBuild.number : TAG}" TYPE_NAME="schat_v5_test" // thư mục sau khi build (trong docker container) BUILD_DIRECTORY="~/Schat.Rocket.Chat/build/$TYPE_NAME" // thư mục volume với host (trong docker container) BUILD_VOLUME="~/build/$TYPE_NAME" DOCKER_REGISTRY_URL = "sgmgmtregistry.azurecr.io" DOCKER_REGISTRY_CREDENTIAL_ID = "dockerRegistry.sgh" // thư mục build image (ngoài host) IMAGE_DIRECTORY="/home/schatadmin/apps/build/$TYPE_NAME" IMAGE_NAME_TAG="$DOCKER_REGISTRY_URL/$TYPE_NAME:${BUILD_VERSION}" SCHAT_SERVER_TEST = "dev-schat02" dockerImage = "" } stages { stage('Git checkout branch') { steps { sh 'echo "Start checkout brand"' sh """docker exec schat_v5 bash -c '\ source .profile \ && cd ~/Schat.Rocket.Chat \ && git reset --hard\ && git gc --prune=now \ && git remote prune origin \ && git fetch --all \ && git checkout origin/$GIT_BRANCH \ '""" // git url: GIT_URL, credentialsId: GIT_CREDENTIAL_ID, branch: GIT_BRANCH } post { failure { sh 'echo "Git checkout false"' } unstable { sh 'echo "Git checkout false"' } } } stage('Yarn install') { steps { sh 'echo "Start yarn install"' script { if (YARN_REINSTALL == 'true') { sh """docker exec schat_v5 bash -c '\ source .profile \ && cd ~/Schat.Rocket.Chat/ \ && rm -rf ./node_modules/ \ && rm -rf ./apps/meteor/node_modules/ \ && { nvm use $NODEJS_VERSION || nvm install $NODEJS_VERSION; } \ && { yarn || { npm i -g yarn && yarn; }; } \ '""" // sh "yarn cache clean --all" } } } post { failure { sh 'echo "Yarn install false"' } unstable { sh 'echo "Yarn install unstable"' } } } stage('Yarn build') { steps { sh 'echo "Start yarn build"' sh """docker exec schat_v5 bash -c '\ source .profile \ && cd ~/Schat.Rocket.Chat/ \ && nvm use $NODEJS_VERSION \ && yarn build\ '""" } post { failure { sh 'echo "Yarn build failure"' } unstable { sh 'echo "Yarn build unstable"' } } } stage('Meteor build') { steps { sh 'echo "Start meteor build"' sh """docker exec schat_v5 bash -c '\ source .profile \ && cd ~/Schat.Rocket.Chat/apps/meteor \ && rm -rf $BUILD_DIRECTORY \ && nvm use $NODEJS_VERSION \ && meteor build --directory $BUILD_DIRECTORY\ '""" } post { failure { sh 'echo "Meteor build failure"' } unstable { sh 'echo "Meteor build unstable"' } } } stage('Docker build image') { steps { sh 'echo "Start docker build image"' sh """docker exec schat_v5 bash -c '\ sudo rm -rf $BUILD_VOLUME \ && sudo cp -R $BUILD_DIRECTORY $BUILD_VOLUME \ && sudo cp ~/Schat.Rocket.Chat/apps/meteor/.docker/Dockerfile $BUILD_VOLUME\ '""" script { dockerImage = docker.build(IMAGE_NAME_TAG, IMAGE_DIRECTORY) } } post { failure { sh 'echo "Docker build image failure"' } unstable { sh 'echo "Docker build image unstable"' } success { sh 'echo "Docker build image success"' } } } stage('Docker push to Registry') { steps { sh 'echo "Docker build image start"' script { docker.withRegistry('https://' + DOCKER_REGISTRY_URL, DOCKER_REGISTRY_CREDENTIAL_ID) { dockerImage.push() } } sh "docker images | grep $TYPE_NAME | awk \'{print \$3}\' | xargs docker image rm -f" } post { failure { sh 'echo "Docker build image failure"' } unstable { sh 'echo "Docker build image unstable"' } success { sh 'echo "Docker build image success"' } } } stage('Server run Docker') { environment { MONGO_URL = credentials("mongo.url.test") MONGO_OPLOG_URL = credentials("mongo.oplogUrl.test") } steps { sh 'echo "Start Server run"' sshRemoteServer(SCHAT_SERVER_TEST, '10.240.13.38', BUILD_VERSION, IMAGE_NAME_TAG, MONGO_URL, MONGO_OPLOG_URL) } post { failure { sh 'echo "Server run failure"' } unstable { sh 'echo "Server run unstable"' } success { sh 'echo "Server run success"' } } } } } def sshRemoteServer(SERVER_ID, INSTANCE_IP, BUILD_VERSION, IMAGE_NAME_TAG, MONGO_URL, MONGO_OPLOG_URL) { def DEPLOY_DIRECTORY = "~/deploy-test-v5" def SCHAT_PORT = 3000 def composeContent = composeText(IMAGE_NAME_TAG, SCHAT_PORT, INSTANCE_IP, MONGO_URL, MONGO_OPLOG_URL) def removeOldVersion = logicRemoveOldVersion("schat_v5_test") sshPublisher(publishers: [ sshPublisherDesc(configName: SERVER_ID, transfers: [ // Delete old image sshTransfer(execCommand: "cat <<'EOF' > $DEPLOY_DIRECTORY/remove-old-version.sh \n$removeOldVersion", sourceFiles: ""), sshTransfer(execCommand: "cd $DEPLOY_DIRECTORY && bash remove-old-version.sh", sourceFiles: ""), sshTransfer(execCommand: "mkdir -p $DEPLOY_DIRECTORY/$BUILD_VERSION", sourceFiles: ""), sshTransfer(execCommand: "cat <<'EOF' > $DEPLOY_DIRECTORY/$BUILD_VERSION/docker-compose.yml \n$composeContent", sourceFiles: ""), sshTransfer(execCommand: "cd $DEPLOY_DIRECTORY/$BUILD_VERSION && docker-compose pull", sourceFiles: ""), sshTransfer(execCommand: "cd $DEPLOY_DIRECTORY/$BUILD_VERSION && docker-compose up -d", sourceFiles: ""), ]) ]) } def composeText(IMAGE_NAME_TAG, schatPort, instanceIp, mongoUrl, mongoOplogUrl) { def compose = "version: '3'\n" compose += "services:\n" compose += " schat_v5_test:\n" compose += " container_name: schatapp\n" compose += " image: $IMAGE_NAME_TAG\n" compose += " command: >\n" compose += " bash -c\n" compose += " \"for i in `seq 1 30`; do\n" compose += " node main.js &&\n" compose += " s=\$\$\$? && break || s=\$\$\$?;\n" compose += " echo \\\"Tried \$\$i times. Waiting 5 secs...\\\";\n" compose += " sleep 5;\n" compose += " done; (exit \$\$s)\"\n" compose += " restart: unless-stopped\n" compose += " networks:\n" compose += " - schat_v5_test_network\n" compose += " volumes:\n" compose += " - /mnt/sghdevschatfilesa:/mnt/sghdevschatfilesa\n" compose += " environment:\n" compose += " - PORT=$schatPort\n" compose += " - ROOT_URL=http://127.0.0.1:$schatPort\n" compose += " - MONGO_URL=$mongoUrl&directConnection=true\n" compose += " - MONGO_OPLOG_URL=$mongoOplogUrl&directConnection=true\n" compose += " - INSTANCE_IP=$instanceIp\n" compose += " - TOOL_NODE_FLAGS=\"--max-old-space-size=2048\"\n" compose += " - DISABLE_PRESENCE_MONITOR=$DISABLE_PRESENCE_MONITOR\n" compose += " - DISABLE_MESSAGE_PARSER=true\n" compose += " ports:\n" compose += " - $schatPort:3000\n" compose += "networks:\n" compose += " schat_v5_test_network:\n" compose += " driver: bridge\n" compose } def logicRemoveOldVersion(string) { def bash = "#! /bin/sh\n" bash += "# Remove all containers run by image\n" bash += "if [[ \"\$(docker ps -a | grep $string | awk '{print \$1}')\" != \"\" ]]; then\n" bash += "docker rm -f \$(docker ps -a | grep $string | awk '{print \$1}')\n" bash += "fi\n" bash += "# Remove all image\n" bash += "if [[ \"\$(docker images | grep $string | awk '{print \$3}')\" != \"\" ]]; then\n" bash += "docker rmi -f \$(docker images | grep $string | awk '{print \$3}')\n" bash += "fi\n" bash += "# Remove all network\n" bash += "if [[ \"\$(docker network ls | grep $string | awk '{print \$1}')\" != \"\" ]]; then\n" bash += "docker network rm \$(docker network ls | grep $string | awk '{print \$1}')\n" bash += "fi\n" bash } |
2. Example 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
pipeline { agent any environment { DOCKER_REGISTRY_URL = "sgmgmtregistry.azurecr.io" DOCKER_REGISTRY_CREDENTIAL_ID = "dockerRegistry.sgh" IMAGE = "schat_test" IMAGE_PUSH = "$DOCKER_REGISTRY_URL/${IMAGE}:$NEXT_VERSION" GIT_URL = "https://github.com/ntqpjt/Schat.Rocket.Chat.git" GIT_CREDENTIAL_ID = "git.vuongtq" GIT_BRANCH = "$BUILD_BRANCH" BUILD_DIR = "/tmp/${IMAGE}" SCHAT_SERVER_TEST = "dev-schat02" dockerImage = "" } stages { stage('Git checkout') { steps { withCredentials([gitUsernamePassword(credentialsId: GIT_CREDENTIAL_ID, gitToolName: 'git-tool')]) { sh 'git gc --prune=now && git remote prune origin && git fetch --all' } git url: GIT_URL, credentialsId: GIT_CREDENTIAL_ID, branch: GIT_BRANCH } post { failure { notifyBuild('Git checkout') } unstable { notifyBuild('Git checkout') } } } stage('Install packages') { steps { sh "meteor npm cache clean --force" sh "rm -rf node_modules/" sh "meteor npm i" } post { failure { notifyBuild('Install packages') } unstable { notifyBuild('Install packages') } } } stage('Meteor build') { steps { sh "rm -rf $BUILD_DIR" sh "meteor build --directory $BUILD_DIR" } post { failure { notifyBuild('Meteor build') } unstable { notifyBuild('Meteor build') } } } stage('Build docker image') { steps { sh "cp .docker/Dockerfile $BUILD_DIR" script { dockerImage = docker.build(IMAGE_PUSH, BUILD_DIR) } } post { failure { notifyBuild('Build docker image') } unstable { notifyBuild('Build docker image') } success { notifyBuildSchat('start') } } } stage('Docker push to registry') { steps { script { docker.withRegistry('https://' + DOCKER_REGISTRY_URL, DOCKER_REGISTRY_CREDENTIAL_ID) { dockerImage.push() } } } post { failure { notifyBuild('Docker push to registry') } unstable { notifyBuild('Docker push to registry') } } } stage('Remove unused docker image') { steps { sh "docker rmi -f \$(docker images --filter=reference=\"$DOCKER_REGISTRY_URL/$IMAGE*\" -q)" } post { failure { notifyBuild('Remove unused docker image') } unstable { notifyBuild('Remove unused docker image') } } } stage('Deploy to remote servers') { environment { MONGO_URL = credentials("mongo.url.test") MONGO_OPLOG_URL = credentials("mongo.oplogUrl.test") } steps { sshRemoteServer(SCHAT_SERVER_TEST, '10.240.13.38', DOCKER_REGISTRY_URL, IMAGE, IMAGE_PUSH, CURRENT_VERSION, NEXT_VERSION, MONGO_URL, MONGO_OPLOG_URL) } post { failure { notifyBuild('Deploy to remote servers') } unstable { notifyBuild('Deploy to remote servers') } } } // stage('Create release version') { // environment { // LOGIN_CREDS = credentials('schat.login.test') // } // steps { // sleep 60 // seconds // script { // def loginRes = login(LOGIN_CREDS_USR, LOGIN_CREDS_PSW) // createReleaseVersion(loginRes.userId, loginRes.authToken) // } // } // post { // failure { // notifyBuild('Create release version') // } // unstable { // notifyBuild('Create release version') // } // } // } } post { success { notifyBuild('', 'success') notifyBuildSchat('done') } } } def sshRemoteServer(SERVER_ID, INSTANCE_IP, DOCKER_REGISTRY_URL, IMAGE, IMAGE_PUSH, CURRENT_VERSION, NEXT_VERSION, MONGO_URL, MONGO_OPLOG_URL) { def SCHAT_PORT = 3000 def HUBOT_PORT = 3001 def compose = composeText(IMAGE + '_' + NEXT_VERSION, IMAGE_PUSH, SCHAT_PORT, HUBOT_PORT, INSTANCE_IP, MONGO_URL, MONGO_OPLOG_URL) def fcompose = "~/deploy-test/$NEXT_VERSION/docker-compose.yml" def removeOldImage = removeOldImageText(DOCKER_REGISTRY_URL, IMAGE, CURRENT_VERSION) def fremoveOldImage = "~/deploy-test/remove-old-image.sh" sshPublisher(publishers: [ sshPublisherDesc(configName: SERVER_ID, transfers: [ // Delete old image sshTransfer(execCommand: "cat <<'EOF' > $fremoveOldImage \n$removeOldImage", sourceFiles: ""), sshTransfer(execCommand: "cd ~/deploy-test && bash remove-old-image.sh", sourceFiles: ""), sshTransfer(execCommand: "mkdir -p ~/deploy-test/$NEXT_VERSION", sourceFiles: ""), sshTransfer(execCommand: "cat <<'EOF' > $fcompose \n$compose", sourceFiles: ""), sshTransfer(execCommand: "cd ~/deploy-test/$NEXT_VERSION && docker-compose pull", sourceFiles: ""), sshTransfer(execCommand: "cd ~/deploy-test/$CURRENT_VERSION && docker-compose stop", sourceFiles: ""), sshTransfer(execCommand: "cd ~/deploy-test/$NEXT_VERSION && docker-compose up -d", sourceFiles: ""), ]) ]) } def composeText(image, imagePush, schatPort, hubotPort, instanceIp, mongoUrl, mongoOplogUrl) { def compose = "version: '3'\n" compose += "services:\n" compose += " $image:\n" compose += " image: $imagePush\n" compose += " command: >\n" compose += " bash -c\n" compose += " \"for i in `seq 1 30`; do\n" compose += " node main.js &&\n" compose += " s=\$\$\$? && break || s=\$\$\$?;\n" compose += " echo \\\"Tried \$\$i times. Waiting 5 secs...\\\";\n" compose += " sleep 5;\n" compose += " done; (exit \$\$s)\"\n" compose += " restart: unless-stopped\n" compose += " volumes:\n" compose += " - /mnt/sghdevschatfilesa:/mnt/sghdevschatfilesa\n" compose += " environment:\n" compose += " - PORT=3000\n" compose += " - ROOT_URL=http://127.0.0.1:$schatPort\n" compose += " - MONGO_URL=$mongoUrl\n" compose += " - MONGO_OPLOG_URL=$mongoOplogUrl\n" compose += " - INSTANCE_IP=$instanceIp\n" compose += " - TOOL_NODE_FLAGS=\"--max-old-space-size=2048\"\n" compose += " - DISABLE_PRESENCE_MONITOR=$DISABLE_PRESENCE_MONITOR\n" compose += " - DISABLE_MESSAGE_PARSER=true\n" compose += " ports:\n" compose += " - 3000:3000\n" // compose += " hubot:\n" // compose += " image: rocketchat/hubot-rocketchat:latest\n" // compose += " restart: unless-stopped\n" // compose += " environment:\n" // compose += " - ROCKETCHAT_URL=schat:$schatPort\n" // compose += " - ROCKETCHAT_ROOM=GENERAL\n" // compose += " - ROCKETCHAT_USER=bot\n" // compose += " - ROCKETCHAT_PASSWORD=botpassword\n" // compose += " - BOT_NAME=bot\n" // compose += " - EXTERNAL_SCRIPTS=hubot-help,hubot-seen,hubot-links,hubot-diagnostics\n" // compose += " depends_on:\n" // compose += " - $image\n" // compose += " labels:\n" // compose += " - 'traefik.enable=false'\n" // compose += " ports:\n" // compose += " - $hubotPort:8080\n" compose } def removeOldImageText(dockerRegistry, image, currentVersion) { def bash = "#! /bin/sh\n" bash += "# Remove stopped containers\n" bash += "if [[ \"\$(docker ps -a --filter 'ancestor=$dockerRegistry/$image:$currentVersion' --filter 'status=exited' -q)\" != \"\" ]]; then\n" bash += "docker rm \$(docker ps -a --filter 'ancestor=$dockerRegistry/$image:$currentVersion' --filter 'status=exited' -q)\n" bash += "fi\n" bash += "# Remove old images\n" bash += "if [[ \"\$(docker images --filter=reference=\"$dockerRegistry/$image*\" --filter \"before=$dockerRegistry/$image:$currentVersion\" -q)\" != \"\" ]]; then\n" bash += "docker rmi -f \$(docker images --filter=reference=\"$dockerRegistry/$image*\" --filter \"before=$dockerRegistry/$image:$currentVersion\" -q)\n" bash += "fi\n" bash } def notifyBuild(String step = '', String status = 'failed') { // Default values // def colorCode = '#FF9FA1' // def subject = status == 'failed' ? "[${env.JOB_NAME}][${env.BUILD_NUMBER}] Step \"${step}\" ${status}" : "[${env.JOB_NAME}][${env.BUILD_NUMBER}] Deploy successfully" // def summary = "${subject} (${env.BUILD_URL})" // if (status == 'failed') { // colorCode = '#FF9FA1' // } else { // colorCode = '#00FF00' // } // // Send notifications // slackSend (color: colorCode, message: summary) } def login(user, password) { def body = """ {"user": "$user", "password": "$password"} """ def response = httpRequest acceptType: 'APPLICATION_JSON', contentType: 'APPLICATION_JSON', httpMode: 'POST', requestBody: body, url: "https://test.schatapp.tk/api/v1/login" if (response.status == 200) { def jsonObj = readJSON text: response.content; if (jsonObj.status == "success") { jsonObj.data } } } def createReleaseVersion(userId, token) { def releaseNote = RELEASE_NOTE.replaceAll('\n', '{{NEW_LINE}}'); def body = """ {"version": "$NEXT_VERSION", "releaseNote": "$releaseNote"} """ def response = httpRequest acceptType: 'APPLICATION_JSON', contentType: 'APPLICATION_JSON_UTF8', httpMode: 'POST', requestBody: body, customHeaders:[[name: 'X-Auth-Token', value: "${token}"], [name: 'X-User-Id', value: "${userId}"]], url: "https://test.schatapp.tk/api/v1/web-versions.create" if (response.status == 200) { def jsonObj = readJSON text: response.content; if (jsonObj.status == "success") { jsonObj.data } } } def notifyBuildSchat(String step = 'start') { def message = "Em dang build con test nhe" def who = WHO ? WHO : "notify-build" def releaseNote = RELEASE_NOTE.replaceAll('\n', ', '); if (releaseNote) { message += ". Task: ${releaseNote}" } if (step == 'done') { message = 'Con test build xong roi m.n nha.' } def body = """ {"alias": "${who}", "text": "$message"} """ try { def response = httpRequest acceptType: 'APPLICATION_JSON', contentType: 'APPLICATION_JSON', httpMode: 'POST', requestBody: body, // url: "https://test.schatapp.tk/hooks/WtMMmeQftobYe3e67/YEtQHMh5hzQZDkJk2Z6a4TndgdqmsxQZ9QTBkTki6AB3md8d" url: "https://schat.ntq.solutions/hooks/ezczqdNisGabjBoBd/CFq7tLhqGoBgDwJSteCsXFjNqftdyuJNZ2KHBzPBser3bobu" } catch (err) { echo "Failed: ${err}" } } |