Skip to content

Commit

Permalink
update GH actions to change go version in go.mod file
Browse files Browse the repository at this point in the history
  • Loading branch information
ntheanh201 committed Dec 27, 2024
1 parent be68eef commit 72d88f9
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
6 changes: 4 additions & 2 deletions .github/actions/pr-to-update-go/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ Environment Variables
+----------------------------+----------------------------------------------------------------------------------+
| ``GO_VERSION_FILE`` | Required. The file in the repo containing the version of Go used by the repo. |
+----------------------------+----------------------------------------------------------------------------------+


| ``GO_MOD_FILE`` | Optional. The go.mod file in the repo containing the version of Go used by the |
| | repo. If not provided, the action will not update the go.mod file. |
+----------------------------+----------------------------------------------------------------------------------+
Outputs
=======

Expand All @@ -66,6 +67,7 @@ Example usage
GIT_AUTHOR_NAME: asf-ci-trafficcontrol
GITHUB_TOKEN: ${{ github.token }}
GO_VERSION_FILE: GO_VERSION
GO_MOD_FILE: go.mod
Tests
=====
Expand Down
9 changes: 9 additions & 0 deletions .github/actions/pr-to-update-go/pr_to_update_go/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
ENV_ENV_FILE - The repository-relative path to an environment file containing
a line setting the variable GO_VERSION to the Go version
(e.g. GO_VERSION=3.2.1)
ENV_GO_MOD_FILE - The repository-relative path to the go.mod file containing the
version of Go used by the repo (e.g. go 3.2.1).
Miscellaneous:
Expand Down Expand Up @@ -71,6 +73,13 @@
containing the Go version.
"""

ENV_GO_MOD_FILE: Final = 'GO_MOD_FILE'
"""
The name of the environment variable set to repository-relative path to the
go.mod file containing the version of Go used by the repo (e.g. go 3.2.1).
"""


ENV_ENV_FILE: Final = 'ENV_FILE'
"""
The name of the environment variable set to repository-relative path to an
Expand Down
12 changes: 10 additions & 2 deletions .github/actions/pr-to-update-go/pr_to_update_go/go_pr_maker.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
GO_REPO_NAME,
RELEASE_PAGE_URL,
ENV_GO_VERSION_FILE,
ENV_GO_MOD_FILE,
ENV_GIT_AUTHOR_NAME,
GIT_AUTHOR_EMAIL_TEMPLATE,
GO_VERSION_KEY,
Expand Down Expand Up @@ -375,7 +376,7 @@ def set_go_version(self, go_version: str, commit_message: str,
Makes the commits necessary to change the Go version used by the
repository.
This includes updating the GO_VERSION and .env files at the repository's
This includes updating the GO_VERSION, GO_MOD, and .env files at the repository's
root.
"""
master_tip = self.repo.get_branch('master').commit
Expand All @@ -388,11 +389,18 @@ def set_go_version(self, go_version: str, commit_message: str,
with open(go_version_file, 'w') as go_version_file_stream:
go_version_file_stream.write(f'{go_version}\n')
env_file = getenv(ENV_ENV_FILE)
go_mod_file = getenv(ENV_GO_MOD_FILE)
if go_mod_file:
with open(go_mod_file, 'r') as go_mod_file_stream:
content = go_mod_file_stream.read()
updated_content = re.sub(r'go \d+\.\d+(\.\d+)?', f'go {go_version}', content)
with open(go_mod_file, 'w') as go_mod_file_stream:
go_mod_file_stream.write(updated_content)
env_path = PurePath(os.path.dirname(env_file), ".env")
set_key(dotenv_path=env_path, key_to_set=GO_VERSION_KEY, value_to_set=go_version,
quote_mode='never')
return self.update_files_on_tree(head=master_tip, files_to_check=[go_version_file,
env_file], commit_message=commit_message, source_branch_name=source_branch_name)
env_file, go_mod_file], commit_message=commit_message, source_branch_name=source_branch_name)

def update_files_on_tree(self, head: Union[Commit, GitCommit], files_to_check: list[str],
commit_message: str, source_branch_name:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/pr-to-update-go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ jobs:
PR_GITHUB_TOKEN: ${{ secrets.ASFCI_TOKEN }}
GO_VERSION_FILE: GO_VERSION
ENV_FILE: .env
GO_MOD_FILE: go.mod
steps:
- name: Checkout repo
uses: actions/checkout@master
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module github.com/apache/trafficcontrol/v8
// specific language governing permissions and limitations
// under the License.

go 1.23.0
go 1.23.4

require (
code.cloudfoundry.org/bytefmt v0.0.0-20211005130812-5bb3c17173e5
Expand Down

0 comments on commit 72d88f9

Please sign in to comment.