wangming1993 / issues

记录学习中的一些问题,体会与心得 https://wangming1993.github.io/issues
8 stars 4 forks source link

使用 curl 触发 Jenkins 部署 #79

Open wangming1993 opened 6 years ago

wangming1993 commented 6 years ago
#!/bin/bash

USER=''
API_TOKEN=''
Jenkins_IP='http://127.0.0.1:8080'

CRUMB=$(curl -s "http://${USER}:${API_TOKEN}@${Jenkins_IP}//crumbIssuer/api/xml?xpath=concat(//crumbRequestField,\":\",//crumb)")

trigger_build () {
    jobName=$1
    service=$2

    data="{\"parameter\": [
        {\"name\":\"SERVICE\", \"value\":\"${service}\"}
    ]}"

    curl -X POST "http://${Jenkins_IP}/job/${jobName}/job/${BRANCH}/build" \
        --user "${USER}:${API_TOKEN}" \
        -H ${CRUMB} \
        --data-urlencode json="${data}"
}

这里需要注意的是:

如果 build 的路径是 http://${Jenkins_IP}/job/${jobName}/build

那么

data="{\"parameter\": [
        {\"name\":\"SERVICE\", \"value\":\"${service}\"},
        {\"name\":\"BRANCH\", \"value\":\"${branch}\"}
]}"