Store text from the CI job log in a variable

Question

Is it possible to store text from the CI job log in a variable (or otherwise) in order to integrate it into a Slack notification (via webhook)?

Background: The job runs a python script which outputs something like “5 out of 10 returned path points were inside of the track”. The aim is to store this line from the job log in a variable. The variable is then integrated into a webhook, which sends a Slack notification. The (simplified) job configuration can be seen below.

Configuration

test script:
  stage: test
  image: docker image
  script:
    - python script
  after_script:
    - >
      if [ "$CI_JOB_STATUS" == "success" ]; then
        EMOJI_STATUS=":white_check_mark:"
      else
        EMOJI_STATUS=":exclamation:"
      fi;
      curl -X POST -H 'Content-type: application/json' --data "{
        \"text\": \"*Project:* $CI_PROJECT_NAME\n*Stage:* $CI_JOB_STAGE\n*Job:* $CI_JOB_NAME\n*User:* $GITLAB_USER_NAME\n*Status:* $CI_JOB_STATUS_UPPER $EMOJI_STATUS\n*Test Result:* \n*Details:* <$CI_JOB_URL|Job Details>\",
        \"mrkdwn\": true
      }" $SLACK_WEBHOOK;

Version

  • Self-managed
  • GitLab.com SaaS
  • Self-hosted Runners

Version

  • GitLab: 16.9