@@ -44,14 +44,16 @@ <h1 class="title">Module <code>continuous_delivery_scripts.plugins.golang</code>
4444from continuous_delivery_scripts.utils.language_specifics_base import BaseLanguage, get_language_from_file_name
4545from continuous_delivery_scripts.spdx_report.spdx_project import SpdxProject
4646from continuous_delivery_scripts.utils.configuration import configuration, ConfigurationVariable
47- from continuous_delivery_scripts.utils.git_helpers import LocalProjectRepository
47+ from continuous_delivery_scripts.utils.git_helpers import LocalProjectRepository, GitWrapper
4848
4949logger = logging.getLogger(__name__)
5050
51- SRC_DIR = configuration.get_value(ConfigurationVariable.SOURCE_DIR)
52- ROOT_DIR = configuration.get_value(ConfigurationVariable.PROJECT_ROOT)
51+ SRC_DIR = Path(str( configuration.get_value(ConfigurationVariable.SOURCE_DIR)) )
52+ ROOT_DIR = Path(str( configuration.get_value(ConfigurationVariable.PROJECT_ROOT)) )
5353ENVVAR_GORELEASER_GIT_TOKEN = "GITHUB_TOKEN"
5454ENVVAR_GORELEASER_CUSTOMISED_TAG = "GORELEASER_CURRENT_TAG"
55+ ENVVAR_GO_MOD = "GO111MODULE"
56+ GO_MOD_ON_VALUE = "on"
5557
5658
5759def _generate_golds_command_list(output_directory: Path, module: str) -> List[str]:
@@ -86,20 +88,43 @@ <h1 class="title">Module <code>continuous_delivery_scripts.plugins.golang</code>
8688def _call_golds(output_directory: Path, module: str) -> None:
8789 """Calls Golds for generating the docs."""
8890 logger.info("Installing Golds if missing.")
89- check_call(_install_golds_command_list())
91+ env = os.environ
92+ env[ENVVAR_GO_MOD] = GO_MOD_ON_VALUE
93+ check_call(_install_golds_command_list(), env=env)
9094 logger.info("Creating Golds documentation.")
91- check_call(_generate_golds_command_list(output_directory, module), cwd=SRC_DIR)
95+ check_call(_generate_golds_command_list(output_directory, module), cwd=SRC_DIR, env=env )
9296
9397
9498def _call_goreleaser_check(version: str) -> None:
9599 """Calls go releaser check to verify configuration."""
96100 logger.info("Installing GoReleaser if missing.")
97- check_call(_install_goreleaser_command_list())
98- logger.info("Checking GoReleaser configuration.")
99101 env = os.environ
102+ env[ENVVAR_GO_MOD] = GO_MOD_ON_VALUE
103+ check_call(_install_goreleaser_command_list(), env=env)
104+ logger.info("Checking GoReleaser configuration.")
100105 env[ENVVAR_GORELEASER_CUSTOMISED_TAG] = version
101106 env[ENVVAR_GORELEASER_GIT_TOKEN] = configuration.get_value(ConfigurationVariable.GIT_TOKEN)
102- check_call(_generate_goreleaser_check_command_list(), cwd=ROOT_DIR)
107+ check_call(_generate_goreleaser_check_command_list(), cwd=ROOT_DIR, env=env)
108+
109+
110+ def _determine_go_module_tag(version) -> Optional[str]:
111+ """Determines go module for tagging.
112+
113+ See https://golang.org/ref/mod#vcs-version.
114+ and https://github.com/golang/go/wiki/Modules#should-i-have-multiple-modules-in-a-single-repository.
115+ """
116+ module = ""
117+ try:
118+ module = str(SRC_DIR.relative_to(ROOT_DIR))
119+ except ValueError:
120+ try:
121+ module = str(ROOT_DIR.relative_to(SRC_DIR))
122+ except ValueError as exception:
123+ logger.warning(exception)
124+ if module == "." or len(module) == 0:
125+ return None
126+ module = module.rstrip("/")
127+ return f"{module}/{version}"
103128
104129
105130class Go(BaseLanguage):
@@ -151,10 +176,19 @@ <h1 class="title">Module <code>continuous_delivery_scripts.plugins.golang</code>
151176 """States whether the repository must be cleaned before packaging happens."""
152177 return True
153178
179+ def tag_release(self, git: GitWrapper, version: str) -> None:
180+ """Tags release commit."""
181+ super().tag_release(git, version)
182+ go_tag = _determine_go_module_tag(self.get_version_tag(version))
183+ if go_tag:
184+ git.create_tag(go_tag, message=f"Golang module release: {go_tag}")
185+
154186 def _call_goreleaser_release(self, version: str) -> None:
155187 """Calls go releaser release to upload packages."""
156188 logger.info("Installing GoReleaser if missing.")
157- check_call(_install_goreleaser_command_list())
189+ env = os.environ
190+ env[ENVVAR_GO_MOD] = GO_MOD_ON_VALUE
191+ check_call(_install_goreleaser_command_list(), env=env)
158192 tag = self.get_version_tag(version)
159193 # The tag of the release must be retrieved
160194 # See https://github.com/goreleaser/goreleaser/discussions/1426
@@ -165,7 +199,6 @@ <h1 class="title">Module <code>continuous_delivery_scripts.plugins.golang</code>
165199 git.checkout(f"tags/{tag}")
166200 logger.info("Release package.")
167201 changelogPath = configuration.get_value(ConfigurationVariable.CHANGELOG_FILE_PATH)
168- env = os.environ
169202 env[ENVVAR_GORELEASER_CUSTOMISED_TAG] = tag
170203 env[ENVVAR_GORELEASER_GIT_TOKEN] = configuration.get_value(ConfigurationVariable.GIT_TOKEN)
171204 check_call(_generate_goreleaser_release_command_list(changelogPath), cwd=ROOT_DIR, env=env)</ code > </ pre >
@@ -238,10 +271,19 @@ <h2 class="section-title" id="header-classes">Classes</h2>
238271 """States whether the repository must be cleaned before packaging happens."""
239272 return True
240273
274+ def tag_release(self, git: GitWrapper, version: str) -> None:
275+ """Tags release commit."""
276+ super().tag_release(git, version)
277+ go_tag = _determine_go_module_tag(self.get_version_tag(version))
278+ if go_tag:
279+ git.create_tag(go_tag, message=f"Golang module release: {go_tag}")
280+
241281 def _call_goreleaser_release(self, version: str) -> None:
242282 """Calls go releaser release to upload packages."""
243283 logger.info("Installing GoReleaser if missing.")
244- check_call(_install_goreleaser_command_list())
284+ env = os.environ
285+ env[ENVVAR_GO_MOD] = GO_MOD_ON_VALUE
286+ check_call(_install_goreleaser_command_list(), env=env)
245287 tag = self.get_version_tag(version)
246288 # The tag of the release must be retrieved
247289 # See https://github.com/goreleaser/goreleaser/discussions/1426
@@ -252,7 +294,6 @@ <h2 class="section-title" id="header-classes">Classes</h2>
252294 git.checkout(f"tags/{tag}")
253295 logger.info("Release package.")
254296 changelogPath = configuration.get_value(ConfigurationVariable.CHANGELOG_FILE_PATH)
255- env = os.environ
256297 env[ENVVAR_GORELEASER_CUSTOMISED_TAG] = tag
257298 env[ENVVAR_GORELEASER_GIT_TOKEN] = configuration.get_value(ConfigurationVariable.GIT_TOKEN)
258299 check_call(_generate_goreleaser_release_command_list(changelogPath), cwd=ROOT_DIR, env=env)</ code > </ pre >
@@ -377,6 +418,7 @@ <h3>Inherited members</h3>
377418< li > < code > < a title ="continuous_delivery_scripts.utils.language_specifics_base.BaseLanguage.generate_source_licence_header_template " href ="../utils/language_specifics_base.html#continuous_delivery_scripts.utils.language_specifics_base.BaseLanguage.generate_source_licence_header_template "> generate_source_licence_header_template</ a > </ code > </ li >
378419< li > < code > < a title ="continuous_delivery_scripts.utils.language_specifics_base.BaseLanguage.should_clean_before_packaging " href ="../utils/language_specifics_base.html#continuous_delivery_scripts.utils.language_specifics_base.BaseLanguage.should_clean_before_packaging "> should_clean_before_packaging</ a > </ code > </ li >
379420< li > < code > < a title ="continuous_delivery_scripts.utils.language_specifics_base.BaseLanguage.should_include_spdx_in_package " href ="../utils/language_specifics_base.html#continuous_delivery_scripts.utils.language_specifics_base.BaseLanguage.should_include_spdx_in_package "> should_include_spdx_in_package</ a > </ code > </ li >
421+ < li > < code > < a title ="continuous_delivery_scripts.utils.language_specifics_base.BaseLanguage.tag_release " href ="../utils/language_specifics_base.html#continuous_delivery_scripts.utils.language_specifics_base.BaseLanguage.tag_release "> tag_release</ a > </ code > </ li >
380422</ ul >
381423</ li >
382424</ ul >
0 commit comments