Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save wesley-dean/b8f732350d3469dafc879944decfab95 to your computer and use it in GitHub Desktop.

Select an option

Save wesley-dean/b8f732350d3469dafc879944decfab95 to your computer and use it in GitHub Desktop.
GitHub Action to update system-wide Golang packages
---
name: Update Golang packages for global installation
# yamllint disable-line rule:truthy
on:
workflow_dispatch:
schedule:
- cron: "0 0 * * 0" # Every Sunday at 00:00 UTC
permissions:
contents: read
jobs:
update-go-packages:
permissions:
contents: write
pull-requests: write
runs-on: ubuntu-22.04
steps:
- name: Setup Node
id: setup-node
uses: actions/setup-node@cdca7365b2dadb8aad0a33bc7601856ffabcc48e # pin@v4
- name: Checkout code
id: checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # pin@v4
- name: Update go packages
id: update-go-packages
run: go install github.com/nao1215/gup@latest
- name: Create commit
id: create-commit
run: |
# set some variables so we don't lose our minds
gup_executable="${HOME}/go/bin/gup"
original_package_list="${{ github.workspace }}/go_packages.txt"
new_package_list="$(mktemp)"
# update the go packages
"${gup_executable}" update
"${gup_executable}" list > "${new_package_list}"
if diff "${new_package_list}" "${original_package_list}" ; then
rm -f "${original_package_list}"
mv -f "${new_package_list}" "${original_package_list}"
git add "${original_package_list}"
git commit -m "Bump go stuff"
echo "updates=true" >> "$GITHUB_OUTPUT"
else
rm -f "${new_package_list}"
echo "updates=false" >> "$GITHUB_OUTPUT"
fi
- name: "Import GPG key"
id: import-gpg
uses: crazy-max/ghaction-import-gpg@cb9bde2e2525e640591a934b1fd28eef1dcaf5e5 # pin@v6
if: ${{ steps.create-commit.outputs.updates == 'true' }}
with:
gpg_private_key: ${{ env.GPG_PRIVATE_KEY }}
passphrase: ${{ env.GPG_PRIVATE_KEY_PASSPHRASE }}
git_user_signingkey: true
git_commit_gpgsign: true
- name: Create Pull Request updated files
id: cpr
uses: peter-evans/create-pull-request@4e1beaa7521e8b457b572c090b25bd3db56bf1c5 # pin@v5
if: ${{ steps.create-commit.outputs.updates == 'true' }}
with:
token: ${{ secrets.PAT || secrets.GITHUB_TOKEN }}
commit-message: "Update golang packages for global install"
title: "Update golang packages for global install"
labels: bot
sign-commits: true
committer: "${{ steps.import-gpg.outputs.name }} <${{ steps.import-gpg.outputs.email }}>"
- name: Create PR output
id: pr-output
if: ${{ steps.create-commit.outputs.updates == 'true' }}
run: |
echo "Pull Request Number - ${{ steps.cpr.outputs.pull-request-number }}"
echo "Pull Request URL - ${{ steps.cpr.outputs.pull-request-url }}"
#!/usr/bin/env bash
set -euo pipefail
command_exists() {
type -p "$1" > /dev/null 2> /dev/null
# type "$1" 2> /dev/null | grep -Eq "is (a function|aliased to)"
}
export- f command_exists
if command_exists "go" \
&& [ -f "${HOME}/go_packages.txt" ] ; then
extra_packages_go() {
sed -Ene 's/[^:]*:\s*//p' < "${HOME}/go_packages.txt" | xargs -n1 go install
}
export -f extra_packages_go
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment