使用playwright脚本实现jenkins构建项目
1. 需要安装node环境,并安装playwright依赖
2. node <file.name> -projects=<pname1, pname2...> -url=<jenkins_url> -user=<jenkins_user> -pwd= <jenkins_pwd>
1 const { chromium } = require('playwright-chromium'); 2 3 4 (async () => { 5 6 let printHelpInfo = () =>{ 7 console.log('=========================help info=========================') 8 console.log('-projects', 'setting built project name, multis use "," separated', `for example: '-projects="a,b"'`) 9 //console.log('-delay', 'setting built delay seconds, default is 0', `for example: '-delay=5'`) 10 console.log('-url', 'setting built base url, default is http://192.168.0.40:9876', `for example: '-url=http://192.168.0.40:9876'`) 11 console.log('-user', 'setting built login user, default is admin', `for example: '-user=anyone'`) 12 console.log('-pwd', 'setting built login password, default is 123456', `for example: '-pwd=123456'`) 13 console.log('=========================help info=========================') 14 } 15 16 const defaultParams = { 17 '-user': 'jenkins_username', 18 '-pwd': 'jenkins_password', 19 '-url': 'jenkins_url', 20 //'-delay': 0, 21 } 22 23 const [node, path, ...args] = process.argv; 24 console.log('>>> input args=', args) 25 26 // help info 27 if (['-?', '-h', 'help', '-help', ''].includes((args[0] && args[0].trim()) || '')) { 28 printHelpInfo() 29 return ; 30 } 31 32 let argv = {} 33 args.forEach(arg =>{ 34 let pos = -1 35 if ((pos = arg.indexOf('=')) > -1) { 36 let key = arg.substring(0, pos), value = arg.substring(pos + 1) 37 argv[key] = value 38 } 39 }) 40 41 argv = Object.assign({}, defaultParams, argv) 42 console.log('>>> params=', argv) 43 if (argv['-projects'] == undefined || argv['-projects'] == false) { 44 console.error('project name is null', 'resolve: use "-projects=XX" Enter at least one project name, multis please use "," separated') 45 return 46 } 47 48 const username = argv['-user'] 49 const password = argv['-pwd'] 50 let baseurl = argv['-url'] 51 //let delaySec = argv['-delay'] 52 baseurl = baseurl.substring(baseurl.length-1) == '/' ? baseurl : baseurl + '/' 53 //let buildTemplateUrl = baseurl + 'job/{projectName}/build?delay={delaySec}sec' 54 55 let projectNames = argv['-projects'].split(',') 56 57 // do login 58 const browser = await chromium.launch({ headless: false} ); 59 const context = await browser.newContext(); 60 const page = await context.newPage(); 61 await page.goto(baseurl); 62 await page.fill('input[type="text"]', username) 63 await page.fill('input[type="password"]', password) 64 await page.click('input[type="submit"]') 65 66 for (let idx = 0; idx < projectNames.length; idx++) { 67 let name = projectNames[idx].trim() 68 //let buildUrl = buildTemplateUrl.replace('{projectName}', name) 69 //buildUrl = buildUrl.replace('{delaySec}', delaySec) 70 console.log(`>>>>reday to build [${name}]`) 71 await page.click(`a[href="job/${name}/build?delay=0sec"]`) 72 console.log(`>>>>built [${name}] done...`) 73 } 74 75 console.log(`>>>>done...`, 'ctrl + c to exit') 76 // await browser.close(); 77 })();