1111from continuous_delivery_scripts .utils .language_specifics_base import BaseLanguage , get_language_from_file_name
1212from continuous_delivery_scripts .spdx_report .spdx_project import SpdxProject
1313from continuous_delivery_scripts .utils .configuration import configuration , ConfigurationVariable
14- from continuous_delivery_scripts .utils .git_helpers import LocalProjectRepository
14+ from continuous_delivery_scripts .utils .git_helpers import LocalProjectRepository , GitWrapper
1515
1616logger = logging .getLogger (__name__ )
1717
18- SRC_DIR = configuration .get_value (ConfigurationVariable .SOURCE_DIR )
19- ROOT_DIR = configuration .get_value (ConfigurationVariable .PROJECT_ROOT )
18+ SRC_DIR = Path ( str ( configuration .get_value (ConfigurationVariable .SOURCE_DIR )) )
19+ ROOT_DIR = Path ( str ( configuration .get_value (ConfigurationVariable .PROJECT_ROOT )) )
2020ENVVAR_GORELEASER_GIT_TOKEN = "GITHUB_TOKEN"
2121ENVVAR_GORELEASER_CUSTOMISED_TAG = "GORELEASER_CURRENT_TAG"
22+ ENVVAR_GO_MOD = "GO111MODULE"
23+ GO_MOD_ON_VALUE = "on"
2224
2325
2426def _generate_golds_command_list (output_directory : Path , module : str ) -> List [str ]:
@@ -53,20 +55,43 @@ def _install_goreleaser_command_list() -> List[str]:
5355def _call_golds (output_directory : Path , module : str ) -> None :
5456 """Calls Golds for generating the docs."""
5557 logger .info ("Installing Golds if missing." )
56- check_call (_install_golds_command_list ())
58+ env = os .environ
59+ env [ENVVAR_GO_MOD ] = GO_MOD_ON_VALUE
60+ check_call (_install_golds_command_list (), env = env )
5761 logger .info ("Creating Golds documentation." )
58- check_call (_generate_golds_command_list (output_directory , module ), cwd = SRC_DIR )
62+ check_call (_generate_golds_command_list (output_directory , module ), cwd = SRC_DIR , env = env )
5963
6064
6165def _call_goreleaser_check (version : str ) -> None :
6266 """Calls go releaser check to verify configuration."""
6367 logger .info ("Installing GoReleaser if missing." )
64- check_call (_install_goreleaser_command_list ())
65- logger .info ("Checking GoReleaser configuration." )
6668 env = os .environ
69+ env [ENVVAR_GO_MOD ] = GO_MOD_ON_VALUE
70+ check_call (_install_goreleaser_command_list (), env = env )
71+ logger .info ("Checking GoReleaser configuration." )
6772 env [ENVVAR_GORELEASER_CUSTOMISED_TAG ] = version
6873 env [ENVVAR_GORELEASER_GIT_TOKEN ] = configuration .get_value (ConfigurationVariable .GIT_TOKEN )
69- check_call (_generate_goreleaser_check_command_list (), cwd = ROOT_DIR )
74+ check_call (_generate_goreleaser_check_command_list (), cwd = ROOT_DIR , env = env )
75+
76+
77+ def _determine_go_module_tag (version ) -> Optional [str ]:
78+ """Determines go module for tagging.
79+
80+ See https://golang.org/ref/mod#vcs-version.
81+ and https://github.com/golang/go/wiki/Modules#should-i-have-multiple-modules-in-a-single-repository.
82+ """
83+ module = ""
84+ try :
85+ module = str (SRC_DIR .relative_to (ROOT_DIR ))
86+ except ValueError :
87+ try :
88+ module = str (ROOT_DIR .relative_to (SRC_DIR ))
89+ except ValueError as exception :
90+ logger .warning (exception )
91+ if module == "." or len (module ) == 0 :
92+ return None
93+ module = module .rstrip ("/" )
94+ return f"{ module } /{ version } "
7095
7196
7297class Go (BaseLanguage ):
@@ -118,10 +143,19 @@ def should_clean_before_packaging(self) -> bool:
118143 """States whether the repository must be cleaned before packaging happens."""
119144 return True
120145
146+ def tag_release (self , git : GitWrapper , version : str ) -> None :
147+ """Tags release commit."""
148+ super ().tag_release (git , version )
149+ go_tag = _determine_go_module_tag (self .get_version_tag (version ))
150+ if go_tag :
151+ git .create_tag (go_tag , message = f"Golang module release: { go_tag } " )
152+
121153 def _call_goreleaser_release (self , version : str ) -> None :
122154 """Calls go releaser release to upload packages."""
123155 logger .info ("Installing GoReleaser if missing." )
124- check_call (_install_goreleaser_command_list ())
156+ env = os .environ
157+ env [ENVVAR_GO_MOD ] = GO_MOD_ON_VALUE
158+ check_call (_install_goreleaser_command_list (), env = env )
125159 tag = self .get_version_tag (version )
126160 # The tag of the release must be retrieved
127161 # See https://github.com/goreleaser/goreleaser/discussions/1426
@@ -132,7 +166,6 @@ def _call_goreleaser_release(self, version: str) -> None:
132166 git .checkout (f"tags/{ tag } " )
133167 logger .info ("Release package." )
134168 changelogPath = configuration .get_value (ConfigurationVariable .CHANGELOG_FILE_PATH )
135- env = os .environ
136169 env [ENVVAR_GORELEASER_CUSTOMISED_TAG ] = tag
137170 env [ENVVAR_GORELEASER_GIT_TOKEN ] = configuration .get_value (ConfigurationVariable .GIT_TOKEN )
138171 check_call (_generate_goreleaser_release_command_list (changelogPath ), cwd = ROOT_DIR , env = env )
0 commit comments