Jenkins脚本式pipeline笔记

1- 硬杀job任务

POST http://jenkins-url/job/job-name/job-nubmer/kill

2- Jenkins构建页面获取git分支或tag

import jenkins.model.*
//jenkins凭证名字
credentialsId = 'xxxxx'

try {
    def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
        com.cloudbees.plugins.credentials.common.StandardUsernameCredentials.class, Jenkins.instance, null, null ).find {
            it.id == credentialsId
    }

    def user = creds.username.replaceAll('@', '%40')
    def pass = creds.password.toString().replaceAll('@', '%40')

    def cmd = "git ls-remote -h -t https://${user}:${pass}@xxx.gitlab.com/ce/repo-code-for-app.git"
    def proc = cmd.execute()
    proc.waitFor()
    
    def branchs = proc.in.text.readLines().collect {
        it.replaceAll("[0-9a-z]*\trefs/[a-z]*/", '')
    }
    return branchs.findAll{
         it[-3..-1] != '^{}'
    }.sort{ a,b-> 
         return -a.compareTo(b) 
    } 
} catch (Exception e) {
    return ["Exception"]
}

3- Jenkins读取job列表

import jenkins.model.Jenkins
import hudson.model.Job

Jenkins.instance.allItems(Job).findAll{
  it ->it.fullName.startsWith('mydefined-prefix-') && it.fullName.split('-').size()>3
}.collect{
	job -> job.fullName
}

4- 下载Job所有配置

        4.1 安装工具jq

                sudo apt-get install jq

        4.2 脚本执行

#!/bin/bash
authKey='[uername]:[password]'
JENKINS_URL='http--x---'
vm_jenkins_list=`curl -u $authKey -X GET $JENKINS_URL/view/all/api/json?tree=jobs[name] | jq -r .jobs[].name`
echo $vm_jenkins_list>fetch_vm/all.name.list.txt
for jobname in $vm_jenkins_list;
do
   echo $jobname
   curl -X GET -u $authKey $JENKINS_URL/job/$jobname/config.xml -o fetch_vm/$jobname-config.xml
done

5- npm构建报错

    5.1  Jenkins容器jnlp的.ssh/config导致问题,配置:

## .ssh/config
StrictHostKeyChecking no
UserKnownHostsFile /dev/null

报错

npm ERR! Permission denied (publickey).
npm ERR! fatal: Could not read from remote repository.
npm ERR! 
npm ERR! Please make sure you have the correct access rights
npm ERR! and the repository exists.
npm ERR! 
npm ERR! exited with error code: 128

解决方法:

export StrictHostKeyChecking=ask
npm install

   

5.2   git协议问题

npm ERR! /usr/bin/git ls-remote -h -t git://github.com/adobe-webplatform/eve.git
npm ERR! 
npm ERR! fatal: remote error: 
npm ERR!   The unauthenticated git protocol on port 9418 is no longer supported.

解决方法:

git config --global url."https://".insteadOf git://
npm install

你可能感兴趣的:(jenkins,git,运维)