Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save reecepbcups/859e79382470a3fb4b1668190b53589a to your computer and use it in GitHub Desktop.

Select an option

Save reecepbcups/859e79382470a3fb4b1668190b53589a to your computer and use it in GitHub Desktop.
Takes a parent repo's go.mod changes and applies them to a subdirectory
name: Sync Dependabot to SUB_____________DIRECTORY
on:
pull_request:
paths:
- "go.mod"
- "go.sum"
permissions:
contents: write
pull-requests: write
jobs:
sync-interchaintest:
if: github.actor == 'dependabot[bot]'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }}
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
- uses: actions/setup-go@v5
with:
go-version-file: go.mod
- name: Extract updated deps from root go.mod
id: deps
run: |
# get the diff of go.mod against base branch
git fetch origin ${{ github.base_ref }}
CHANGED=$(git diff origin/${{ github.base_ref }}...HEAD -- go.mod \
| grep "^+" | grep -v "^+++" \
| sed 's/^+//' | sed 's/^require //' | sed 's/^\s*//' \
| grep -oP '^\S+\s+v\S+' || true)
echo "changed<<EOF" >> "$GITHUB_OUTPUT"
echo "$CHANGED" >> "$GITHUB_OUTPUT"
echo "EOF" >> "$GITHUB_OUTPUT"
- name: Update SUB_____________DIRECTORY/go.mod
if: steps.deps.outputs.changed != ''
working-directory: SUB_____________DIRECTORY
run: |
while IFS= read -r line; do
[ -z "$line" ] && continue
PKG=$(echo "$line" | awk '{print $1}')
VER=$(echo "$line" | awk '{print $2}')
# only update if the dep exists in SUB_____________DIRECTORY/go.mod
if grep -q "$PKG" go.mod; then
echo "Updating $PKG to $VER in SUB_____________DIRECTORY"
go get "$PKG@$VER"
fi
done <<< "${{ steps.deps.outputs.changed }}"
go mod tidy
- name: Commit and push
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
if git diff --quiet; then
echo "No changes to commit"
exit 0
fi
git add SUB_____________DIRECTORY/go.mod SUB_____________DIRECTORY/go.sum
git commit -m "chore(deps): sync dependabot update to SUB_____________DIRECTORY"
git push
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment