githubansiblegithub-package-registryansible-galaxy

Ansible github_packages


There are two releases that can be obtained from a GitHub repo (Binary Releases and Package Releases) as shown below:

GitHub SS

I want to use Ansible to retrieve Package Releases from my GitHub Repo

I did some searching on Ansible docs and found a collection community.general.github_release but this gives the latest Release binaries of the repo and not Package Releases.

Can anyone help if they know a collection that can fetch Package Releases from GitHub ?

Appreciate any help. Thanks


Solution

  • You can use Github GraphQL API (as shown in this question and this one) such as:

      #
      # 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
    

    This will provide an output like:

      {
        "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"
                            }
                        ]
                    }
                }
            }
        },
    }
    

    Depending on packageType you may need to perform different action. For example, a DOCKER packageType would require you to pull the image such as:

      - 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 }}"