Ansible github_packages

Ansible github_packages

可以从 GitHub 存储库中获得两个版本(二进制版本和包版本),如下所示:

我想使用 Ansible 从我的 GitHub Repo

中检索软件包发布

我在 Ansible 文档上进行了一些搜索并找到了一个集合 community.general.github_release 但这给出了最新的 Release 存储库二进制文件而不是 Package Releases.

任何人如果知道可以从 GitHub 获取软件包发布的集合,可以提供帮助吗?

感谢任何帮助。谢谢

community.general.github_release Ansible role is part of the ansible-collections/community.general代码。

它的源代码 source_control/github/github_release.py shows that is is using github3.py,使用 GitHub 的 REST 的库 API。
具体来说,latest_release endpoint (code here) uses the GET /repos/{owner}/{repo}/releases/latest REST API.

但是,对于 github3.py(由 Ansible 角色使用),“软件包发布”是 an asset, with an ID you can find in a github3.repos.release.Release: the original_assets will give you all the assets id.

因此您需要编写一个类似于 latest_release 的角色,使用 latest_release 返回的版本来调用 github3.repos.release.Release,获取资产 ID 并下载您的需要使用 asset(asset_id)

您可以使用Github GraphQL API (as shown in and ) 例如:

  #
  # 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 的不同,您可能需要执行不同的操作。例如,DOCKER 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 }}"