Skip to main content

Continuing our Slack trend from the previous blogpost, today we will be focusing on another topic aimed at helping the developers and DevOps engineers detect and diagnose issues.

CI/CD pipelines are essential for modern DevOps workflows. But when something fails, developers often have to log into GitLab, find the job, and scroll through logs to figure out what went wrong. Wouldn’t it be more efficient to push the full pipeline logs directly to Slack?

In this post, I’ll walk you through how to automatically send full GitLab pipeline logs to a Slack channel whenever a job runs — whether it succeeds or fails.

🎯 Why Send Logs to Slack?

There are several reasons why pushing logs to Slack can be useful:

  • Immediate visibility: Developers see issues as soon as they occur.
  • Faster debugging: Logs show exactly where a failure happened.
  • Audit trail: Slack history becomes a lightweight record of CI activity.

While GitLab’s native Slack integration supports pipeline status updates, it doesn’t send actual logs. To do that, we’ll write a custom script within .gitlab-ci.yml.

🛠️ Prerequisites

Before we begin, make sure you have:

  • A Slack Bot Token
    More information can be found here.
  • A GitLab repository with a .gitlab-ci.yml file.
  • Proper permissions to edit your project pipeline configuration.

⚙️ The General Strategy

  1. Capture the job logs using GitLab’s CI/CD runtime features.
  2. Format the logs for readability (Slack supports Markdown).
  3. Send the content to Slack using a curl POST request with a JSON payload.

Since GitLab doesn’t provide an environment variable to directly access the job logs, the trick is to tee the output into a file and post that content.

🧪 Example Pipeline Job

Below is a sample job that runs a set of commands, saves the log output, and posts it to Slack using curl.

default:
  after_script:
    - |
        echo "Job finished. Sending notification to slack channel"
        sleep 20
        curl -H "PRIVATE-TOKEN: $PRIVATE_TOKEN" \
            --output "logs.txt" \
            "https://gitlab.com/api/v4/projects/$CI_PROJECT_ID/jobs/$CI_JOB_ID/trace"
        export FILE_PATH="logs.txt"
        FILENAME=$(basename "$FILE_PATH")
        FILE_SIZE=$(stat -c%s logs.txt)
        UPLOAD_RESPONSE=$(curl -s -F files=@"$FILENAME" -F filename="$FILENAME" -F token=$SLACK_TOKEN -F length=$FILE_SIZE https://slack.com/api/files.getUploadURLExternal)
        UPLOAD_URL=$(echo "$UPLOAD_RESPONSE" | jq -r '.upload_url')
        FILE_ID=$(echo "$UPLOAD_RESPONSE" | jq -r '.file_id')
        curl -X POST "$UPLOAD_URL" \
          -F "file=@logs.txt" \
          -F "token=$SLACK_TOKEN" \
          -F "file_id=$FILE_ID"
        curl -X POST "https://slack.com/api/files.completeUploadExternal" \
          -H "Authorization: Bearer $SLACK_TOKEN" \
          -H "Content-Type: application/json; charset=utf-8" \
          -d '{
                "files": [
                  {
                    "id": "'"$FILE_ID"'",
                    "title": "Job logs - '"$CI_JOB_ID"'"
                  }
                ],
                "channel_id": "'"$CHANNEL_ID"'",
                "initial_comment": "🚨 *Pipeline Failed* 🚨\n\n*Repository:* '"$CI_PROJECT_TITLE"'\n*Pipeline:* <'"$CI_PIPELINE_URL"'>\n*Job:* '"$CI_JOB_NAME"'\n*Commit:* '"$CI_COMMIT_SHA"' by '"$CI_COMMIT_AUTHOR"'\n*Branch/Tag:* '"$CI_COMMIT_REF_NAME"'\n\nPlease check the attached logs for more details."
              }'
      fi

Make sure to replace/define all the variables needed (e.g., PRIVATE_TOKEN, SLACK_TOKEN).

💡 Tips and Considerations

  • Security: Don’t expose secrets in logs. Mask or filter sensitive content before sending.
  • Performance: Posting logs might take a few seconds. If needed, separate the log-pushing logic into a post-processing stage to keep your main job fast.

✅ Example Use Cases

  • Notifying a Slack channel when a nightly build fails, including logs.
  • Sending test failure details to a QA Slack channel.
  • Creating audit records for CI runs in a private Slack channel.

🏁 Final Thoughts

Sending full pipeline logs to Slack can drastically improve visibility and reduce time to diagnose issues. With just a few lines of script, you can bridge the gap between GitLab and Slack and bring your CI/CD insights where your team already collaborates.

If you’re interested in extending this, you could:

  • Integrate with a log storage service like S3 or GCS
  • Include environment metadata like $CI_COMMIT_BRANCH or $CI_PIPELINE_SOURCE for context

Leave a Reply


The reCAPTCHA verification period has expired. Please reload the page.