环境准备
安装插件:
安装好Kubernetes plugin后,配置Kubernetes,我们的构建全部是放在k8s里面做的,可以做到多编译环境灵活切换以及提升jenkins效率。
这里建议关闭https证书检查,避免不必要的麻烦。
还有一定要使用admin.conf放入凭据里,防止权限不够,创建不了pod
推荐直接使用docker安装:Docker GitLab by sameersbn
当然你也可以直接使用Google的gitlab
docker安装:
docker pull docker.bintray.io/jfrog/artifactory-jcr:latest
docker run --name artifactory-jcr \
-d \
-v /app/jfrog/artifactory:/var/opt/jfrog/artifactory \
-p 8081:8081 -p 8082:8082 \
--privileged=true \
docker.bintray.io/jfrog/artifactory-jcr:latest
项目CI/CD配置
Jenkins 配置
创建一个多分支流水线任务
对于一般项目,直接使用pipline即可。这里使用多分支流水线,考虑的是一个git仓库里面有多个子项目需要单独构建的复杂场景。
分支源设置
设置触发自动集成规则
这里我们要根据打好的tag去自动集成,所以需要设置自动发现tag,以及build strategies里面的 tag触发规则
设置git仓库中Jenkins pipline文件的位置?
?设置Jenkins 扫描规则
?这里采用Jenkins Scan的方式去拉取git仓库信息,你也可以设置为webhook。
DockerFile配置
FROM maven:3.8.1-jdk-8
RUN echo "Asia/Shanghai" > /etc/timezone && \
dpkg-reconfigure --frontend noninteractive tzdata
VOLUME /tmp
ADD ./output/ /app/
ENV TZ=Asia/Shanghai
ENV PARAMS=""
ENTRYPOINT ["sh","-c","java -jar /app/app.jar $PARAMS"]
Jenkinsfile配置
#!/usr/bin/env groovy
final def releaseTag = (env.TAG_NAME ?: env.BRANCH_NAME).replace('/', '-')
pipeline {
agent {
kubernetes {
label "${UUID.randomUUID().toString()}"
yaml '''
metadata:
labels:
some-label: some-label-value
class: KubernetesDeclarativeAgentTest
namespace: default
spec:
nodeSelector:
jenkins-ci: "true"
containers:
- name: base
image: ubuntu:20.04
command:
- "cat"
tty: true
env:
- name: CONTAINER_ENV_VAR
value: prepare
lifecycle:
postStart:
exec:
command: ["/bin/bash", "-c", "apt update;apt install gettext -y"]
volumeMounts:
- name: docker-bin
mountPath: /usr/bin/docker
- name: docker-sock
mountPath: /var/run/docker.sock
- name: docker-conf
mountPath: /root/.docker
- name: kubectl-bin
mountPath: /usr/bin/kubectl
- name: build
image: maven:3.8.1-jdk-8
command:
- cat
tty: true
env:
- name: CONTAINER_ENV_VAR
value: build
volumeMounts:
- mountPath: /root/.m2/repository
name: mvn-repository
volumes:
- name: docker-bin
hostPath:
path: /usr/bin/docker
- name: docker-sock
hostPath:
path: /var/run/docker.sock
- name: docker-conf
hostPath:
path: /root/.docker
- name: kubectl-bin
hostPath:
path: /usr/bin/kubectl
- name: mvn-repository
hostPath:
path: /var/mvn/.m2/repository
'''
}
}
options {
gitLabConnection('gitlab.com')
}
stages {
stage('prepare') {
environment {
LANG = 'en_US.UTF-8'
}
steps {
container('base') {
echo "上报gitlab 编译信息..."
script {
updateGitlabCommitStatus name: "${releaseTag}", state: 'running'
}
}
}
}
stage('build') {
parallel {
stage("socket-netty-build") {
when {
tag comparator: 'REGEXP', pattern: '^socket-netty.*'
beforeAgent true
}
steps {
echo "releaseTag:${releaseTag}"
echo 'Building..'
container('build') {
sh 'mvn clean package -Dmaven.test.skip'
sh 'mkdir ./output'
sh 'mv ./target/socks5-netty-*-SNAPSHOT.jar ./output/app.jar'
}
}
}
}
}
stage('push') {
parallel {
stage("socket-netty-push") {
when {
tag comparator: 'REGEXP', pattern: '^socket-netty.*'
beforeAgent true
}
environment {
DOCKER_FILE_LOCATION = "./scm/Dockerfile"
IMAGE_URL = "xiaohunsdt/socket-netty:${releaseTag}"
}
steps {
container('base') {
sh '''
docker build -t $IMAGE_URL -f $DOCKER_FILE_LOCATION .
docker push $IMAGE_URL
'''
}
}
}
}
}
stage('deploy') {
parallel {
stage("socket-netty-deploy") {
when {
tag comparator: 'REGEXP', pattern: '^socket-netty.*'
beforeAgent true
}
environment {
K8S_FILE_LOCATION = './scm/k8s.yaml'
K8S_CONFIG = credentials('k8s-local')
APP_VERSION = "${releaseTag}"
IMAGE_URL = "xiaohunsdt/socket-netty:${releaseTag}"
}
steps {
container('base') {
sh '''
envsubst < $K8S_FILE_LOCATION | kubectl apply --kubeconfig $K8S_CONFIG -f -
'''
}
}
}
}
}
}
post {
failure {
container('prepare') {
script {
updateGitlabCommitStatus name: "${releaseTag}", state: 'failed'
// send_mail()
}
}
}
success {
container('prepare') {
script {
updateGitlabCommitStatus name: "${releaseTag}", state: 'success'
}
}
}
unstable {
container('prepare') {
script {
updateGitlabCommitStatus name: "${releaseTag}", state: 'success'
// send_mail()
}
}
}
aborted {
container('prepare') {
script {
updateGitlabCommitStatus name: "${releaseTag}", state: 'canceled'
// send_mail()
}
}
}
}
}
def send_mail() {
emailext (
attachLog: true,
subject: "${env.JOB_NAME} - Build # ${env.BUILD_NUMBER} - ${currentBuild.currentResult} !",
body: """
Check the info below to get more information
Job Link: ${env.BUILD_URL}
Branch/Tag Name: ${env.BRANCH_NAME}
Job Name: ${env.JOB_NAME}
Build Number: ${env.BUILD_NUMBER}
Result: ${currentBuild.currentResult}
""",
to: "825292796@qq.com",
from: "825292796@qq.com"
)
}
k8s部署文件
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: default
name: socket-netty-deployment
labels:
app: socket-netty
appVersion: $APP_VERSION
spec:
replicas: 2
selector:
matchLabels:
app: socket-netty
template:
metadata:
labels:
app: socket-netty
appVersion: $APP_VERSION
spec:
containers:
- name: socket-netty
image: $IMAGE_URL
imagePullPolicy: IfNotPresent
ports:
- containerPort: 11996
protocol: TCP
- containerPort: 11996
protocol: UDP
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
tolerations:
- effect: NoSchedule
key: project
operator: Equal
value: aidi
---
apiVersion: v1
kind: Service
metadata:
name: socket-netty-service
spec:
type: NodePort
selector:
app: socket-netty
ports:
- name: socket-tcp
protocol: TCP
port: 11996
targetPort: 11996
nodePort: 31996
- name: socket-upd
protocol: UDP
port: 11996
targetPort: 11996
nodePort: 31996
|