-
Notifications
You must be signed in to change notification settings - Fork 4
/
zanata-nexus-release
executable file
·202 lines (179 loc) · 6.49 KB
/
zanata-nexus-release
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/bin/bash
### NAME
### zanata-nexus-release - Release maven artifacts in nexus-staging
###
### SYNOPSIS
### zanata-nexus-release [options] [Projects]
###
### DESCRIPTION
### This script releases artifacts that already in nexus staging.
###
### Note that this script assume you are already in correct directory and
### checkout correct branch.
###
### plugin docs:
### https://github.com/sonatype/nexus-maven-plugins/tree/master/staging/maven-plugin
###
### ENVIRONMENT
### ZANATA_RELEASE_MODE:
### <empty> : Default mode. Builds, deploy to nexus staging, and push changes
### to source control
### testBuild: Builds, deploy to nexus staging, but does not push changes to
### source control, and nor does it close the nexus staging repo
### dryRun : Only show command to be run.
: ${ZANATA_RELEASE_MODE:=}
: ${DryRunMode:=0}
export LC_ALL=C
set -eu
ScriptDir=$(dirname $(readlink -q -f $0))
FunctionScriptFile=$ScriptDir/zanata-functions
source "$FunctionScriptFile"
trap exit_print_error EXIT
##=== parsing Start ===
print_status -t parsing -s "Start"
RepoName=$(repo_name_get)
ArtifactId=$(get_artifact_id $RepoName)
Version=$(maven_project_version)
MavenReleaseProfiles=$(get_artifact_var_value $ArtifactId MAVEN_RELEASE_PROFILES)
## List all artifacts to be released in sonatype nexus
## e.g. !server/zanata-test-war,!server/functional-test
NexusReleaseProjects=$(get_artifact_var_value $ArtifactId MAVEN_NEXUS_RELEASE_PROJECTS )
## Staging repository prefix, e.g. orgzanata
NexusStagingRepositoryPrefix=$(get_artifact_var_value $ArtifactId STAGING_REPOSITORY)
## default values
Projects="$NexusReleaseProjects"
PushBranch=$(branch_get_releasing)
PushMode=0
PushRepository=origin
###
### OPTIONS
while getopts "hb:pr:s:" opt;do
case $opt in
### -h: Show detail help
h )
zanata_script_help $0
exit ${EXIT_OK}
;;
###
### -b: Branch to push
### Default: The RELEASING branch of the repository.
b )
PushBranch=$OPTARG
;;
###
### -p: Push commits and tag to Git
### Push when nexus staging repository is successfully closed
p )
PushMode=1
;;
###
### -r: Git repository for push
### Can be either an URL or name as defined in git remote.
### This is required when you need to pass credential with URL.
### Default: origin
r )
PushRepository="$OPTARG"
;;
###
### -s: Nexus Staging Repository Prefix
### Normally you do not need to specify this, unless you are dealing with new
### repository whose Nexus Staging Repository prefix is not in zanata-env.sh
s )
NexusStagingRepositoryPrefix=$OPTARG
;;
* )
failed ${EXIT_FATAL_INVALID_OPTIONS} "$opt"
;;
esac
done
shift $((OPTIND-1))
case $ZANATA_RELEASE_MODE in
dryRun )
DryRunMode=1
;;
testBuild )
DryRunMode=0
;;
* )
;;
esac
export DryRunMode
[[ -n NexusStagingRepositoryPrefix ]] || failed $EXIT_FATAL_INVALID_OPTIONS "Nexus staging repository prefix is empty. Please specify with option '-s', like: '-s orgzanata'"
[[ -n ${1:-} ]] && Projects="$1"
if [[ -n $Projects && $Projects != 'null' ]]; then
ProjectOpts="-pl $Projects"
else
ProjectOpts=""
fi
if [[ -n $MavenReleaseProfiles ]]; then
ReleaseProfileOpts=-P$MavenReleaseProfiles
else
ReleaseProfileOpts=
fi
## Minimize duplication of maven options invokes
run_maven(){
local goal=$1
shift
run_command ./mvnw $MAVEN_COMMON_OPTIONS ${MAVEN_NEXUS_STAGING_OPTIONS:-} $ReleaseProfileOpts -DstagingDescription="$ArtifactId:$Version" ${MAVEN_NEXUS_STAGING_PLUGIN}:$goal "$@"
}
## This function runs the staging plugin to fetch the repository id and state into these variables:
## StagingRepositoryId
## StagingRepositoryState
get_StagingRepository(){
## We only query the "root" project, which only takes 10 secs
## Querying on all projects takes 1:09 minutes.
local StagingRepositoryBuffer=$(run_maven rc-list -pl . | sed -r -n -e '/'$NexusStagingRepositoryPrefix'-.* / s/^\[INFO\] ('$NexusStagingRepositoryPrefix'-[0-9]+)\s*([A-Z]+)/\1 \2/ p' )
if [[ $DryRunMode -eq 0 ]]; then
if [[ -n $StagingRepositoryBuffer ]]; then
## Pick the latest Nexus staging
local stagingRepositoryLine=$( awk '{print $1,$2}' <<<"$StagingRepositoryBuffer" | sort -rV | head -n 1)
StagingRepositoryId=$( awk '{print $1}' <<<"$stagingRepositoryLine" )
StagingRepositoryState=$( awk '{print $2}' <<<"$stagingRepositoryLine" )
elif [[ $ZANATA_RELEASE_MODE = testBuild ]]; then
print_status " [testBuild] StagingRepositoryId is not necessary available"
StagingRepositoryId=StagingRepositoryId
StagingRepositoryState=OPEN
else
failed $EXIT_FATAL_FAIL " Failed to find artifacts in ${MAVEN_NEXUS_STAGING_PLUGIN}:rc-list"
fi
else
print_status " [Dry-Run] No StagingRepositoryId available in DryRunMode, use pseudo Id instead"
## Provide pseudo Id and state for display
StagingRepositoryId=StagingRepositoryId
StagingRepositoryState=OPEN
fi
print_status " StagingRepositoryId=$StagingRepositoryId StagingRepositoryState=$StagingRepositoryState"
}
##=== nexus-release Start ===
print_status -t nexus-release -s "Start"
get_StagingRepository
## TODO: Try -DautoReleaseAfterClose (and skip the release goal below)
if [ "$StagingRepositoryState" = "OPEN" ];then
print_status " nexus-staging:close"
## No closing in testBuild mode
if [[ $ZANATA_RELEASE_MODE = testBuild ]]; then
export DryRunMode=1
fi
set +e
run_maven close -DstagingRepositoryId=${StagingRepositoryId} $ProjectOpts
set -e
if [[ $ExitCode -eq 0 ]]; then
if [[ $PushMode -eq 1 ]]; then
## Push when success
run_command git_detached_merge_branch $PushBranch
run_command git push --follow-tags "$PushRepository" $PushBranch
fi
fi
if [[ $ZANATA_RELEASE_MODE = testBuild ]]; then
export DryRunMode=0
fi
fi
print_status " nexus-staging:release"
if [[ $ZANATA_RELEASE_MODE = testBuild ]]; then
export DryRunMode=1
fi
get_StagingRepository
if [ "$StagingRepositoryState" = "CLOSED" ];then
run_maven release -DstagingRepositoryId=${StagingRepositoryId} $ProjectOpts
fi
## No need to restore DryRunMode in end of the script