Jenkins-在Centos上配置自動化部署(Jenkins+Gitlab+Rancher)

Jenkins-在Centos上配置自動化部署(Jenkins+Gitlab+Rancher)

環境:centos7html

首先在服務器上安裝好Jenkins和Gitlab和Ranchernode

Gitlab安裝:http://www.javashuo.com/article/p-bdwquasu-hb.htmlweb

Jenkins安裝:http://www.javashuo.com/article/p-uizxhgev-gt.htmldocker

 Rancher安裝:http://www.javashuo.com/article/p-bbdatcwt-m.htmlshell

 

打開Jenkins,地址:http://192.168.122.199:8078/json

 

 

 

 

添加憑據 centos

 

 

 增長全局憑據bash

類型:SSH類型服務器

username:自定義app

Key:C:\Users\lihongyuan\.ssh\id_rsa(如何生成 SSH 密鑰可在網上查到)

 

 

  肯定後,添加成功

 

 

 

  

建立一個新任務

 

 

 

項目配置General

 

 

 

配置源碼管理

注意提早安裝GitLab 插件。 

Repository URL:GitLab中的項目地址

credentials:憑據

獲取代碼的分支 此處以 master 爲例

 

 

 

構建觸發器

記下 這個地址,配置到 GitLab中 (當有分支有提交時,自動推送代碼過來,自動構建) 

GitLab 中的配置方式:http://www.javashuo.com/article/p-bdwquasu-hb.html

 

 

  

點擊高級,生成密鑰,密鑰配置到 GitLab中 (當有分支有提交時,自動推送代碼過來,自動構建) 

 

構建 執行shell腳本。

需安裝dotnet core 環境:http://www.javashuo.com/article/p-skantnaw-gp.html

也可在服務器上 /var/lib/jenkins/workspace 目錄下 運行下面的語句。

 

 

 

 

 

 

 

Jenkins在shell腳本運行docker權限報錯解決,參考:http://www.javashuo.com/article/p-dwmhopex-gk.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調用的語句,請查詢:http://www.javashuo.com/article/p-bbdatcwt-m.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項目從新部署。

相關文章
相關標籤/搜索