|
498 | 498 |
|
499 | 499 | #### 简单的 pipeline 写法(Docker 方式运行)(闭源项目 -- 码云为例)
|
500 | 500 |
|
| 501 | +- **确保** 项目根目录有 Dockerfile 文件(部分内容自己修改),内容模板: |
501 | 502 |
|
| 503 | +``` |
| 504 | +FROM java:8 |
| 505 | +VOLUME /tmp |
| 506 | +
|
| 507 | +ENV TZ=Asia/Shanghai |
| 508 | +RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone |
| 509 | +
|
| 510 | +ADD ./target/buildApp.jar /app.jar |
| 511 | +
|
| 512 | +RUN bash -c 'touch /app.jar' |
| 513 | +
|
| 514 | +EXPOSE 8081 |
| 515 | +
|
| 516 | +ENTRYPOINT ["java", "-jar", "-Xms512M", "-Xmx512M" , "-XX:MetaspaceSize=128M", "-XX:MaxMetaspaceSize=256M" ,"/app.jar"] |
| 517 | +``` |
| 518 | + |
| 519 | +- Pipeline 写法 |
| 520 | + |
| 521 | +``` |
| 522 | +pipeline { |
| 523 | + agent any |
| 524 | +
|
| 525 | + /*=======================================工具环境修改-start=======================================*/ |
| 526 | + tools { |
| 527 | + jdk 'JDK8' |
| 528 | + maven 'MAVEN3' |
| 529 | + } |
| 530 | + /*=======================================工具环境修改-end=======================================*/ |
| 531 | +
|
| 532 | + options { |
| 533 | + timestamps() |
| 534 | + disableConcurrentBuilds() |
| 535 | + buildDiscarder(logRotator( |
| 536 | + numToKeepStr: '20', |
| 537 | + daysToKeepStr: '30', |
| 538 | + )) |
| 539 | + } |
502 | 540 |
|
| 541 | + /*=======================================常修改变量-start=======================================*/ |
503 | 542 |
|
| 543 | + environment { |
| 544 | + gitUrl = "https://gitee.com/youmeek/springboot-jenkins-demo.git" |
| 545 | + branchName = "master" |
| 546 | + giteeCredentialsId = "Gitee" |
| 547 | + projectWorkSpacePath = "${env.WORKSPACE}" |
| 548 | + projectBuildTargetPath = "${env.WORKSPACE}/target" |
| 549 | + projectJarNewName = "buildApp.jar" |
| 550 | +
|
| 551 | +
|
| 552 | + dockerImageName = "docker.youmeek.com/demo/${env.JOB_NAME}:${env.BUILD_NUMBER}" |
| 553 | + dockerContainerName = "${env.JOB_NAME}" |
| 554 | + inHostPort = "8082" |
| 555 | + inDockerAndJavaPort = "8081" |
| 556 | + inHostLogPath = "/data/docker/logs/${dockerContainerName}" |
| 557 | + inDockerLogPath = "/data/logs" |
| 558 | + dockerRunParam = "--name ${dockerContainerName} -v /etc/hosts:/etc/hosts -v ${inHostLogPath}:${inDockerLogPath} --restart=always -p ${inHostPort}:${inDockerAndJavaPort}" |
| 559 | + } |
| 560 | + |
| 561 | + /*=======================================常修改变量-end=======================================*/ |
| 562 | + |
| 563 | + stages { |
| 564 | + |
| 565 | + stage('Pre Env') { |
| 566 | + steps { |
| 567 | + echo "======================================项目名称 = ${env.JOB_NAME}" |
| 568 | + echo "======================================项目 URL = ${gitUrl}" |
| 569 | + echo "======================================项目分支 = ${branchName}" |
| 570 | + echo "======================================当前编译版本号 = ${env.BUILD_NUMBER}" |
| 571 | + echo "======================================项目空间文件夹路径 = ${projectWorkSpacePath}" |
| 572 | + echo "======================================项目 build 后 jar 路径 = ${projectBuildTargetPath}" |
| 573 | + echo "======================================项目 jar 新名称 = ${projectJarNewName}" |
| 574 | + echo "======================================Docker 镜像名称 = ${dockerImageName}" |
| 575 | + echo "======================================Docker 容器名称 = ${dockerContainerName}" |
| 576 | + } |
| 577 | + } |
| 578 | + |
| 579 | + stage('Git Clone'){ |
| 580 | + steps { |
| 581 | + git branch: "${branchName}", |
| 582 | + credentialsId: "${giteeCredentialsId}", |
| 583 | + url: "${gitUrl}" |
| 584 | + } |
| 585 | + } |
| 586 | +
|
| 587 | + stage('Maven Clean') { |
| 588 | + steps { |
| 589 | + sh "mvn clean" |
| 590 | + } |
| 591 | + } |
| 592 | +
|
| 593 | + stage('Maven Package') { |
| 594 | + steps { |
| 595 | + sh "mvn package -DskipTests" |
| 596 | + } |
| 597 | + } |
| 598 | +
|
| 599 | + stage('构建 Docker 镜像') { |
| 600 | + steps { |
| 601 | + sh """ |
| 602 | + mv ${projectBuildTargetPath}/*.jar ${projectBuildTargetPath}/${projectJarNewName} |
| 603 | + |
| 604 | + cd ${projectWorkSpacePath} |
| 605 | + |
| 606 | + docker build -t ${dockerImageName} ./ |
| 607 | + """ |
| 608 | + } |
| 609 | + } |
| 610 | +
|
| 611 | + stage('运行 Docker 镜像') { |
| 612 | + steps { |
| 613 | + sh """ |
| 614 | + docker rm -f ${dockerContainerName} | true |
| 615 | + |
| 616 | + docker run -d ${dockerRunParam} ${dockerImageName} |
| 617 | + """ |
| 618 | + } |
| 619 | + } |
| 620 | + |
| 621 | + |
| 622 | +
|
| 623 | + |
| 624 | + |
| 625 | +
|
| 626 | + } |
| 627 | +} |
| 628 | +``` |
504 | 629 |
|
505 | 630 |
|
506 | 631 |
|
|
0 commit comments