In Jenkins, I want to upload the current build log to appraise if a build fails
- User
In Jenkins, I want to upload the current build log to appraise if a build fails
Assistant
To upload the current build log to a remote location (such as a server, storage service, or messaging platform) in Jenkins when a build fails, you can use a post-build step or a pipeline script. The method you choose will depend on whether you're using Jenkins' traditional freestyle projects or Jenkins pipelines (scripted or declarative). Below are examples for both approaches.
For Freestyle Projects
-
Configure your build steps as usual.
-
Add a post-build action for failed builds:
- Go to the Post-build Actions section.
- Add the Post-build task plugin (if not already installed, you'll need to install it from the Jenkins plugin manager).
- Configure the post-build task to execute only if the build fails.
- Use a script or a command to upload the log. For example, if you're using a shell script to upload to an S3 bucket via the AWS CLI, your script might look like this:
Replacesh
aws s3 cp ${BUILD_LOG} s3://your-bucket/path/${BUILD_NUMBER}.log${BUILD_LOG}with the actual path to the log file (e.g.,${JENKINS_HOME}/jobs/your-job/builds/${BUILD_ID}/log) or use thecatcommand to read the log content and pipe it directly to the upload command. Replaceyour-bucketandyour-pathwith your actual S3 bucket and path.
-
Save your configuration and run the build.
For Jenkins Pipelines (Scripted or Declarative)
Scripted Pipeline
groovynode { // Your build steps here try { // Build commands } catch (err) { // Upload the log on failure sh 'aws s3 cp ${BUILD_LOG} s3://your-bucket/path/${BUILD_NUMBER}.log' throw err } }Declarative Pipeline
groovy-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.