Skip to content

Instantly share code, notes, and snippets.

@mikroskeem
Created March 16, 2024 07:19
Show Gist options
  • Save mikroskeem/10089b91dbd8f2af6dc62f1093c513ed to your computer and use it in GitHub Desktop.
Save mikroskeem/10089b91dbd8f2af6dc62f1093c513ed to your computer and use it in GitHub Desktop.
Download a private GitHub release using Ansible
---
- name: "Download a private GitHub release"
vars:
owner: "mikroskeem"
repo: "snakeoil"
tag: "v0.0.1"
asset_name: "snakeoil.x86_64-linux"
get_url_args:
dest: "/tmp/the-thing"
github_pat: "{{ lookup('env', 'GH_TOKEN') }}"
block:
- name: "Obtain GitHub release details"
ansible.builtin.uri:
url: "https://api.github.com/repos/{{ owner }}/{{ repo }}/releases/tags/{{ tag }}"
return_content: true
use_netrc: "{{ github_pat | default(false) is falsy }}"
headers:
"accept": "application/vnd.github.v3+json"
"authorization": "{{ 'token ' + github_pat if github_pat | default('') else None }}"
"x-github-api-version": "2022-11-28"
register: "_gh_release"
- name: "Download release from GitHub"
vars:
gh_release_asset_url: "{{ _gh_release.json.assets | selectattr('name', 'equalto', asset_name) | map(attribute='url') | first }}"
ansible.builtin.get_url:
url: "{{ gh_release_asset_url }}"
dest: "{{ get_url_args.dest | mandatory }}"
owner: "{{ get_url_args.owner | default(None) }}"
group: "{{ get_url_args.group | default(None) }}"
mode: "{{ get_url_args.mode | default(None) }}"
use_netrc: "{{ github_pat | default(false) is falsy }}"
headers:
"accept": "application/octet-stream"
"authorization": "{{ 'token ' + github_pat if github_pat | default('') else None }}"
"x-github-api-version": "2022-11-28"
@robpomeroy
Copy link

Thanks so much for this Mark. Extremely helpful. I needed to download a series of files. So my approach, based on yours, is:

main.yml:

- name: Get GitHub release details
  ansible.builtin.uri:
    url: https://api.github.com/repos/my-org/my-repo/releases/tags/my-tag
    return_content: true
    headers:
      Accept: application/vnd.github.v3+json
      Authorization: "token {{ GITHUB_TOKEN }}"
      x-github-api-version: "2022-11-28"
  register: _gh_release

- name: Download release files
  ansible.builtin.include_tasks: downloadAsset.yml
  with_items:
    - name: file1
      dest: /dest/1
    - name: file2
      dest: /dest/2
  loop_control:
    loop_var: asset_details

downloadAsset.yml:

- name: downloadAsset | Download an asset from a GitHub release ({{ asset_details.name }})
  vars:
    gh_release_asset_url: "{{ _gh_release.json.assets | selectattr('name', 'equalto', asset_details.name) | map(attribute='url') | first }}"
  ansible.builtin.get_url:
    url: "{{ gh_release_asset_url }}"
    dest: "{{ asset_details.dest }}"
    owner: root
    group: root
    mode: '0755'
    headers:
      Accept: application/octet-stream
      Authorization: "token {{ GITHUB_TOKEN }}"
      x-github-api-version: "2022-11-28"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment