Skip to content

Commit

Permalink
fix: authenticate curl GET requests to GitHub with token input
Browse files Browse the repository at this point in the history
  • Loading branch information
mtardy committed Oct 31, 2023
1 parent 6e3505b commit adeab4f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
14 changes: 12 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,20 @@ on:
- '.github/workflows/test.yaml'

jobs:
build:
runs-on: ubuntu-20.04
test:
runs-on: ubuntu-latest
steps:
- name: Check out repository
uses: actions/checkout@v3
- name: Install bpftool
uses: ./

test-authenticated:
runs-on: ubuntu-latest
steps:
- name: Check out repository
uses: actions/checkout@v3
- name: Install bpftool
uses: ./
with:
token: ${{ secrets.GITHUB_TOKEN }}
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ steps:
bpftool version
```

Note that you might want to set the `token` variable to avoid rate limiting
from the GitHub API.

Using custom inputs:

```yaml
Expand All @@ -33,6 +36,7 @@ steps:
with:
version: 'v7.2.0'
path: '/usr/bin'
token: ${{ secrets.GITHUB_TOKEN }}

- name: Run bash script
run: |
Expand All @@ -46,6 +50,7 @@ steps:
| --------- | ------ | ---------------- | --------------------------------------------------------------------------------------------------- |
| `version` | String | `latest` | [bpftool](https://github.com/libbpf/bpftool/releases/) version to install. (eg. `v7.2.0`, `latest`) |
| `path` | String | `/usr/local/bin` | Destination path to install bpftool |
| `token` | String | | A GitHub token (e.g. `secrets.GITHUB_TOKEN`) to authenticate requests to GitHub API |

## License

Expand Down
22 changes: 17 additions & 5 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@ branding:
color: 'gray-dark'
inputs:
version:
description: 'Version of bpftool'
description: 'Version of bpftool to install'
required: false
default: 'latest'
path:
description: 'Installation path'
required: false
default: '/usr/local/bin'
token:
description: 'A GitHub token (e.g. secrets.GITHUB_TOKEN) to authenticate requests to GitHub API'
required: false


runs:
using: "composite"
steps:
Expand All @@ -28,22 +33,27 @@ runs:
- name: retrieve version
id: version
shell: bash {0}
env:
AUTH_TOKEN: ${{ inputs.token }} # for bash param expansion
run: |
if [[ "${{ inputs.version }}" = "latest" ]]; then
max_attempts=7
attempt=1
retry_delay=1
while [ -z "$version" ] && [ "$attempt" -le "$max_attempts" ]; do
while [ -z "$version" ]; do
echo "Attempt $attempt: Fetching latest version from GitHub..."
response=$(curl -s https://api.github.com/repos/libbpf/bpftool/releases/latest)
response=$(curl -s ${AUTH_TOKEN:+ -H "authorization: Bearer $AUTH_TOKEN"} https://api.github.com/repos/libbpf/bpftool/releases/latest)
version=$(echo $response | jq -r '.tag_name | select (.!=null)')
attempt=$((attempt + 1))
if [ -z "$version" ]; then
if [ "$attempt" -gt "$max_attempts" ]; then
break
fi
delay=$((2 ** (attempt - 1) * retry_delay))
echo -e "$response\n"
echo "Fetching latest version from GitHub failed. Retrying in $delay seconds..."
sleep "$delay"
fi
attempt=$((attempt + 1))
done
if [ -z "$version" ]; then
echo "Fetching latest version failed after $max_attempts attempts. GitHub API might be unavailable, rerun this workflow later, exiting!"
Expand All @@ -66,8 +76,10 @@ runs:
- name: download
if: steps.cache-download.outputs.cache-hit != 'true'
shell: bash
env:
AUTH_TOKEN: ${{ inputs.token }} # for bash param expansion
run: |
curl -s -L https://github.com/libbpf/bpftool/releases/download/${{ steps.version.outputs.version }}/bpftool-${{ steps.version.outputs.version }}-${{ steps.arch.outputs.arch }}.tar.gz | tar xz
curl -s -L ${AUTH_TOKEN:+ -H "authorization: Bearer $AUTH_TOKEN"} https://github.com/libbpf/bpftool/releases/download/${{ steps.version.outputs.version }}/bpftool-${{ steps.version.outputs.version }}-${{ steps.arch.outputs.arch }}.tar.gz | tar xz
sudo install bpftool ${{ inputs.path }}
- name: check installation
Expand Down

0 comments on commit adeab4f

Please sign in to comment.