jenkins 发版获取镜像仓库中tag

2021-12-28  本文已影响0人  济南打工人

一、安装阿里云linux客户端工具

选择官网下载地址

# wget wget https://aliyuncli.alicdn.com/aliyun-cli-linux-latest-amd64.tgz

# tar -xf aliyun-cli-linux-latest-amd64.tgz

# cp aliyun /usr/local/bin

二、使用RAM进行子账号权限管理

创建RAM子账号,并对该子账号授权,记录账号的AccessKey ID等信息

权限名称
AliyunContainerRegistryFullAccess
AliyunContainerRegistryReadOnlyAccess

三、配置客户端工具

# aliyun configure
Configuring profile 'default' in 'AK' authenticate mode...
Access Key Id [*********************kCg]:
Access Key Secret [***************************2w7]:
Default Region Id [cn-hangzhou]:
Default Output Format [json]: json (Only support json)
Default Language [zh|en] zh:
Saving profile[default] ...Done.

四、获取镜像信息

aliyun cr GetRepoTags --help
阿里云CLI命令行工具 3.0.64

Product:     cr (容器镜像服务)
Method:      GET
PathPattern: /repos/[RepoNamespace]/[RepoName]/tags

Parameters:
  --RepoName      String  Required
  --RepoNamespace String  Required
  --Page          Integer Optional
  --PageSize      Integer Optional
# aliyun cr GetRepoTags --RepoNamespace xxxx --RepoName xxxxxxx
{
    "data": {
        "page": 1,
        "pageSize": 30,
        "tags": [
            {
                "digest": "c06e0fe7ca4759dc15c517007a21465cfce16cc35ac3f83058c306880f804ea9",
                "imageCreate": 1606813621000,
                "imageId": "878046fe77276fe5ff5883537a5435734c9d21a953b271534df8d01f4da14e1e",
                "imageSize": 154166831,
                "imageUpdate": 1606813621000,
                "status": "NORMAL",
                "tag": "1.0.2"
            }
        ],
        "total": 1
    }
}

只获取tag,需要提前安装jq命令

# aliyun cr GetRepoTags --RepoNamespace xxxx --RepoName xxxxxxxxx | jq ".data.tags[].tag"
"1.0.2"

五、Jenkins配置

import groovy.json.JsonSlurperClassic

def cmd = "/var/jenkins_home/aliyun cr GetRepoListByNamespace   --RepoNamespace  payfun --PageSize  100"
def aliyun_images_json = cmd.execute()

// Parse JSON into Groovy object
def data = new JsonSlurperClassic().parseText(aliyun_images_json.text)

// Prepare the results list
def aliyun_images = [];

// Add all tags
data.data.repos.each { aliyun_images.push(it.repoName) }


return aliyun_images
image.png
import groovy.json.JsonSlurperClassic

def cmd1 = "/var/jenkins_home/aliyun cr  GetRepoTags  --RepoNamespace  payfun --RepoName  " +  project
def aliyun_tags_json = cmd1.execute()

// Parse JSON into Groovy object
def data1 = new JsonSlurperClassic().parseText(aliyun_tags_json.text)

// Prepare the results list
def aliyun_tags = [];

// Add all tags
data1.data.tags.each { aliyun_tags.push(it.tag) }


return aliyun_tags
image.png
import groovy.json.JsonSlurperClassic

def cmd1 = "/var/jenkins_home/aliyun cr  GetRepoTags  --RepoNamespace  payfun --RepoName  " +  'aiotjp-data'
def aliyun_tags_json = cmd1.execute()

// Parse JSON into Groovy object
def data1 = new JsonSlurperClassic().parseText(aliyun_tags_json.text)

// Prepare the results list
def aliyun_tags = [];

// Add all tags
data1.data.tags.each { aliyun_tags.push(it.tag) }


return aliyun_tags
image.png
image.png
上一篇下一篇

猜你喜欢

热点阅读