安装master

  • 下载master镜像:docker pull cnkevin/jenkins
  • 启动master镜像:docker run -d --name="jenkins_master" -p 8100:8080 cnkevin/jenkins
  • 配置nginx.conf
  • 访问jks

jks-1

  • 复制initialAdminPassword文件中的内容到输入框中,该内容即为admin密码
  • 安装推荐的插件

jks-2

  • 设置密码或者跳过

jks-3

  • 若安装失败,可手动安装插件,具体参考[附1-jks插件]

配置从节点,以maven为例

  • 下载镜像: docker pull cnkevin/jenkins-slave-dind-java-maven
  • 运行镜像: docker run -d --name='slave-maven' cnkevin/jenkins-slave-dind-java-maven
  • 添加凭据

jks-4
* username:登陆从节点服务器时的用户名
* password:登陆从节点服务器时的密码
* id:凭据标识

  • 在jks中添加maven从节点

jks-5

jks-6

jks-7

  • 参考上述步骤添加python节点
  • 在maven容器中测试git 和mvn 命令是否正常
    • git clone 测试git命令正常(github均是公开,不需要配置ssh key ,如果是gitlab需要设置凭据);
    • 通过vi命令在home/jenkins/.m2下创建settings.xml文件

配置job

  • 新建pipelines任务
  • 项目添加 dockerfile
  • jenkins窗口中构建docker时报错
* /home/jenkins/workspace/common-frame # docker ps
Cannot connect to the Docker daemon. Is the docker daemon running on this host?
* 解决方案:创建slave-maven-docker 容器时,挂载docker.sock文件;然后在jenkins中配置从节点,将ip改为最新的slave-maven-docker容器ip
* 具体命令:docker run -d --name=slave-maven-docker -v /home/liu-kevin/workspace:/home/jenkins/workspace -v /var/run/docker.sock:/var/run/docker.sock 36f8f8a4736b
注:此处应再加上mvn本地仓库的目录挂载
参考:https://blog.csdn.net/zhou8622/article/details/52259709
  • pipeline内容
//项目github地址
def github = 'https://github.com/clkevin/$app_name.git'
def image_name = "docker.io/cnkevin/$app_name:1.0.0-test"

node('slave-maven-docker'){
    stage('checkout') { 
        git branch: "$brans", credentialsId: 'github', url: github
    }
    stage('compile') { 
        sh "mvn -Dmaven.test.skip=true clean package"
    }
    
    stage('push docker') { 
        sh "sudo docker build -t $image_name -f docker/Dockerfile ."
        sh "sudo docker push $image_name"
    }
    stage('run docker'){
        sh "sudo docker run -d $image_name docker/entrypoint.sh"
    }
}

附1-jks插件

插件名 版本 具体名
Symbol annotation 1.10 org.jenkins-ci:symbol-annotation:1.10
Structs 1.10 org.jenkins-ci.plugins:structs:1.10
Pipeline: Step API 2.14 org.jenkins-ci.plugins.workflow:workflow-step-api:2.14
Pipeline: API 2.24 org.jenkins-ci.plugins.workflow:workflow-api:2.24
Pipeline: Milestone Step 1.3.1 org.jenkins-ci.plugins:pipeline-milestone-step:1.3.1
Pipeline: Supporting APIs 2.14 org.jenkins-ci.plugins.workflow:workflow-support:2.14
Pipeline: SCM Step 2.5 org.jenkins-ci.plugins.workflow:workflow-scm-step:2.5
Pipeline: Groovy 2.41 org.jenkins-ci.plugins.workflow:workflow-cps:2.41
Pipeline: Input Step 2.8 org.jenkins-ci.plugins:pipeline-input-step:2.8
Pipeline: Stage Step 2.3 org.jenkins-ci.plugins:pipeline-stage-step:2.3
Pipeline: Job 2.11.2 org.jenkins-ci.plugins.workflow:workflow-job:2.11.2
Pipeline Graph Analysis 1.6 org.jenkins-ci.plugins:pipeline-graph-analysis:1.6
Jackson 2 API 2.7.3 org.jenkins-ci.plugins:jackson2-api:2.7.3
Pipeline: REST API 2.9 org.jenkins-ci.plugins.pipeline-stage-view:pipeline-rest-api:2.9
bouncycastle API 2.16.2 org.jenkins-ci.plugins:bouncycastle-api:2.16.2
Pipeline: Stage View 2.9 org.jenkins-ci.plugins.pipeline-stage-view:pipeline-stage-view:2.9
Pipeline: Build Step 2.6 org.jenkins-ci.plugins:pipeline-build-step:2.6
Pipeline: Model API 1.2.6 org.jenkinsci.plugins:pipeline-model-api:1.2.6
Pipeline: Declarative Extension Points API 1.2.6 org.jenkinsci.plugins:pipeline-model-extensions:1.2.6
Apache HttpComponents Client 4.x API 4.5.3-2.0 org.jenkins-ci.plugins:apache-httpcomponents-client-4-api:4.5.3-2.0
Git client 2.7.0 org.jenkins-ci.plugins:git-client:2.7.0
GIT server Plugin 1.7 org.jenkins-ci.plugins:git-server:1.7
Folders 6.1.2 org.jenkins-ci.plugins:cloudbees-folder:6.1.2
Pipeline: Shared Groovy Libraries 2.9 org.jenkins-ci.plugins.workflow:workflow-cps-global-lib:2.9
Branch API 2.0.18 org.jenkins-ci.plugins:branch-api:2.0.18
Pipeline: Multibranch 2.16 org.jenkins-ci.plugins.workflow:workflow-multibranch:2.16
Plain Credentials Plugin 1.4 org.jenkins-ci.plugins:plain-credentials:1.4
Credentials Binding 1.14 org.jenkins-ci.plugins:credentials-binding:1.14
Docker Commons 1.11 org.jenkins-ci.plugins:docker-commons:1.11
Pipeline: Nodes and Processes 2.16 org.jenkins-ci.plugins.workflow:workflow-durable-task-step:2.16
Pipeline: Basic Steps 2.6 org.jenkins-ci.plugins.workflow:workflow-basic-steps:2.6
Docker Pipeline 1.14 org.jenkins-ci.plugins:docker-workflow:1.14
Pipeline: Stage Tags Metadata 1.2.6 org.jenkinsci.plugins:pipeline-stage-tags-metadata:1.2.6
Pipeline: Declarative Agent API 1.1.1 org.jenkinsci.plugins:pipeline-model-declarative-agent:1.1.1
Pipeline: Declarative 1.2.6 org.jenkinsci.plugins:pipeline-model-definition:1.2.6
Pipeline 2.5 org.jenkins-ci.plugins.workflow:workflow-aggregator:2.5
Authentication Tokens API Plugin 1.3 org.jenkins-ci.plugins:authentication-tokens:1.3
Azure Commons 0.2.4 org.jenkins-ci.plugins:azure-commons:0.2.4
Config File Provider 2.17 org.jenkins-ci.plugins:config-file-provider:2.17
Credentials 2.1.16 org.jenkins-ci.plugins:credentials:2.1.16
Dashboard View 2.9.11 org.jenkins-ci.plugins:dashboard-view:2.9.11
Display URL API 2.2.0 org.jenkins-ci.plugins:display-url-api:2.2.0
Durable Task 1.17 org.jenkins-ci.plugins:durable-task:1.17
Jenkins Mailer Plugin 1.4 org.jenkins-ci.plugins:mailer:1.4
Email Ext Recipients Column Plugin 1.0 org.jenkins-ci.plugins:email-ext-recipients-column:1.0
Email Extension 2.61 org.jenkins-ci.plugins:email-ext:2.61
Email Extension Template Plugin 1 org.jenkinsci.plugins:emailext-template:1.0
Git 3.7.0 org.jenkins-ci.plugins:git:3.7.0
Icon Shim Plugin 2.0.3 org.jenkins-ci.plugins.icon-shim:icon-shim:2.0.3
Javadoc Plugin 1.4 org.jenkins-ci.plugins:javadoc:1.4
JavaScript GUI Lib: ACE Editor bundle plugin 1.1 org.jenkins-ci.ui:ace-editor:1.1
JavaScript GUI Lib: Handlebars bundle plugin 1.1.1 org.jenkins-ci.ui:handlebars:1.1.1
JavaScript GUI Lib: jQuery bundles (jQuery and jQuery UI) plugin 1.2.1 org.jenkins-ci.ui:jquery-detached:1.2.1
JavaScript GUI Lib: Moment.js bundle plugin 1.1.1 org.jenkins-ci.ui:momentjs:1.1.1
JSch dependency 0.1.54.1 org.jenkins-ci.plugins:jsch:0.1.54.1
JUnit 1.23 org.jenkins-ci.plugins:junit:1.23
Kubernetes 1.1.3 org.csanchez.jenkins.plugins:kubernetes:1.1.3
Kubernetes Cli Plugin 1.0.0 org.jenkins-ci.plugins:kubernetes-cli:1.0.0
Kubernetes Continuous Deploy 0.1.4 org.jenkins-ci.plugins:kubernetes-cd:0.1.4
Kubernetes Credentials 0.2.0 org.jenkinsci.plugins:kubernetes-credentials:0.2.0
Mailer 1.2 org.jenkins-ci.plugins:mailer:1.20
Matrix Authorization Strategy 1.7 org.jenkins-ci.plugins:matrix-auth:1.7
Matrix Project 1.12 org.jenkins-ci.plugins:matrix-project:1.12
Maven Integration 3 org.jenkins-ci.main:maven-plugin:3.0
OWASP Markup Formatter Plugin 1.5 org.jenkins-ci.plugins:antisamy-markup-formatter:1.5
Poll Mailbox Trigger Plugin 1.026
Resource Disposer 0.8 org.jenkins-ci.plugins:resource-disposer:0.8
SCM API 2.2.6 org.jenkins-ci.plugins:scm-api:2.2.6
Script Security 1.4 org.jenkins-ci.plugins:script-security:1.40
SSH Agent Plugin 1.15 org.jenkins-ci.plugins:ssh-agent:1.15
SSH Credentials 1.13 org.jenkins-ci.plugins:ssh-credentials:1.13
SSH Slaves 1.25 org.jenkins-ci.plugins:ssh-slaves:1.25
Timestamper 1.8.9 org.jenkins-ci.plugins:timestamper:1.8.9
Token Macro 2.3 org.jenkins-ci.plugins:token-macro:2.3
user build vars plugin 1.5 org.jenkins-ci.plugins:build-user-vars-plugin:1.5
Variant Plugin 1.1 org.jenkins-ci.plugins:variant:1.1
Windows Slaves Plugin 1.3.1
Workspace Cleanup Plugin 0.34 org.jenkins-ci.plugins:ws-cleanup:0.34

以上插件参考
打包好的部分插件
插件名称是想要安装的插件,在其它已安装的jks中查找该插件,并点击版本,可查找到插件的mavenid即hpi包;然后在http://updates.jenkins-ci.org/download/plugins/下载需要的插件

附2-jks pipeline

app_version = "1.0."+ env.BUILD_NUMBER
currentBuild.displayName = app_version
def image_name = "$DOCKER_HUB_ADDRESS/$app_name:$app_version-$tag"
try{
    node {
        wrap([$class: 'BuildUser']) {
            // aa="${BUILD_USER}"
            // bb="${BUILD_USER_ID}"
            builder="${BUILD_USER_EMAIL}"
        }
    }
    node('slave-maven'){
        stage('checkout') { 
            git branch: "$brans", credentialsId: 'gitlab', url: "$GIT_URL/${app_name}.git"
       }
        stage('maven build') {
            sh "mvn -Dmaven.test.skip=true clean package"
        }
        stage('push docker') {
            sh "docker build -t $image_name -f docker/Dockerfile ."
            sh "docker push $image_name"  
        }
    }
    node('slave-python') {
        stage('deploy') {
            sh """python /home/jenkins/script/deploy_testA.py $cluster_id $app_name $rc_number $image_name $app_status $tag $USE_CONFIG_SERVER $request_mem $limit_mem $name_space""" 
        }
        stage('check_deploy') {
            sh """python /home/jenkins/script/check_svc.py $cluster_id $app_name $name_space"""
        }
    }
} catch (e) {
        // If there was an exception thrown, the build failed
            currentBuild.result = "FAILED"
            throw e
        } finally {
        // Success or failure, always send notifications
        notifyBuild(currentBuild.result)
        }
        
def notifyBuild(String buildStatus = 'STARTED') {
    // build status of null means successful
    buildStatus = buildStatus ?: 'SUCCESSFUL'
  // Default values
  def subject = "Jenkins $tag ${buildStatus}: Job '${env.JOB_NAME}' (${env.BUILD_NUMBER})"
  def details = '''(本邮件是程序自动下发的,请勿回复!)<br/><hr/>

项目名称:$PROJECT_NAME<br/><hr/>

构建编号:$BUILD_NUMBER<br/><hr/>

构建状态:$BUILD_STATUS<br/><hr/>

触发原因:${CAUSE}<br/><hr/>

构建日志地址:<a href="${BUILD_URL}console">${BUILD_URL}console</a><br/><hr/>

构建地址:<a href="$BUILD_URL">$BUILD_URL</a><br/><hr/>

变更集:${JELLY_SCRIPT,template="html"}<br/><hr/>'''

  emailext (
      //attachLog: true,
      subject: subject,
      body: details,
      to: "$builder"
    )
}

附3-dockerfile

FROM cnkevin/java-jre

ENV PROJECT_DIR=/opt/docker
WORKDIR $PROJECT_DIR

COPY target/app.jar $PROJECT_DIR/app.jar
COPY docker/entrypoint.sh $PROJECT_DIR/entrypoint.sh
RUN chown -R daemon:daemon $PROJECT_DIR

EXPOSE 8080
USER daemon

ENTRYPOINT ["sh", "entrypoint.sh"]

entrypoint.sh 中启动镜像中的java,同时可从配置中心拉取配置文件,达到不同环境配置文件不同的效果

附4-entrypoint.sh

#!/usr/bin/env bash


echo -e "下载应用$APPLICATION_NAME标签为$TAG的配置文件..."
wget -O application.yml http://$CONFIG_SERVER_ADDRESS/api/download?tag=$TAG\&application=$APPLICATION_NAME
echo -e "========================== 配置文件信息 ========================== \n"
cat $PROJECT_DIR/application.yml
echo -e "\n=============================================================== \n"
exec java -Djava.security.egd=file:/dev/./urandom -jar $PROJECT_DIR/app.jar --spring.config.name=app --spring.config.location=$PROJECT_DIR/application.yml