You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Jenkins输出保存至文件并通过Shell命令上传至S3(无需构建后插件)

Answers to Jenkins Output Saving Questions

1. How to save Jenkins output to a file?

Jenkins has several simple ways to preserve build output:

  • Built-in log storage: Jenkins automatically keeps console logs for every build (you can view them via the build's "Console Output" page). If you need a persistent copy stored on the Jenkins server, use the Archive the artifacts post-build action (no extra plugins required for basic archiving) to save log files or generated output artifacts.

  • Shell command redirection: During your build steps, redirect output from individual commands or entire scripts to a file directly. Examples:

    # Redirect only standard output to a file (overwrites existing content)
    ./your-build-script.sh > build-output.log
    
    # Redirect both standard output and error to the same file
    ./your-build-script.sh &> build-output.log
    
    # Append output to a file instead of overwriting
    ./your-build-script.sh >> build-output.log
    

2. Save Jenkins output to an S3-uploadable file without post-build plugins

If you want to avoid post-build plugins and handle everything via shell commands, here's a practical method using the AWS CLI (ensure your Jenkins agent has AWS CLI installed and configured with S3 write permissions):

Step 1: Capture build output to a file

First, capture all output from your build commands—including errors—to a log file for complete visibility:

# Enable pipefail to ensure the script fails if any command inside fails
set -o pipefail  

{
  echo "=== Build started at $(date) ==="
  # Run your actual build commands here
  npm install
  npm run build
  echo "=== Build finished at $(date) ==="
} &> build-output.log

Step 2: Upload the file to S3

Use the AWS CLI s3 cp command to upload the log file to your S3 bucket. Add the Jenkins build number to the filename to avoid overwriting previous logs:

# Upload to S3 with unique build identifier
aws s3 cp build-output.log s3://your-bucket-name/jenkins-build-logs/build-${BUILD_NUMBER}-output.log

# Optional: Set access controls (e.g., make the log publicly readable)
aws s3 cp build-output.log s3://your-bucket-name/jenkins-build-logs/build-${BUILD_NUMBER}-output.log --acl public-read

Key Notes:

  • ${BUILD_NUMBER} is a Jenkins environment variable that auto-replaces with the current build's number—great for organizing logs chronologically.
  • Configure AWS credentials on the Jenkins agent via environment variables, ~/.aws/credentials, or IAM roles (if running on EC2 instances) to avoid hardcoding secrets.
  • If you need the full Jenkins console log (not just your shell step output), you can fetch it via the Jenkins API in a shell step, but this requires authentication. For most use cases, capturing build step output directly is sufficient.

内容的提问来源于stack exchange,提问作者Lawrence Johnson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 04:16:22