Update Go Modules #14
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Update Go Modules | |
on: | |
schedule: | |
- cron: '0 0 * * *' # Runs every day at midnight | |
workflow_dispatch: # Allows manual triggering of the workflow | |
jobs: | |
update_go_modules: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v2 | |
- name: Ensure running on main branch | |
id: check_branch | |
run: | | |
if [ "${{ github.ref }}" != "refs/heads/main" ]; then | |
echo "Not on main branch, exiting." | |
exit 1 | |
fi | |
- name: Set up Go | |
uses: actions/setup-go@v2 | |
with: | |
go-version: '^1.17' | |
- name: Get current Go version | |
id: get_current_go_version | |
run: | | |
CURRENT_GO_VERSION=$(grep '^go ' go.mod | cut -d ' ' -f 2) | |
echo "CURRENT_GO_VERSION=$CURRENT_GO_VERSION" >> $GITHUB_ENV | |
- name: Get latest Go version | |
id: get_latest_go_version | |
run: | | |
LATEST_GO_VERSION=$(go version | cut -d ' ' -f 3 | sed 's/go//') | |
echo "LATEST_GO_VERSION=$LATEST_GO_VERSION" >> $GITHUB_ENV | |
- name: Update Go version in go.mod if needed | |
id: update_go_version | |
run: | | |
if [ "${{ env.CURRENT_GO_VERSION }}" != "${{ env.LATEST_GO_VERSION }}" ]; then | |
sed -i "s/^go .*/go $LATEST_GO_VERSION/" go.mod | |
echo "GO_VERSION_UPDATED=true" >> $GITHUB_ENV | |
else | |
echo "GO_VERSION_UPDATED=false" >> $GITHUB_ENV | |
fi | |
- name: Update Go modules | |
run: | | |
go get -u ./... | |
go mod tidy | |
- name: Check for changes | |
id: check_changes | |
run: | | |
if git diff --quiet; then | |
echo "No changes in go.mod or go.sum" | |
echo "exit_code=0" >> $GITHUB_ENV | |
else | |
echo "Changes found in go.mod or go.sum" | |
git config --global user.name "github-actions[bot]" | |
git config --global user.email "github-actions[bot]@users.noreply.github.com" | |
TIMESTAMP=$(date +%Y%m%d%H%M%S) | |
BRANCH_NAME="update-go-modules-$TIMESTAMP" | |
git checkout -b $BRANCH_NAME | |
git add go.mod go.sum | |
git commit -m "Update Go modules and Go version to the latest version" | |
git push --set-upstream origin $BRANCH_NAME | |
echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV | |
echo "exit_code=1" >> $GITHUB_ENV | |
fi | |
- name: Create Pull Request | |
if: env.exit_code == '1' | |
uses: peter-evans/create-pull-request@v3 | |
with: | |
token: ${{ secrets.GITHUB_TOKEN }} | |
commit-message: Update Go modules and Go version to the latest version | |
branch: ${{ env.BRANCH_NAME }} | |
base: main | |
title: Update Go modules and Go version | |
body: Update Go and Go modules | |
labels: go-modules, automated-update |