Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reusable action for go version updates #88

Merged
merged 5 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions .github/workflows/go-version-workflow.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Copyright (c) 2024 Dell Inc., or its subsidiaries. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0

# Reusable workflow to perform go version update on Golang based projects
name: Update Go Version Workflow

on:
workflow_call:

jobs:
go-version:
name: Update go version
runs-on: ubuntu-latest
steps:
- name: Check latest go version
run: |
GO_VERSION_STRIPPED=$(curl -sL https://go.dev/VERSION?m=text | awk 'NR==1{print $1}' | tr -cd '[:digit:].')
GO_VERSION=$(curl -sL https://go.dev/VERSION?m=text | awk 'NR==1{print $1}')

if [[ -z "$GO_VERSION_STRIPPED" || -z "$GO_VERSION" ]]; then
echo "Error: GO_VERSION_STRIPPED or GO_VERSION is empty"
exit 1
fi

echo "GO_VERSION_STRIPPED=$GO_VERSION_STRIPPED" >> $GITHUB_ENV
echo "GO_VERSION=$GO_VERSION" >> $GITHUB_ENV

- uses: actions/setup-go@v5
with:
go-version: "${{ env.GO_VERSION_STRIPPED }}"
cache: false
check-latest: true

- name: Checkout the code
uses: actions/checkout@v4

# This step will update the go version in:
# 1 - go.mod files and go.mod toolchain, if found
# 2 - csm-common.mk for UBI image
# 3 - github actions
# 4 - Dockerfiles
- name: Update go version
env:
gomod: go.mod
grep_cmd2: grep -l "go-version"
grep_cmd3: grep -l "FROM golang"
exclude_file: go-version-workflow.yaml
run: |
echo "Updating go version to ${{ env.GO_VERSION_STRIPPED }}"

find . -name "${{ env.gomod }}" -execdir sh -c '[ -f ${{ env.gomod }} ] && go mod edit -go=${{ env.GO_VERSION_STRIPPED }} && go mod tidy' \;

find . -name "${{ env.gomod }}" -execdir sh -c '[ -f ${{ env.gomod }} ] && grep -q "toolchain" go.mod && go mod edit -toolchain=${{ env.GO_VERSION }} && go mod tidy' \;

if [ -f config/csm-common.mk ]; then sed -i "s/DEFAULT_GOVERSION.*/DEFAULT_GOVERSION=\"${{ env.GO_VERSION_STRIPPED }}\"/g" config/csm-common.mk; fi

find . -type f ! -name "${{ env.exclude_file }}" -exec ${{ env.grep_cmd2 }} {} \; | while read -r file; do sed -i "s/go-version:.*/go-version: \"${{ env.GO_VERSION_STRIPPED }}\"/" "$file"; done

find . -type f ! -name "${{ env.exclude_file }}" -exec ${{ env.grep_cmd3 }} {} \; | while read -r file; do sed -i "s/golang:.*/golang:${{ env.GO_VERSION_STRIPPED }}/" "$file"; done

# Needed for signing commits using Github App tokens
# See: https://github.com/peter-evans/create-pull-request/blob/main/docs/concepts-guidelines.md#commit-signing
- uses: actions/create-github-app-token@v1.11.0
id: generate-token
with:
app-id: ${{ vars.CSM_RELEASE_APP_ID }}
private-key: ${{ secrets.CSM_RELEASE_APP_PRIVATE_KEY }}

# Must enable "allow GitHub Actions to create pull requests" setting
# Author defaults to the user who triggered the workflow run
- name: Create pull request
uses: peter-evans/create-pull-request@v7
with:
token: ${{ steps.generate-token.outputs.token }}
branch: "go-${{ env.GO_VERSION_STRIPPED }}"
commit-message: "Update go version to ${{ env.GO_VERSION_STRIPPED }}"
title: "Update go version to ${{ env.GO_VERSION_STRIPPED }}"
body: |
Go version updated to ${{ env.GO_VERSION_STRIPPED }}
Auto-generated by [common-github-actions](https://github.com/dell/common-github-actions)
sign-commits: true
delete-branch: true
22 changes: 22 additions & 0 deletions .github/workflows/go-version.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright (c) 2024 Dell Inc., or its subsidiaries. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0

# Reusable workflow to perform go version update on Golang based projects
name: Go Version Update

on:
workflow_dispatch:
repository_dispatch:
types: [go-update-workflow]

jobs:
# go version update
go-version-update:
uses: dell/common-github-actions/.github/workflows/go-version-workflow.yaml@main
name: Go Version Update
secrets: inherit
79 changes: 79 additions & 0 deletions .github/workflows/trigger-go-workflow.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Copyright (c) 2024 Dell Inc., or its subsidiaries. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0

# Trigger workflow for go version update on CSM projects
name: Trigger Go Version Workflow

on:
# Can be manually triggered
workflow_dispatch:
inputs:
internal-repositories:
description: 'Internal repositories to trigger workflow against. Matrix example: "org/repo1, org/repo2"'
required: false

# environment variables
env:
WF_ORG: dell
INTERNAL_REPOS: ${{ github.event.inputs.internal-repositories }}

jobs:
trigger:
name: Trigger Go Version Update
runs-on: ubuntu-latest

strategy:
matrix:
repo:
[
"$WF_ORG/cert-csi",
"$WF_ORG/common-github-actions",
"$WF_ORG/cosi",
"$WF_ORG/csi-metadata-retriever",
"$WF_ORG/csi-powerflex",
"$WF_ORG/csi-powermax",
"$WF_ORG/csi-powerscale",
"$WF_ORG/csi-powerstore",
"$WF_ORG/csi-unity",
"$WF_ORG/csi-volumegroup-snapshotter",
"$WF_ORG/csm",
"$WF_ORG/csm-authorization",
"$WF_ORG/csm-docs",
"$WF_ORG/csm-metrics-powermax",
"$WF_ORG/csm-metrics-powerscale",
"$WF_ORG/csm-metrics-powerstore",
"$WF_ORG/csm-metrics-unity",
"$WF_ORG/csm-observability",
"$WF_ORG/csm-operator",
"$WF_ORG/csm-replication",
"$WF_ORG/dell-csi-extensions",
"$WF_ORG/gobrick",
"$WF_ORG/gocsi",
"$WF_ORG/gofsutil",
"$WF_ORG/goiscsi",
"$WF_ORG/gonvme",
"$WF_ORG/goobjectscale",
"$WF_ORG/gopowermax",
"$WF_ORG/gopowerscale",
"$WF_ORG/gopowerstore",
"$WF_ORG/goscaleio",
"$WF_ORG/gounity",
"$WF_ORG/karavi-metrics-powerflex",
"$WF_ORG/karavi-resiliency",
"$INTERNAL_REPOS",
]
shaynafinocchiaro marked this conversation as resolved.
Show resolved Hide resolved

steps:
- name: Trigger Go Version Update
uses: peter-evans/repository-dispatch@v3
with:
# For token information, see: https://github.com/peter-evans/repository-dispatch/tree/main?tab=readme-ov-file#token
token: ${{ secrets.CSMBOT_PAT }}
repository: ${{ matrix.repo }}
event-type: go-update-workflow
client-payload: '{"ref": "${{ github.ref }}", "sha": "${{ github.sha }}"}'
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ name: Release Gobrick
# Invocable as a reusable workflow
# Can be manually triggered
on:
workflow_call:
workflow_call:
workflow_dispatch:
inputs:
version:
Expand All @@ -100,6 +100,28 @@ jobs:
name: Release Go Client Libraries
```

### go-version-workflow

This workflow updates to the latest go version in repositories that utilize Golang as the primary development language. The workflow is triggered by https://github.com/dell/common-github-actions/actions/workflows/trigger-go-workflow.yaml or can be triggered manually.

The workflow does not accept any parameters and can be used from any repository by creating a workflow that resembles the following
Note: Workflows that call reusable workflows in the same organization or enterprise can use the inherit keyword to implicitly pass the secrets. See: https://docs.github.com/en/actions/sharing-automations/reusing-workflows#passing-inputs-and-secrets-to-a-reusable-workflow.

```yaml
name: Go Version Update

on:
workflow_dispatch:
repository_dispatch:
types: [go-update-workflow]
Comment on lines +113 to +116

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What will trigger this workflow?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


jobs:
go-version-update:
uses: dell/common-github-actions/.github/workflows/go-version-workflow.yaml@main
name: Go Version Update
secrets: inherit
```

## Support

Don’t hesitate to ask! Contact the team and community on [our support](./docs/SUPPORT.md).
Expand Down