11
11
from continuous_delivery_scripts .utils .language_specifics_base import BaseLanguage , get_language_from_file_name
12
12
from continuous_delivery_scripts .spdx_report .spdx_project import SpdxProject
13
13
from 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
15
15
16
16
logger = logging .getLogger (__name__ )
17
17
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 )) )
20
20
ENVVAR_GORELEASER_GIT_TOKEN = "GITHUB_TOKEN"
21
21
ENVVAR_GORELEASER_CUSTOMISED_TAG = "GORELEASER_CURRENT_TAG"
22
+ ENVVAR_GO_MOD = "GO111MODULE"
23
+ GO_MOD_ON_VALUE = "on"
22
24
23
25
24
26
def _generate_golds_command_list (output_directory : Path , module : str ) -> List [str ]:
@@ -53,20 +55,43 @@ def _install_goreleaser_command_list() -> List[str]:
53
55
def _call_golds (output_directory : Path , module : str ) -> None :
54
56
"""Calls Golds for generating the docs."""
55
57
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 )
57
61
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 )
59
63
60
64
61
65
def _call_goreleaser_check (version : str ) -> None :
62
66
"""Calls go releaser check to verify configuration."""
63
67
logger .info ("Installing GoReleaser if missing." )
64
- check_call (_install_goreleaser_command_list ())
65
- logger .info ("Checking GoReleaser configuration." )
66
68
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." )
67
72
env [ENVVAR_GORELEASER_CUSTOMISED_TAG ] = version
68
73
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 } "
70
95
71
96
72
97
class Go (BaseLanguage ):
@@ -118,10 +143,19 @@ def should_clean_before_packaging(self) -> bool:
118
143
"""States whether the repository must be cleaned before packaging happens."""
119
144
return True
120
145
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
+
121
153
def _call_goreleaser_release (self , version : str ) -> None :
122
154
"""Calls go releaser release to upload packages."""
123
155
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 )
125
159
tag = self .get_version_tag (version )
126
160
# The tag of the release must be retrieved
127
161
# See https://github.com/goreleaser/goreleaser/discussions/1426
@@ -132,7 +166,6 @@ def _call_goreleaser_release(self, version: str) -> None:
132
166
git .checkout (f"tags/{ tag } " )
133
167
logger .info ("Release package." )
134
168
changelogPath = configuration .get_value (ConfigurationVariable .CHANGELOG_FILE_PATH )
135
- env = os .environ
136
169
env [ENVVAR_GORELEASER_CUSTOMISED_TAG ] = tag
137
170
env [ENVVAR_GORELEASER_GIT_TOKEN ] = configuration .get_value (ConfigurationVariable .GIT_TOKEN )
138
171
check_call (_generate_goreleaser_release_command_list (changelogPath ), cwd = ROOT_DIR , env = env )
0 commit comments