Jenkins-在Centos上配置自动化部署(Jenkins+Gitlab+Rancher)
Jenkins-在Centos上配置自动化部署(Jenkins+Gitlab+Rancher)
环境:centos7
首先在服务器上安装好Jenkins和Gitlab和Rancher
Gitlab安装:https://www.cnblogs.com/1285026182YUAN/p/10726539.html
Jenkins安装:https://www.cnblogs.com/1285026182YUAN/p/11791171.html
Rancher安装:https://www.cnblogs.com/1285026182YUAN/p/11546743.html
打开Jenkins,地址:http://192.168.122.199:8078/
添加凭据 ,也可以在创建项目时直接添加 gitlab 的账号密码凭据
增加全局凭据
类型:SSH类型
username:自定义
Key:C:\Users\lihongyuan\.ssh\id_rsa(如何生成 SSH 密钥可在网上查到)
确定后,添加成功
创建一个新任务
项目配置General
配置源码管理
注意提前安装GitLab 插件。
Repository URL:GitLab中的项目地址
credentials:凭据
获取代码的分支 此处以 master 为例
构建触发器
记下 这个地址,配置到 GitLab中 (当有分支有提交时,自动推送代码过来,自动构建)
GitLab 中的配置方式:https://www.cnblogs.com/1285026182YUAN/p/10726539.html , https://www.cnblogs.com/1285026182YUAN/p/15497625.html
点击高级,生成密钥,密钥配置到 GitLab中 (当有分支有提交时,自动推送代码过来,自动构建)
构建 执行shell脚本。
需安装dotnet core 环境:https://www.cnblogs.com/1285026182YUAN/p/11833674.html
也可在服务器上 /var/lib/jenkins/workspace 目录下 运行下面的语句。
Jenkins 远程触发 403 No valid crumb was included in the request
在下面脚本命令行中输入hudson.security.csrf.GlobalCrumbIssuerConfiguration.DISABLE_CSRF_PROTECTION = true
Jenkins在shell脚本运行docker权限报错解决,参考:https://www.cnblogs.com/morang/p/9536622.html
Jenkins在执行shell脚本时可能会报类似如下的错误,是由于Jenkins没有docker权限
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.38/containers/create?name=my-node-8: dial unix /var/run/docker.sock: connect: permission denied.
解决方法
将jenkins用户加入docker组
重启Jenkins服务
sudo gpasswd -a jenkins docker
systemctl restart jenkins
查看权限组
cat /etc/group
OK 配置完成,可重新构建测试。
Jenkins自动部署Rancher:
获取Rancher API调用的语句,填写在Execute Shell 中。如下:
Rancher API调用的语句,请查询:https://www.cnblogs.com/1285026182YUAN/p/11546743.html
具体如下命令如下:
#!/bin/bash var=`date +%Y%m%d%H%M%S` #发布 dotnet publish ProjNetCore2/ProjNetCore2.csproj -c Release -o ../publish/ #打镜像 docker build -t mestest.cn:8085/proj_netcore2:$var publish/. #tag docker tag mestest.cn:8085/proj_netcore2:$var mestest.cn:8085/proj_netcore2:latest #登录 docker login -p 123456 -u admin mestest.cn:8085 #推送 docker push mestest.cn:8085/proj_netcore2:$var docker push mestest.cn:8085/proj_netcore2:latest curl -k -u "token-lgwv7:dvrkw5498mhlpczf8zn2sl99bbfgq69qhcfl2wblt4qtgsrhdh6fws" \ -X PUT \ -H 'Accept: application/json' \ -H 'Content-Type: application/json' \ -d '{ "annotations": { "cattle.io/timestamp": "2019-11-21T00:58:54Z", "workload.cattle.io/state": "{\"bWVzdGVzdA==\":\"c-gqr2n:m-8e84afdc263f\"}" }, "containers": [{ "allowPrivilegeEscalation": false, "image": "mestest.cn:8085/proj_netcore2:'$var'", "imagePullPolicy": "IfNotPresent", "initContainer": false, "name": "mycoreweb", "ports": [{ "containerPort": 80, "dnsName": "mycoreweb-nodeport", "kind": "NodePort", "name": "8089tcp300051", "protocol": "TCP", "sourcePort": 30005, "type": "/v3/project/schemas/containerPort" }], "privileged": false, "readOnly": false, "resources": { "type": "/v3/project/schemas/resourceRequirements" }, "restartCount": 0, "runAsNonRoot": false, "stdin": true, "stdinOnce": false, "terminationMessagePath": "/dev/termination-log", "terminationMessagePolicy": "File", "tty": true, "type": "/v3/project/schemas/container" }], "created": "2019-11-13T05:21:50Z", "creatorId": null, "deploymentConfig": { "maxSurge": 1, "maxUnavailable": 0, "minReadySeconds": 0, "progressDeadlineSeconds": 600, "revisionHistoryLimit": 10, "strategy": "RollingUpdate" }, "deploymentStatus": { "availableReplicas": 1, "conditions": [{ "lastTransitionTime": "2019-11-20T12:05:09Z", "lastTransitionTimeTS": 1574251509000, "lastUpdateTime": "2019-11-20T12:05:09Z", "lastUpdateTimeTS": 1574251509000, "message": "Deployment has minimum availability.", "reason": "MinimumReplicasAvailable", "status": "True", "type": "Available" }, { "lastTransitionTime": "2019-11-21T00:43:28Z", "lastTransitionTimeTS": 1574297008000, "lastUpdateTime": "2019-11-21T00:58:56Z", "lastUpdateTimeTS": 1574297936000, "message": "ReplicaSet \"mycoreweb-74797bc87\" has successfully progressed.", "reason": "NewReplicaSetAvailable", "status": "True", "type": "Progressing" }], "observedGeneration": 28, "readyReplicas": 1, "replicas": 1, "type": "/v3/project/schemas/deploymentStatus", "unavailableReplicas": 0, "updatedReplicas": 1 }, "dnsConfig": { "type": "/v3/project/schemas/podDNSConfig" }, "dnsPolicy": "ClusterFirst", "gids": [], "hostAliases": [], "hostIPC": false, "hostNetwork": false, "hostPID": false, "imagePullSecrets": [], "labels": { "workload.user.cattle.io/workloadselector": "deployment-default-mycoreweb" }, "name": "mycoreweb", "namespaceId": "default", "nodeId": "", "ownerReferences": [], "paused": false, "projectId": "c-gqr2n:p-4zqh9", "publicEndpoints": [], "readinessGates": [], "restartPolicy": "Always", "scale": 1, "schedulerName": "default-scheduler", "scheduling": { "node": { "nodeId": "c-gqr2n:m-8e84afdc263f" } }, "selector": { "matchLabels": { "workload.user.cattle.io/workloadselector": "deployment-default-mycoreweb" }, "type": "/v3/project/schemas/labelSelector" }, "state": "active", "sysctls": [], "terminationGracePeriodSeconds": 30, "transitioning": "no", "transitioningMessage": "", "uuid": "7ede4f06-05d5-11ea-b16e-000c296c6b53", "volumes": [], "workloadAnnotations": { "deployment.kubernetes.io/revision": "11", "field.cattle.io/creatorId": "user-257tk" }, "workloadLabels": { "cattle.io/creator": "norman", "workload.user.cattle.io/workloadselector": "deployment-default-mycoreweb" }, "workloadMetrics": [] }' \ 'https://192.168.122.199:8443/v3/project/c-gqr2n:p-4zqh9/workloads/deployment:default:mycoreweb'
保存。
当Jenkins重新构建时,会自动调用Rancher的API,使Rancher项目重新部署。