How to Archive Workflow Logs as an Artifact in GitHub Actions
Tee command output to log files, then upload the logs directory with if: always() so failures keep it.
Redirect step output to files under a logs/ directory, then upload that directory with actions/upload-artifact gated on if: always().
Steps
- Write command output to files under
logs/. - Upload the
logs/directory as an artifact. - Gate the upload with
if: always()so failures still keep logs.
Workflow
.github/workflows/ci.yml
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: |
mkdir -p logs
npm ci 2>&1 | tee logs/install.log
npm test 2>&1 | tee logs/test.log
- name: Archive logs
if: always()
uses: actions/upload-artifact@v4
with:
name: run-logs
path: logs/
retention-days: 14Gotchas
- Piping to
teecan mask the exit code; setpipefailin the shell if the install or test status must propagate. - Do not log secrets to files, since artifacts are not masked the way live log output is.
Related guides
How to Set Artifact Retention Days in GitHub ActionsControl how long a GitHub Actions artifact is kept with the retention-days input on actions/upload-artifact,…
How to Attach Screenshots From a Failed Test in GitHub ActionsUpload Playwright or Cypress screenshots and traces captured when a UI test fails as a GitHub Actions artifac…