[Jenkins] 使用 python 讀取 RESTful API 回饋值
如果確認無法看到取得的值 要先確認一下 執行權限
先執行
chown jenkins:jenkins aaa.py
#!/usr/bin/env python
#-- coding: utf-8 --
#@File : get_tag.py
#@Author:
#@Date : 2020-03-17
import sys
import subprocess
user= 'xxxxx'
passwd = 'xxxxx'
hurl = 'https://172.19.4.166'
tag = sys.argv[1]
ttoken = '''curl -iksL -X GET -u %s:%s %s/service/token?account=%s\&service=harbor-registry\&scope=repository:java/%s:pull|grep "token" |awk -F '"' '{print $4}' ''' %(user,passwd,hurl,user,tag)
ttoken = subprocess.check_output(ttoken,shell=True).strip()
ttoken = str(ttoken,encoding='utf8')
tlist = '''curl -ksL -X GET -H "Content-Type: application/json" -H "Authorization: Bearer %s" %s/v2/java/%s/tags/list|awk -F '[' '{print $2}'|awk -F ']' '{print $1}'|sed 's/"//g' ''' %(ttoken,hurl,tag)
tlist = subprocess.check_output(tlist,shell=True).strip()
tlist = str(tlist,encoding='utf8')
tlist = tlist.split(",")
tlist.sort(reverse=True)
lst2 = tlist.__iter__()
while 1:
try:
n = lst2.__next__()
print(n)
except StopIteration:
break
沒有留言:
張貼留言