foreword
Before I installed Jekins on Alibaba Cloud ECS ( article on installing jenkins ), I was going to reinstall it recently because of the mining Trojan on the server, but I thought that every time I installed jenkins, I had to manually install jdk, maven, docker, kubectl, I feel It's so troublesome, so I use the officially recommended blue ocean docker image to install and use it, and record some problems encountered during the installation and use process to share with you.
Use the blue ocean image to install jkenkins
First, let's open the official jenkins installation article to see how to install it.
1. Download and run Jenkins in Docker
docker run \
-u root \
--rm \
-d \
-p 8080:8080 \
-p 50000:50000 \
-v jenkins-data:/var/jenkins_home \
-v /var/run/docker.sock:/var/run/docker.sock \
jenkinsci/blueocean
2. Access the Jenkins/Blue Ocean Docker container
docker exec -it jenkins-blueocean bash
3. Post-Installation Setup Wizard - Unlock Jenkins
To view the jenkins key, you must use the following command to obtain and fill in.
docker exec -it jenkins-blueocean bash
docker logs <docker-container-name>
This completes the installation of Jenkins. Let's use Jenkins to publish next.js and spring projects through two examples.
build nodejs project
This is the installation method provided by the official website
Operation process :
- 0. New git service providers (gitlab, github, etc.) ssh credentials and k8s conifg ~/.kube/config credentials (without this kubectl authentication will fail)
- 1. First create a new pipeline task
- 2. Select Pipline Script or Pipline Script from SCM for the pipeline
- 3. Put the Jenkins script in it
- 4. Click Build Now
Below is the complete Jenkinsfile
pipeline {
agent any
environment {
GIT_REPOSITORY="xxx"
K8S_YAML="k8s/test-xxx-webapp.yaml"
POD_NAME="test-xxx-webapp"
DOCKER_USERNAME="xxx"
DOCKER_PWD="xxx
ALIYUN_DOCKER_HOST = 'xxx'
ALIYUN_DOCKER_NAMESPACE="com-xxx"
ALIYUN_DOCKER_REPOSITORY_NAME="webapp-test"
K8S_CONFIG = credentials('test-xxx-k8s-config-text')
}
stages {
stage("Clone") {
agent any
steps {
echo "1.Clone Stage"
// 删除文件夹
deleteDir()
git branch: 'test', credentialsId: 'xxx', url: "${GIT_REPOSITORY}"
script {
// 获取git代码tag为docker仓库tag
// GIT_TAG = sh(returnStdout: true,script: 'git describe --tags --always').trim()
// 获取git提交hash做为docker仓库tag
GIT_TAG = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim()
DOCKER_REPOSITORY = "${ALIYUN_DOCKER_HOST}/${ALIYUN_DOCKER_NAMESPACE}/${ALIYUN_DOCKER_REPOSITORY_NAME}"
// docker 阿里镜像仓库
DOCKER_REPOSITORY_TAG = "${DOCKER_REPOSITORY}:${GIT_TAG}"
}
}
}
stage("Test") {
agent any
steps {
echo "2.Test Stage"
}
}
stage("Build") {
agent any
steps {
echo "3.Build Docker Image Stage"
sh "docker build -t ${DOCKER_REPOSITORY_TAG} -f docker/Dockerfile ."
}
}
stage("Push") {
agent any
steps {
echo "4.Push Docker Image Stage"
//推送Docker镜像,username 跟 password 为 阿里云容器镜像服务的账号密码
sh "docker login --username=${DOCKER_USERNAME} --password=${DOCKER_PWD} ${ALIYUN_DOCKER_HOST}"
// 开始推送镜像
sh "docker push ${DOCKER_REPOSITORY_TAG}"
// 删除生成的image
//sh "docker rmi ${DOCKER_IMG_HOST}/${DOCKER_IMG_PATH}:${GIT_TAG}"
// 删除jenkins生成的image
sh '''
docker images | grep seaurl | awk '{print $3}' | xargs docker rmi -f
'''
}
}
stage("Deploy") {
agent {
docker {
image 'lwolf/helm-kubectl-docker'
}
}
steps {
echo "5.发布镜像"
sh "mkdir -p ~/.kube"
sh "echo ${K8S_CONFIG} | base64 -d > ~/.kube/config"
sh "sed -i 's#<imagename>#${DOCKER_REPOSITORY_TAG}#g;s#<podname>#${POD_NAME}#g' ${K8S_YAML}"
sh "kubectl apply -f ${K8S_YAML} -n xxx"
}
}
}
}
build java project
This is the installation method provided by the official website
Operation process:
0. New git service providers (gitlab, github, etc.) ssh credentials and k8s conifg ~/.kube/config credentials (without this kubectl authentication will fail)
1. First create a new pipeline task
2. Select Pipline Script or Pipline Script from SCM for the pipeline
3. Put the Jenkins script in it
4. Click Build Now
Below is the complete Jenkinsfile
pipeline {
agent any
environment {
GIT_REPOSITORY="xxx"
K8S_YAML="k8s/test-xxx-webapp.yaml"
POD_NAME="test-xxx-webapp"
DOCKER_USERNAME="xxx"
DOCKER_PWD="xxx
ALIYUN_DOCKER_HOST = 'xxx'
ALIYUN_DOCKER_NAMESPACE="com-xxx"
ALIYUN_DOCKER_REPOSITORY_NAME="webapp-test"
K8S_CONFIG = credentials('test-xxx-k8s-config-text')
}
stages {
stage("Clone") {
agent any
steps {
echo "1.Clone Stage"
// 删除文件夹
deleteDir()
git branch: 'test', credentialsId: 'xxx', url: "${GIT_REPOSITORY}"
script {
// 获取git代码tag为docker仓库tag
// GIT_TAG = sh(returnStdout: true,script: 'git describe --tags --always').trim()
// 获取git提交hash做为docker仓库tag
GIT_TAG = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim()
DOCKER_REPOSITORY = "${ALIYUN_DOCKER_HOST}/${ALIYUN_DOCKER_NAMESPACE}/${ALIYUN_DOCKER_REPOSITORY_NAME}"
// docker 阿里镜像仓库
DOCKER_REPOSITORY_TAG = "${DOCKER_REPOSITORY}:${GIT_TAG}"
}
}
}
stage("Test") {
agent any
steps {
echo "2.Test Stage"
}
}
stage("Build") {
agent {
docker {
image 'maven:3-alpine'
args '-v /root/.m2:/root/.m2'
}
}
steps {
echo "3.Build Server"
sh "mvn -e -U -pl ${MODULE_NAME} -am clean package -Dmaven.test.skip=true dockerfile:build -Ddockerfile.tag=${GIT_TAG} -Ddockerfile.repository=${DOCKER_REPOSITORY}"
}
}
stage("Push") {
agent any
steps {
echo "4.Push Docker Image Stage"
//推送Docker镜像,username 跟 password 为 阿里云容器镜像服务的账号密码
sh "docker login --username=${DOCKER_USERNAME} --password=${DOCKER_PWD} ${ALIYUN_DOCKER_HOST}"
// 开始推送镜像
sh "docker push ${DOCKER_REPOSITORY_TAG}"
// 删除生成的image
//sh "docker rmi ${DOCKER_IMG_HOST}/${DOCKER_IMG_PATH}:${GIT_TAG}"
// 删除jenkins生成的image
sh '''
docker images | grep seaurl | awk '{print $3}' | xargs docker rmi -f
'''
}
}
stage("Deploy") {
agent {
docker {
image 'lwolf/helm-kubectl-docker'
}
}
steps {
echo "5.发布镜像"
sh "mkdir -p ~/.kube"
sh "echo ${K8S_CONFIG} | base64 -d > ~/.kube/config"
sh "sed -i 's#<imagename>#${DOCKER_REPOSITORY_TAG}#g;s#<podname>#${POD_NAME}#g' ${K8S_YAML}"
sh "kubectl apply -f ${K8S_YAML} -n xxx"
}
}
}
}
Summarize
1. Because the docker-installed jenkins is used, the following agents can be used for the nodejs project and the java project on the plug-in respectively, and there is no need to install jdk or nodejs and maven to configure it, which is very convenient.
kubectl agent
agent {
docker {
image 'lwolf/helm-kubectl-docker'
}
}
maven agent
agent {
docker {
image 'maven:3-alpine'
args '-v /root/.m2:/root/.m2'
}
}
------- 2022-6-1 update---------
Modify the default 123456 password of the jenkins admin account
Notice:
If you want to configure your own private warehouse maven address (such as Alibaba Cloud effect maven warehouse), first enter the host directory (not in the docker container) /root/.m2
, and then create the setting.xml
file ( The setting.xml file is your own private maven setting.xml file)
2. When the k8s configuration file ~/.kube/config file is used as the jenkins credential, it must be transcoded with base64, otherwise the build will fail.
base64 convert ~/.kube/config file command
base64 ~/.kube/config > kube-config.txt
Execute commands in Jenkinsfile
sh "echo ${K8S_CONFIG} | base64 -d > ~/.kube/config"
3. Add one under each stage or the build will fail
agent any
4. If Pipline Script from SCM is selected when creating a new pipeline task, then the Jenkinsfile file existing in the project is selected.
5. Finally, I am very grateful to Jianshu's @tinylk
the articles written by the big guy helped me a lot. The first and second references below are his articles and sample codes.
quote
Use jenkins pipeline to automatically build and deploy to k8s
pipeline-demo gitee
Install and setup kubectl in Linux system
jenkins-to-docker-image issue
Jenkins-based CI/CD (2)
K8S+Jenkins implements CI/CD of SpringBoot project
CICD Pipeline To Deploy To Kubernetes Cluster Using Jenkins | Jenkins Kubernetes Integration
Jenkins implements Docker in Docker in Pod and deploys with kubectl
Jenkins+k8s realizes automatic deployment
Jenkins connects multiple poses of k8s
Jenkins master is located outside the k8s cluster to realize the dynamic construction of jenkins slave
Jenkins connects to K8S cluster
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。