Downloading a GitHub current release

Downloading a GitHub current release

I had a need to download the current version of a package distributed via GitHub in one of my Ansible playbooks. I also wrote a PowerShell version for use in another script. I wanted to write down the code so I wouldn't forget it.

You have to specify the GitHub account and the repo name. The rest of this code will grab the latest release and download an asset based on a substring of the file name. This only works if the package is distributed via GitHub as a download asset in the release. If they have links in the release to the downloads, this won't capture that.

---
- name: Install Git
  hosts: "{{ Target_Servers }}"
  gather_facts: false
  tasks:
    - name: Get latest release info
      ansible.builtin.uri:
        url: https://api.github.com/repos/{{ GH_account_name }}/{{ GH_repo_name }}/releases/latest
        return_content: true
      register: latest_releases
      delegate_to: localhost
      vars:
        GH_account_name: "git-for-windows"
        GH_repo_name: "git"
    - name: Extract latest release
      set_fact:
        browser_download_url: "{{ latest_releases.json | community.general.json_query(fn_query) }}"
      vars:
        fn_query: "assets[?contains(name,'{{ asset_filter }}')].browser_download_url"
        asset_filter: "64-bit.exe"
    - name: Output Current version
      debug:
        msg: "Processing version {{ latest_releases.json.tag_name }}"
    - name: Download Git installer
      win_get_url:
        url: "{{ browser_download_url[0] }}"
        dest: E:\Admin\GitSetup.exe
$GH_account_name="git-for-windows"
$GH_repo_name="git"
$asset_filter="*64-bit.exe"
$download_path="$ExtractedPath\Installs\Git"
$content=Invoke-RestMethod -Method Get -Uri "https://api.github.com/repos/$($GH_account_name)/$($GH_repo_name)/releases/latest"
$downloadURL=$content.assets | where-object {$_.name -like $asset_filter} | Select-Object -ExpandProperty browser_download_url
Invoke-WebRequest -Uri $downloadURL -Method GET -OutFile "$($download_path)\$($downloadURL -split "/" | select-object -last 1)"