@@ -88,64 +88,73 @@ def call(Map config = [:]) {
88
88
echo " publishBuildStatusReport - Data for shell script: Controller='${ controllerHostname} ', Job='${ jobName} ', Build='${ buildNumber} ', Status='${ buildStatus} '"
89
89
90
90
// --- Step 2: Define the final path for the JSON report on the agent ---
91
- // This path is constructed in Groovy as it uses Jenkins context (WORKSPACE, controllerHostname, jobName).
92
- // The shell script will be responsible for creating this path and writing the file.
93
- String finalReportDirOnAgent = " ${ env.WORKSPACE} /build_status_reports/${ controllerHostname} /${ jobName} "
91
+ String finalReportDirRelative = " build_status_reports/${ controllerHostname} /${ jobName} "
94
92
String finalReportFileName = " status.json"
95
- String finalReportPathOnAgent = " ${ finalReportDirOnAgent} /${ finalReportFileName} "
96
- echo " publishBuildStatusReport - Shell script will be instructed to write final report to: ${ finalReportPathOnAgent} "
93
+ String finalReportPathRelative = " ${ finalReportDirRelative} /${ finalReportFileName} "
94
+ String finalReportPathAbsolute = " ${ env.WORKSPACE} /${ finalReportPathRelative} " // Absolute path for shell script
95
+ echo " publishBuildStatusReport - Shell script will be instructed to write final report to: ${ finalReportPathAbsolute} "
97
96
98
97
// --- Step 3: Prepare and deploy the utility shell script to the agent ---
99
- final String shellScriptResourcePath = ' io/jenkins/infra/pipeline/generateAndWriteBuildStatusReport.sh' // Assuming this is the agreed name and path
98
+ final String shellScriptResourcePath = ' io/jenkins/infra/pipeline/generateAndWriteBuildStatusReport.sh'
100
99
String shellScriptContent = libraryResource shellScriptResourcePath
101
100
102
- String tempScriptDir = " ${ env.WORKSPACE } / .jenkins-scripts"
101
+ String tempScriptDir = " .jenkins-scripts"
103
102
String tempScriptName = " exec_generate_report_${ env.BUILD_ID ?: System.currentTimeMillis()} .sh"
104
103
String tempScriptPathOnAgent = " ${ tempScriptDir} /${ tempScriptName} "
105
104
106
- // Ensure the temporary script directory exists
107
- sh " mkdir -p '${ tempScriptDir} '" // Single quotes for safety if tempScriptDir could have spaces (unlikely for .jenkins-scripts)
105
+ sh " mkdir -p '${ tempScriptDir} '"
108
106
109
107
writeFile file : tempScriptPathOnAgent, text : shellScriptContent
110
108
sh " chmod +x '${ tempScriptPathOnAgent} '"
111
109
echo " publishBuildStatusReport - Utility shell script deployed to agent at: ${ tempScriptPathOnAgent} "
112
110
113
111
// --- Step 4: Execute the utility shell script ---
114
- // Escape Groovy variables for safe inclusion as environment variable *values*.
115
112
String escController = controllerHostname. replaceAll(" '" , " '\\\\ ''" )
116
- String escJobName = jobName. replaceAll(" '" , " '\\\\ ''" ) // jobName can have '/' - this escaping handles single quotes within.
113
+ String escJobName = jobName. replaceAll(" '" , " '\\\\ ''" )
117
114
String escBuildNumber = buildNumber. replaceAll(" '" , " '\\\\ ''" )
118
115
String escBuildStatus = buildStatus. replaceAll(" '" , " '\\\\ ''" )
119
- // finalReportPathOnAgent is constructed from WORKSPACE and sanitized names, less likely to need escaping for this purpose,
120
- // but for consistency, we can escape it too.
121
- String escFinalReportPathOnAgent = finalReportPathOnAgent. replaceAll(" '" , " '\\\\ ''" )
116
+ String escFinalReportPathOnAgent = finalReportPathAbsolute. replaceAll(" '" , " '\\\\ ''" )
122
117
123
118
withEnv([
124
119
" ENV_CONTROLLER_HOSTNAME=${ escController} " ,
125
120
" ENV_JOB_NAME=${ escJobName} " ,
126
121
" ENV_BUILD_NUMBER=${ escBuildNumber} " ,
127
122
" ENV_BUILD_STATUS=${ escBuildStatus} " ,
128
- " ENV_TARGET_JSON_FILE_PATH=${ escFinalReportPathOnAgent} " // Pass the target path
123
+ " ENV_TARGET_JSON_FILE_PATH=${ escFinalReportPathOnAgent} "
129
124
]) {
130
125
echo " publishBuildStatusReport - Executing utility shell script: '${ tempScriptPathOnAgent} '"
131
- // Execute the script. It will handle its own file I/O. No stdout capture.
132
126
sh " '${ tempScriptPathOnAgent} '"
133
127
}
134
128
echo " publishBuildStatusReport - Utility shell script execution completed."
135
129
136
- // --- Step 5: Display generated status.json from workspace (FOR DEBUGGING/VERIFICATION) ---
137
- echo " publishBuildStatusReport - Displaying content of generated report file: ${ finalReportPathOnAgent} "
138
- sh " cat '${ finalReportPathOnAgent} '"
139
-
140
- // --- Step 5: Publish the report (which was created by the shell script) ---
141
- // No Groovy-side validation of file content here, as per "no unnecessary validations".
142
- // We trust the shell script did its job if the 'sh' call above didn't fail (due to set -e in shell script).
143
- // A fileExists check could be added here if deemed essential minimal validation.
144
- echo " publishBuildStatusReport - Publishing report from: ${ finalReportPathOnAgent} "
145
- publishReports([finalReportPathOnAgent])
146
-
147
- // Cleanup of the temporary utility shell script is omitted as per last discussion.
148
- // Jenkins workspace cleanup will eventually remove it.
130
+ // --- Step 5: Publish the generated report using azcopy ---
131
+ final String azureBaseUrl = " https://buildsreportsjenkinsio.file.core.windows.net/builds-reports-jenkins-io"
132
+ final String remotePath = finalReportPathRelative
133
+ final String fullDestinationUrl = " ${ azureBaseUrl} /${ remotePath} "
134
+
135
+ echo " publishBuildStatusReport - Publishing local file '${ finalReportPathAbsolute} ' to remote destination '${ fullDestinationUrl} '"
136
+
137
+ // Escape variables for safe use inside the sh command string.
138
+ String escLocalPath = finalReportPathAbsolute. replaceAll(" '" , " '\\\\ ''" )
139
+ String escDestinationUrl = fullDestinationUrl. replaceAll(" '" , " '\\\\ ''" )
140
+
141
+ withEnv([" AZCOPY_LOCAL_PATH=${ escLocalPath} " , " AZCOPY_DESTINATION_URL=${ escDestinationUrl} " ]) {
142
+ sh'''
143
+ set -ex
144
+ echo "Attempting to publish report with azcopy..."
145
+
146
+ # Ensure any previous azcopy login is cleared to avoid conflicts.
147
+ azcopy logout 2>/dev/null || true
148
+
149
+ # Login using the agent's Managed Identity (credential-less).
150
+ azcopy login --identity
151
+
152
+ # Copy the local file to the remote destination.
153
+ azcopy copy "\$ AZCOPY_LOCAL_PATH" "\$ AZCOPY_DESTINATION_URL" --recursive
154
+
155
+ echo "azcopy copy command completed."
156
+ '''
157
+ }
149
158
150
159
echo " publishBuildStatusReport - Process completed for ${ jobName} #${ buildNumber} ."
151
160
}
0 commit comments