Skip to content

Commit

Permalink
Add a TIL about downloading assets from a GitHub release
Browse files Browse the repository at this point in the history
  • Loading branch information
alexwlchan committed Nov 22, 2024
1 parent 432a5f3 commit 40c35b1
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/_til/2024/2024-11-22-install-from-github-releases.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
layout: til
title: How to install an asset from a GitHub release
date: 2024-11-22 11:35:50 +0000
tags:
- github
summary:
Use `gh release download`, which includes a pattern matcher if you want to pick a specific asset.
---
I wanted to install Caddy in GitHub Actions, and one of the ways [to install it](https://caddyserver.com/docs/install#static-binaries) is by downloading static binaries from their GitHub releases.

I wasn't sure how to download release assets in a sensible way -- I've written shell scripts for it before, but it feels messy and fragile.
Surely there must be a better way!

I stumbled upon [a GitHub Action](https://github.com/php/php-src/blob/master/.github/actions/setup-caddy/action.yml) in the PHP repository (!) which pointed me in the right direction:

```shell
gh release -R caddyserver/caddy download --pattern 'caddy_*_linux_amd64.tar.gz' -O - | sudo tar -xz -C /usr/bin caddy
sudo chmod +x /usr/bin/caddy
```

This is using [the GitHub CLI](https://cli.github.com/), which I haven't used before -- but it's pre-installed in GitHub Actions, which is very convenient for my purposes!

## In a GitHub Action

Here's how I packaged this as a step in a GitHub Action:

```yml
- name: Install Caddy
env:
GH_TOKEN: ${{ github.token }}
run: |
gh release download \
--repo caddyserver/caddy \
--pattern 'caddy_*_linux_amd64.tar.gz' \
--output caddy.tar.gz
tar -xzf caddy.tar.gz --directory /usr/local/bin
chmod +x /usr/local/bin/caddy
which caddy
```
It's a bit more verbose and leaves a `caddy.tar.gz` file lying around, but I find this version easier to understand and debug.

## Notes

* You can specific a release to `gh release download`, or if not it will pick the latest release.
If you let it pick automatically, it skips pre-releases.

0 comments on commit 40c35b1

Please sign in to comment.