k8s 构建jenkins-slave


k8s-构建jenkins-slave

1. 编写 jenkins-slave配置文件

  • 编写jenkins-slave的dockerfile文件

    FROM centos:7
    LABEL maintainer jenkins-slave
    
    RUN yum install -y java-1.8.0-openjdk maven curl git libtool-ltdl-devel && \ 
        yum clean all && \
        rm -rf /var/cache/yum/* && \
        mkdir -p /usr/share/jenkins
    
    COPY slave.jar /usr/share/jenkins/slave.jar  
    COPY jenkins-slave /usr/bin/jenkins-slave
    COPY settings.xml /etc/maven/settings.xml
    RUN chmod +x /usr/bin/jenkins-slave
    
    ENTRYPOINT ["jenkins-slave"]
    
    
  • 编写jenkins-slave的启动脚本

    #!/usr/bin/env sh
    
    if [ $# -eq 1 ]; then
    
    	# if `docker run` only has one arguments, we assume user is running alternate command like `bash` to inspect the image
    	exec "$@"
    
    else
    
    	# if -tunnel is not provided try env vars
    	case "$@" in
    		*"-tunnel "*) ;;
    		*)
    		if [ ! -z "$JENKINS_TUNNEL" ]; then
    			TUNNEL="-tunnel $JENKINS_TUNNEL"
    		fi ;;
    	esac
    
    	# if -workDir is not provided try env vars
    	if [ ! -z "$JENKINS_AGENT_WORKDIR" ]; then
    		case "$@" in
    			*"-workDir"*) echo "Warning: Work directory is defined twice in command-line arguments and the environment variable" ;;
    			*)
    			WORKDIR="-workDir $JENKINS_AGENT_WORKDIR" ;;
    		esac
    	fi
    
    	if [ -n "$JENKINS_URL" ]; then
    		URL="-url $JENKINS_URL"
    	fi
    
    	if [ -n "$JENKINS_NAME" ]; then
    		JENKINS_AGENT_NAME="$JENKINS_NAME"
    	fi  
    
    	if [ -z "$JNLP_PROTOCOL_OPTS" ]; then
    		echo "Warning: JnlpProtocol3 is disabled by default, use JNLP_PROTOCOL_OPTS to alter the behavior"
    		JNLP_PROTOCOL_OPTS="-Dorg.jenkinsci.remoting.engine.JnlpProtocol3.disabled=true"
    	fi
    
    	# If both required options are defined, do not pass the parameters
    	OPT_JENKINS_SECRET=""
    	if [ -n "$JENKINS_SECRET" ]; then
    		case "$@" in
    			*"${JENKINS_SECRET}"*) echo "Warning: SECRET is defined twice in command-line arguments and the environment variable" ;;
    			*)
    			OPT_JENKINS_SECRET="${JENKINS_SECRET}" ;;
    		esac
    	fi
    	
    	OPT_JENKINS_AGENT_NAME=""
    	if [ -n "$JENKINS_AGENT_NAME" ]; then
    		case "$@" in
    			*"${JENKINS_AGENT_NAME}"*) echo "Warning: AGENT_NAME is defined twice in command-line arguments and the environment variable" ;;
    			*)
    			OPT_JENKINS_AGENT_NAME="${JENKINS_AGENT_NAME}" ;;
    		esac
    	fi
    
    	#TODO: Handle the case when the command-line and Environment variable contain different values.
    	#It is fine it blows up for now since it should lead to an error anyway.
    
    	exec java $JAVA_OPTS $JNLP_PROTOCOL_OPTS -cp /usr/share/jenkins/slave.jar hudson.remoting.jnlp.Main -headless $TUNNEL $URL $WORKDIR $OPT_JENKINS_SECRET $OPT_JENKINS_AGENT_NAME "$@"
    fi
    
    
  • jenkins-slave的配置文件settings.xml

    <?xml version="1.0" encoding="UTF-8"?>
    
    
    
    
    
      
    
      
    
      
    
      
      
        
      
    
      
      
        
      
    
      
      
        
    
        
      
    
      
      
        
             
          central     
          central     
          aliyun maven
          https://maven.aliyun.com/repository/public     
        
      
    
      
      
        
    
        
      
    
      
    
    
    
  • 下载slave的包

    
    

2. 使用docker build构建镜像

  • 执行构建命令

    root@k8s-master jenkins-slave]# docker build -t harbor.scajy.org/library/jenkins-slave:1.8 .
    
  • 执行私有仓库登陆

    [root@k8s-master jenkins-slave]# docker login -u admin -p'Aa123456' harbor.scajy.org
    
  • 查询镜像

    [root@k8s-master jenkins-slave]# docker images
    REPOSITORY                                                        TAG       IMAGE ID       CREATED         SIZE
    harbor.scajy.org/library/jenkins-slave                            1.8       804b32f7129d   2 minutes ago   545MB
    
  • 推送镜像到harbor仓库

    [root@k8s-master jenkins-slave]# docker push harbor.scajy.org/library/jenkins-slave:1.8
    The push refers to repository [harbor.scajy.org/library/jenkins-slave]
    b828e93cff2e: Pushed 
    5c72c602436a: Pushed 
    719b9a6ac7c9: Pushed 
    d5abbc8a828f: Pushed 
    33f56046d363: Pushed 
    174f56854903: Pushed 
    1.8: digest: sha256:798282e72962a18984d3468eac8a29f7524670a2713617348226838c0d13c442 size: 1574
    
  • 验证镜像上传是否成功

k8s