https://plugins.jenkins.io/kubernetes/

    test
    jenkins 连接kubernets配置
    使用证书连接:
    通过 ~/.kube/config 生成 cert.pfx,jenkins通过 cert.pfx来连接k8s
    0、生成ca.crt
    echo “ “| base64 -d > ~/ca.crt (~/.kube/config中certificate-authority-data的内容)
    1、生成client.key
    echo “ “| base64 -d > ~/client.key (~/.kube/config中client-key-data的内容)
    2、生成client.crt
    echo “ “ |base64 -d > ~/client.crt (~/.kube/config中client-certificate-data的内容)
    3、生成cert.pfx
    openssl pkcs12 -export -inkey client.key -in client.crt -certfile ca.crt -out cert.pfx
    4、jenkins 上传pkcs12文件

    1. https://plugins.jenkins.io/kubernetes/
    2. test
    3. jenkins 连接kubernets配置
    4. 使用证书连接:
    5. 通过 ~/.kube/config 生成 cert.pfxjenkins通过 cert.pfx来连接k8s
    6. 0、生成ca.crt
    7. echo " "| base64 -d > ~/ca.crt (~/.kube/configcertificate-authority-data的内容)
    8. 1、生成client.key
    9. echo " "| base64 -d > ~/client.key (~/.kube/configclient-key-data的内容)
    10. 2、生成client.crt
    11. echo " " |base64 -d > ~/client.crt (~/.kube/configclient-certificate-data的内容)
    12. 3、生成cert.pfx
    13. openssl pkcs12 -export -inkey client.key -in client.crt -certfile ca.crt -out cert.pfx
    14. 4jenkins 上传pkcs12文件
    15. pod模板:
    16. #!groovy
    17. // Scripted Pipeline
    18. podTemplate(yaml: """
    19. kind: Pod
    20. spec:
    21. containers:
    22. - name: jnlp
    23. image: '172.16.101.214:5000/jenkins-slave:test'
    24. imagePullPolicy: Always
    25. args: ['\$(JENKINS_SECRET)', '\$(JENKINS_NAME)']
    26. resources:
    27. limits:
    28. memory: "512Mi"
    29. cpu: "1000m"
    30. requests:
    31. memory: "256Mi"
    32. cpu: "500m"
    33. - name: npm
    34. image: 172.16.101.214:5000/node:stretch
    35. imagePullPolicy: Always
    36. command: ['cat']
    37. tty: true
    38. resources:
    39. limits:
    40. memory: "3072Mi"
    41. cpu: "2"
    42. requests:
    43. memory: "1024Mi"
    44. cpu: "1"
    45. - name: maven
    46. image: 172.16.101.214:5000/mvn:3.3.9-jdk-8-alpine
    47. imagePullPolicy: Always
    48. command: ['cat']
    49. tty: true
    50. resources:
    51. limits:
    52. memory: "3072Mi"
    53. cpu: "2"
    54. requests:
    55. memory: "1024Mi"
    56. cpu: "1"
    57. - name: kaniko
    58. image: 172.16.101.214:5000/kaniko-executor:debug-v0.24.0
    59. imagePullPolicy: IfNotPresent
    60. command: ['/busybox/cat']
    61. tty: true
    62. resources:
    63. limits:
    64. memory: "512Mi"
    65. cpu: "1000m"
    66. requests:
    67. memory: "256Mi"
    68. cpu: "500m"
    69. volumeMounts:
    70. - name: jenkins-docker-cfg
    71. mountPath: /kaniko/.docker
    72. imagePullSecrets:
    73. - name: ci-nexus-cred
    74. volumes:
    75. - name: jenkins-docker-cfg
    76. configMap:
    77. name: nexus-cred
    78. """
    79. ){
    80. // Pipeline Parameters
    81. properties([
    82. parameters([
    83. gitParameter(name: 'BRANCH_XLPAY_ADMIN',
    84. description: '选择xlpay-admin代码分支',
    85. branchFilter: 'origin/(.*)',
    86. type: 'PT_BRANCH_TAG',
    87. defaultValue: 'master',
    88. useRepository: '.*xlpay.git',
    89. sortMode: 'ASCENDING'),
    90. gitParameter(name: 'BRANCH_PAY_TRUSTLINK_DATA',
    91. description: '选择pay-trustlink-data分支',
    92. branchFilter: 'origin/(.*)',
    93. type: 'PT_BRANCH_TAG',
    94. defaultValue: 'master',
    95. useRepository: '.*pay-trustlink-data.git',
    96. sortMode: 'ASCENDING'),
    97. gitParameter(name: 'BRANCH_PAY_WEB',
    98. description: '选择pay-web分支',
    99. branchFilter: 'origin/(.*)',
    100. type: 'PT_BRANCH_TAG',
    101. defaultValue: 'master',
    102. useRepository: '.*pay-web.git',
    103. sortMode: 'ASCENDING'),
    104. choice(choices: ['test', 'prod'], description: '配置版本', name: 'CONFIG'),
    105. string(defaultValue: 'xyf', description: '部署namespace名称', name: 'NAMESPACE', trim: false),
    106. extendedChoice(defaultValue: 'xlpay_admin,xlpay_pay_user,pay_trustlink_data,pay_web', description: '选择需要部署的组件', descriptionPropertyValue: '部署xlpay_admin,部署xlpay_pay_user,部署pay_trustlink_data,部署pay_web', multiSelectDelimiter: ',', name: 'deploy_list', quoteValue: false, saveJSONParameterToFile: false, type: 'PT_CHECKBOX', value: 'xlpay_admin,xlpay_pay_user,pay_trustlink_data,pay_web', visibleItemCount: 4)
    107. ])
    108. ])
    109. node(POD_LABEL) {
    110. env.IMAGE_TAG_XLPAY_ADMIN = 'latest'
    111. if (params.BRANCH_XLPAY_ADMIN != 'master') {
    112. env.IMAGE_TAG_XLPAY_ADMIN = "${params.BRANCH_XLPAY_ADMIN}"
    113. }
    114. env.IMAGE_TAG_PAY_TRUSTLINK_DATA = 'latest'
    115. if (params.BRANCH_PAY_TRUSTLINK_DATA != 'master') {
    116. env.IMAGE_TAG_PAY_TRUSTLINK_DATA = "${params.BRANCH_PAY_TRUSTLINK_DATA}"
    117. }
    118. env.IMAGE_TAG_PAY_WEB = 'latest'
    119. if (params.BRANCH_PAY_WEB != 'master') {
    120. env.IMAGE_TAG_PAY_WEB = "${params.BRANCH_PAY_WEB}"
    121. }
    122. env.NAMESPACE = params.NAMESPACE
    123. env.CONFIG = params.CONFIG
    124. list = env.deploy_list
    125. echo list
    126. echo ","
    127. String[] str;
    128. str = list.split(",");
    129. str.each{
    130. echo it
    131. if ( it == 'xlpay_admin'){
    132. env.xlpay_admin=true
    133. echo "是否部署xlpay_admin:${xlpay_admin}"
    134. }
    135. if ( it == 'xlpay_pay_user'){
    136. env.xlpay_pay_user=true
    137. echo "是否部署xlpay_pay_user:${xlpay_pay_user}"
    138. }
    139. if ( it == 'pay_trustlink_data'){
    140. env.pay_trustlink_data=true
    141. echo "是否部署pay_trustlink_data:${pay_trustlink_data}"
    142. }
    143. if ( it == 'pay_web'){
    144. env.pay_web=true
    145. echo "是否部署pay_web:${pay_web}"
    146. }
    147. }
    148. stage('编译') {
    149. download_code:{
    150. container('maven'){
    151. checkout([$class: 'GitSCM',
    152. branches: [[name: "${params.BRANCH_XLPAY_ADMIN}"]],
    153. doGenerateSubmoduleConfigurations: false,
    154. extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'xlpay']],
    155. submoduleCfg: [],
    156. userRemoteConfigs: [[credentialsId: 'ted_gitlab', url: 'http://172.16.101.211/business-project/xlpay/xlpay.git']]
    157. ])
    158. checkout([$class: 'GitSCM',
    159. branches: [[name: "${params.BRANCH_PAY_TRUSTLINK_DATA}"]],
    160. doGenerateSubmoduleConfigurations: false,
    161. extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'pay-trustlink-data']],
    162. submoduleCfg: [],
    163. userRemoteConfigs: [[credentialsId: 'ted_gitlab', url: 'http://172.16.101.211/business-project/xlpay/pay-trustlink-data.git']]
    164. ])
    165. checkout([$class: 'GitSCM',
    166. branches: [[name: "${params.BRANCH_PAY_WEB}"]],
    167. doGenerateSubmoduleConfigurations: false,
    168. extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'pay-web']],
    169. submoduleCfg: [],
    170. userRemoteConfigs: [[credentialsId: 'ted_gitlab', url: 'http://172.16.101.211/business-project/xlpay/pay-web.git']]
    171. ])
    172. }
    173. }
    174. parallel build_xlpay_admin: {
    175. if (env.xlpay_admin == 'true' || env.xlpay_pay_user == 'true') {
    176. container('maven'){
    177. echo "====== [DEBUG]: Before building xlpay admin======"
    178. echo "Branch/Tag: ${params.BRANCH_XLPAY_ADMIN}"
    179. echo "Image tag: ${env.IMAGE_TAG_XLPAY_ADMIN}"
    180. sh 'cd xlpay;mvn clean install -U -Dmaven.test.skip=true'
    181. echo "====== [DEBUG]: After building ======"
    182. }
    183. }
    184. }, build_pay_trustlink_data: {
    185. if (env.pay_trustlink_data == 'true') {
    186. container('maven'){
    187. echo "====== [DEBUG]: Before building ======"
    188. echo "Branch/Tag: ${params.BRANCH_PAY_TRUSTLINK_DATA}"
    189. echo "Image tag: ${env.IMAGE_TAG_PAY_TRUSTLINK_DATA}"
    190. sh 'cd pay-trustlink-data;mvn clean install -U -Dmaven.test.skip=true'
    191. echo "====== [DEBUG]: After building ======"
    192. }
    193. }
    194. }, build_pay_web: {
    195. if (env.pay_web == 'true') {
    196. container('npm'){
    197. echo "====== [DEBUG]: Before building ======"
    198. echo "Branch/Tag: ${params.BRANCH_PAY_WEB}"
    199. echo "Image tag: ${env.BRANCH_PAY_WEB}"
    200. sh 'npm config set registry http://172.16.101.214:8081/repository/npm/'
    201. sh 'cd pay-web;ls;npm install;npm run build'
    202. echo "====== [DEBUG]: After building ======"
    203. }
    204. }
    205. }
    206. }
    207. stage('build Docker image'){
    208. build_image_xlpay_admin: {
    209. if (env.xlpay_admin == 'true') {
    210. container('kaniko') {
    211. sh "/kaniko/executor -f `pwd`/xlpay/xlpay-admin/Dockerfile -c `pwd`/xlpay/xlpay-admin --insecure --skip-tls-verify --cache=true --destination=172.16.101.214:5000/xlpay-admin:${IMAGE_TAG_XLPAY_ADMIN}"
    212. }
    213. }
    214. }
    215. build_image_xlpay_pay_user: {
    216. if (env.xlpay_pay_user == 'true') {
    217. container('kaniko') {
    218. sh "/kaniko/executor -f `pwd`/xlpay/xlpay-pay-user/Dockerfile -c `pwd`/xlpay/xlpay-pay-user --insecure --skip-tls-verify --cache=true --destination=172.16.101.214:5000/xlpay-pay-user:${IMAGE_TAG_XLPAY_ADMIN}"
    219. }
    220. }
    221. }
    222. buld_image_pay_trustlink_data: {
    223. if (env.pay_trustlink_data == 'true') {
    224. container('kaniko') {
    225. sh "/kaniko/executor -f `pwd`/pay-trustlink-data/Dockerfile -c `pwd`/pay-trustlink-data --insecure --skip-tls-verify --cache=true --destination=172.16.101.214:5000/pay-trustlink-data:${IMAGE_TAG_PAY_TRUSTLINK_DATA}"
    226. }
    227. }
    228. }
    229. build_image_pay_web: {
    230. if (env.pay_web == 'true') {
    231. container('kaniko') {
    232. sh "/kaniko/executor -f `pwd`/pay-web/Dockerfile -c `pwd`/pay-web --insecure --skip-tls-verify --cache=true --destination=172.16.101.214:5000/pay-web:${IMAGE_TAG_PAY_WEB}"
    233. }
    234. }
    235. }
    236. }//stage('build Docker image')
    237. stage('部署'){
    238. download_yaml: {
    239. checkout([$class: 'GitSCM',
    240. branches: [[name: "*/master"]],
    241. doGenerateSubmoduleConfigurations: false,
    242. extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'ci']],
    243. submoduleCfg: [],
    244. userRemoteConfigs: [[credentialsId: 'ted_gitlab', url: 'http://172.16.101.211/business-project/xlpay/ci.git']]
    245. ])
    246. }
    247. deploy_xlpay_admin: {
    248. if (env.xlpay_admin == 'true') {
    249. env.IMAGE_TAG=env.IMAGE_TAG_XLPAY_ADMIN
    250. container('jnlp') {
    251. sh "set +e;kubectl -n ${NAMESPACE} delete configmap xlpay-admin;set -e"
    252. sh "kubectl -n ${NAMESPACE} create configmap xlpay-admin --from-env-file=./ci/config/${CONFIG}/xlpay-admin.env"
    253. sh "envsubst < `pwd`/ci/deploy_yaml/xyf-xlpay-admin.yaml|kubectl -n ${NAMESPACE} apply -f -"
    254. }
    255. }
    256. }
    257. deploy_pay_trustlink_data: {
    258. if (env.pay_trustlink_data == 'true') {
    259. env.IMAGE_TAG=env.IMAGE_TAG_PAY_TRUSTLINK_DATA
    260. container('jnlp') {
    261. sh "set +e;kubectl -n ${NAMESPACE} delete configmap pay-trustlink-data;set -e"
    262. sh "kubectl -n ${NAMESPACE} create configmap pay-trustlink-data --from-env-file=./ci/config/${CONFIG}/pay-trustlink-data.env"
    263. sh "envsubst < `pwd`/ci/deploy_yaml/xyf-pay-trustlink-data.yaml|kubectl -n ${NAMESPACE} apply -f -"
    264. }
    265. }
    266. }
    267. deploy_xlpay_pay_user: {
    268. if (env.xlpay_pay_user == 'true') {
    269. env.IMAGE_TAG=env.IMAGE_TAG_XLPAY_ADMIN
    270. container('jnlp') {
    271. sh "set +e;kubectl -n ${NAMESPACE} delete configmap xlpay-pay-user;set -e"
    272. sh "kubectl -n ${NAMESPACE} create configmap xlpay-pay-user --from-env-file=./ci/config/${CONFIG}/xlpay-pay-user.env"
    273. sh "envsubst < `pwd`/ci/deploy_yaml/xyf-xlpay-pay-user.yaml|kubectl -n ${NAMESPACE} apply -f -"
    274. }
    275. }
    276. }
    277. deploy_pay_web: {
    278. if (env.pay_web == 'true') {
    279. env.IMAGE_TAG=env.IMAGE_TAG_PAY_WEB
    280. container('jnlp') {
    281. sh "envsubst < `pwd`/ci/deploy_yaml/xyf-pay-web.yaml|kubectl -n ${NAMESPACE} apply -f -"
    282. }
    283. }
    284. }
    285. if (env.CONFIG == 'prod') {
    286. deploy_ingress: {
    287. sh "kubectl -n ${NAMESPACE} apply -f `pwd`/ci/deploy_yaml/xyf-ingress.yaml"
    288. sh "sleep 10s"
    289. sh "kubectl -n ${NAMESPACE} get po -o wide"
    290. }
    291. }
    292. }//stage('部署')
    293. }//node(POD_LABEL)
    294. }//podTemplate