记录笔记
一、后端服务部署模板
1、发布部署文件模板
apiVersion: apps/v1
kind: Deployment
metadata:
name: ibuilds-v3-$APP_NAME
namespace: $PROJECT_NAMESPACE
spec:
replicas: 1
selector:
matchLabels:
app: ibuilds-v3-$APP_NAME
template:
metadata:
labels:
app: ibuilds-v3-$APP_NAME
spec:
nodeSelector:
app: ibuild-v3
containers:
- name: ibuilds-v3-$APP_NAME
image: $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:$BRANCH_NAME-$BUILD_NUMBER
env:
- name: ENV
value: $DEPLOY_ENV
command: ["java", "-jar", "-Dspring.profiles.active=$(ENV)", "/app/app.jar"]
volumeMounts:
- name: $APP_NAME-log
mountPath: /app/logs
- name: localtime
mountPath: /etc/localtime
- name: ibuilds-data-ca
mountPath: /data/docker/ibuilds-data/file
ports:
- name: port8082
containerPort: 8082
#- name: port8083
# containerPort: 8083
# livenessProbe:
# httpGet:
# path: /api/common/health
# port: port8080
# initialDelaySeconds: 60
# periodSeconds: 10
# readinessProbe:
# httpGet:
# path: /api/common/health
# port: port8080
# initialDelaySeconds: 60
# periodSeconds: 10
resources:
requests:
cpu: 300m
memory: 1Gi
limits:
cpu: 500m
memory: 2Gi
imagePullSecrets:
- name: habor-repo
volumes:
- name: $APP_NAME-log
hostPath:
path: /data/logs/$APP_NAME
type: DirectoryOrCreate
- name: localtime
hostPath:
path: /etc/localtime
- name: ibuilds-data-file
persistentVolumeClaim:
claimName: ibuilds-file-rwx
---
apiVersion: v1
kind: Service
metadata:
name: svc-ibuilds-v3-$APP_NAME
namespace: $PROJECT_NAMESPACE
labels:
app: ibuilds-v3-$APP_NAME
spec:
#type: ClusterIP
type: NodePort
ports:
- name: port8082
port: 8082
targetPort: 8082
nodePort: $NODEPORT
#- name: port8083
# port: 8083
# targetPort: 8083
#nodePort: 30083
selector:
app: ibuilds-v3-$APP_NAME
2、发布流水线脚本模板
pipeline {
agent {
label 'maven'
}
stages {
stage('checkout scm') {
steps {
script {
git(branch: "$BRANCH_NAME", url: "$GIT_URL", credentialsId: 'gitlab-tangxuan', changelog: true, poll: false)
sh "echo ${params.DeployMode}"
if (params.DeployMode == 'dev') {
PROJECT_NAMESPACE = "dev"
DEPLOY_ENV = "dev2"
NODEPORT = "38082"
} else if (params.DeployMode == 'uat') {
PROJECT_NAMESPACE = "uat"
DEPLOY_ENV = "uat"
NODEPORT = "48082"
}
}
}
}
stage('build & push') {
agent none
steps {
container('maven') {
sh 'mvn -Dmaven.test.skip=true clean package'
withCredentials([usernamePassword(credentialsId : 'harbor-repo' ,passwordVariable : 'DOCKER_PASSWORD' ,usernameVariable : 'DOCKER_USERNAME' ,)]) {
sh 'echo "$DOCKER_PASSWORD" | docker login $REGISTRY -u "$DOCKER_USERNAME" --password-stdin'
sh 'docker build -t $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:$BRANCH_NAME-$BUILD_NUMBER .'
sh 'docker push $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:$BRANCH_NAME-$BUILD_NUMBER'
}
}
}
}
stage('deploy to dev') {
agent none
when {
expression {
params.DeployMode == 'dev'
}
}
steps {
container('maven') {
withCredentials([
kubeconfigFile(
credentialsId: env.KUBECONFIG_CREDENTIAL_ID,
variable: 'KUBECONFIG')
]) {
//sh "echo $PROJECT_NAMESPACE $DEPLOY_ENV"
sh "sed -i 's/\$PROJECT_NAMESPACE/${PROJECT_NAMESPACE}/' k8s.yaml"
sh "sed -i 's/\$DEPLOY_ENV/${DEPLOY_ENV}/' k8s.yaml"
sh "sed -i 's/\$NODEPORT/${NODEPORT}/' k8s.yaml"
sh "envsubst < k8s.yaml | cat -"
sh 'envsubst < k8s.yaml | kubectl apply -f -'
}
}
}
}
stage('deploy to uat') {
agent none
when {
expression {
params.DeployMode == 'uat'
}
}
steps {
container('maven') {
withCredentials([
kubeconfigFile(
credentialsId: env.KUBECONFIG_CREDENTIAL_ID,
variable: 'KUBECONFIG')
]) {
//sh "echo $PROJECT_NAMESPACE $DEPLOY_ENV"
sh "sed -i 's/\$PROJECT_NAMESPACE/${PROJECT_NAMESPACE}/' k8s.yaml"
sh "sed -i 's/\$DEPLOY_ENV/${DEPLOY_ENV}/' k8s.yaml"
sh "sed -i 's/\$NODEPORT/${NODEPORT}/' k8s.yaml"
sh "envsubst < k8s.yaml | cat -"
sh 'envsubst < k8s.yaml | kubectl apply -f -'
}
}
}
}
}
environment {
DOCKER_CREDENTIAL_ID = 'harbor-repo'
KUBECONFIG_CREDENTIAL_ID = 'kubeconfig-id'
REGISTRY = '192.168.1.137:8011'
DOCKERHUB_NAMESPACE = 'devops'
//要修改
GIT_URL = 'https://git.com/ibuilds-data.git'
APP_NAME = 'ibuilds-data'
BRANCH_NAME = "${params.deployBranch}"
}
parameters {
choice(name: 'DeployMode', choices:'dev\nuat', description:'选择发布的环境')
string(name: 'deployBranch', defaultValue: 'uat', description: '请输入git仓库分支,默认uat分支')
}
}
二、前端服务部署模板
1、k8s.yaml部署文件
apiVersion: apps/v1
kind: Deployment
metadata:
name: ibuilds-v3-$APP_NAME
namespace: $PROJECT_NAMESPACE
spec:
replicas: 1
selector:
matchLabels:
app: ibuilds-v3-$APP_NAME
template:
metadata:
labels:
app: ibuilds-v3-$APP_NAME
spec:
nodeSelector:
app: ibuild-v3
containers:
- name: ibuilds-v3-$APP_NAME
image: $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:$BRANCH_NAME-$BUILD_NUMBER
env:
- name: ENV
value: $DEPLOY_ENV
volumeMounts:
- name: ibuilds-file
mountPath: /data/project/ibuilds-v2/file
- name: localtime
mountPath: /etc/localtime
- name: nginx-conf
mountPath: /etc/nginx/conf.d/default.conf
subPath: default.conf
ports:
- name: port80
containerPort: 80
#- name: port8083
# containerPort: 8083
# livenessProbe:
# httpGet:
# path: /api/common/health
# port: port80
# initialDelaySeconds: 60
# periodSeconds: 10
# readinessProbe:
# httpGet:
# path: /api/common/health
# port: port80
# initialDelaySeconds: 60
# periodSeconds: 10
resources:
requests:
cpu: 100m
memory: 100Mi
limits:
cpu: 300m
memory: 300Mi
imagePullSecrets:
- name: habor-repo
volumes:
- name: nginx-conf
configMap:
name: 80-config
defaultMode: 420
- name: localtime
hostPath:
path: /etc/localtime
type: ''
- name: ibuilds-file
persistentVolumeClaim:
claimName: ibuilds-file-rwx
---
apiVersion: v1
kind: Service
metadata:
name: svc-ibuilds-v3-$APP_NAME
namespace: $PROJECT_NAMESPACE
labels:
app: ibuilds-v3-$APP_NAME
spec:
type: ClusterIP
#type: NodePort
ports:
- name: port80
port: 80
targetPort: 80
#nodePort: 308087
selector:
app: ibuilds-v3-$APP_NAME
2、流水线脚本
pipeline {
agent {
label 'nodejs'
}
stages {
stage('拉取') {
agent none
steps {
script {
container('nodejs') {
git(url: "$GIT_URL", credentialsId: 'gitlab-tangxuan', branch: "$BRANCH_NAME", changelog: true, poll: false)
sh 'ls '
if (params.DeployMode == 'dev') {
PROJECT_NAMESPACE = "dev"
//DEPLOY_ENV = "dev2"
} else if (params.DeployMode == 'uat') {
PROJECT_NAMESPACE = "uat"
//DEPLOY_ENV = "uat"
}
}
}
}
}
stage('编译') {
agent none
steps {
container('nodejs') {
sh 'ls'
sh 'npm install --registry=https://registry.npm.taobao.org'
sh "npm run build:prod"
}
}
}
stage('打包镜像') {
agent none
steps {
container('nodejs') {
withCredentials([usernamePassword(credentialsId : 'harbor-repo' ,passwordVariable : 'DOCKER_PASSWORD' ,usernameVariable : 'DOCKER_USERNAME' ,)]) {
sh 'ls '
sh 'echo "$DOCKER_PASSWORD" | docker login $REGISTRY -u "$DOCKER_USERNAME" --password-stdin'
sh "docker build -t $APP_NAME:latest ."
}
}
}
}
stage('推送仓库') {
agent none
steps {
container('nodejs') {
withCredentials([usernamePassword(credentialsId : 'harbor-repo' ,passwordVariable : 'DOCKER_PASSWORD' ,usernameVariable : 'DOCKER_USERNAME' ,)]) {
sh "docker tag $APP_NAME:latest $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:$BRANCH_NAME-$BUILD_NUMBER"
sh 'echo "$DOCKER_PASSWORD" | docker login $REGISTRY -u "$DOCKER_USERNAME" --password-stdin'
sh 'docker push $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:$BRANCH_NAME-$BUILD_NUMBER'
}
}
}
}
stage('deploy to dev') {
agent none
when {
expression {
params.DeployMode == 'dev'
}
}
steps {
container('nodejs') {
withCredentials([
kubeconfigFile(
credentialsId: env.KUBECONFIG_CREDENTIAL_ID,
variable: 'KUBECONFIG')
]) {
sh "sed -i 's/\$PROJECT_NAMESPACE/${PROJECT_NAMESPACE}/' k8s.yaml"
sh 'envsubst < k8s.yaml | kubectl apply -f -'
}
}
}
}
stage('deploy to uat') {
agent none
when {
expression {
params.DeployMode == 'uat'
}
}
steps {
container('nodejs') {
withCredentials([
kubeconfigFile(
credentialsId: env.KUBECONFIG_CREDENTIAL_ID,
variable: 'KUBECONFIG')
]) {
sh "sed -i 's/\$PROJECT_NAMESPACE/${PROJECT_NAMESPACE}/' k8s.yaml"
sh 'envsubst < k8s.yaml | kubectl apply -f -'
}
}
}
}
}
environment {
DOCKER_CREDENTIAL_ID = 'harbor-repo'
KUBECONFIG_CREDENTIAL_ID = 'kubeconfig-id'
REGISTRY = '192.168.1.137:8011'
DOCKERHUB_NAMESPACE = 'devops'
//要修改
GIT_URL = 'https://gitlab.com/ibuilds-web.git'
APP_NAME = 'ibuilds-web'
BRANCH_NAME = "${params.deployBranch}"
}
parameters {
choice(name: 'DeployMode', choices:'dev\nuat', description:'选择发布的环境')
string(name: 'deployBranch', defaultValue: 'test', description: '请输入git仓库分支,默认test分支')
}
}