您可以使用Github GraphQL API(如this question和this one所示),例如:
#
# Tasks that may be included in an Ansible playbook or role depending on your needs
#
# Some variables to define to identify your repository
# They may be set as playbook or role variables as well
# You'll need a Bearer token (see https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token)
- set_fact:
bearer_token: YOUR_BEARER_TOKEN
repository_name: repository-name
repository_owner: repository-owner
- name: Retrieve packages for repository
uri:
url: https://api.github.com/graphql
method: POST
body: '{"query":
"query { repository(name: \"{{ repository_name }}\", owner: \"{{ repository_owner }}\") {
packages(first:10) { nodes { name, packageType, latestVersion {
version, files(first:100) { nodes { url } }
} } }
}
}"'
headers:
Content-Type: application/json
Accept: "application/vnd.github.packages-preview+json"
Authorization: "bearer {{ bearer_token }}"
register: github_packages_json
这将提供如下输出:
{
"json": {
"data": {
"repository": {
"packages": {
"nodes": [
{
"latestVersion": {
"files": {
"nodes": [
{
"url": "https://pkg.githubusercontent.com/xxx/some-url"
},
{
"url": "https://pkg.githubusercontent.com/xxx/another-url"
}
]
},
"version": "my-package-1.2.3"
},
"name": "my-package",
"packageType": "DOCKER"
}
]
}
}
}
},
}
根据packageType
您可能需要执行不同的操作。例如,aDOCKER
packageType
将要求您拉取图像,例如:
- name: pull docker
shell: docker pull docker.pkg.github.com/{{ repository_owner | lower }}/{{ repository_name }}/{{ docker_image_name }}:{{ docker_image_version }}
vars:
docker_image_name: "{{ github_packages_json.json.data.repository.packages.nodes[0].name }}"
docker_image_version: "{{ github_packages_json.json.data.repository.packages.nodes[0].latestVersion.version }}"