Hey so this has been killing me for couple of days, I feel I'm missing a minor syntax.
I want to send a slack message with test status from the gitlab pipeline. I'm using stage:allure to get these values to env file as an artifact. Another stage:send-slack-message to use the values to send slack message.
allure:
stage: allure
tags:
- testrunner
needs:
- combine-results #this is a previous stage
image:
name: mycomp.io/test-allure:latest
pull_policy: always
before_script:
- apk update
- apk add jq
- apk add curl
- apk upgrade
script:
- TESTS_PASSED=$(curl --header PRIVATE-TOKEN:" "ab $PIPELINE_URL | jq .total.success)
- TESTS_FAILED=$(curl --header PRIVATE-TOKEN:" "ab $PIPELINE_URL | jq .total.failed)
- echo "TESTS_PASSED=$TESTS_PASSED" >> test_stats.env
- echo "TESTS_FAILED=$TESTS_FAILED" >> test_stats.env
- python /allure_report.py
artifacts:
paths:
- test_stats.env
After this job completes, i'm able to view the env file as an artifact and it has the following values:
TESTS_PASSED=15
TESTS_FAILED=1
NOTE: there are no blank spaces they seem to be on next line for each values
My problem is here in the following job to send the slack message by using the below custom variable:
send-slack-message:
extends:
- .slack-send-message-custom
stage: alert-slack
when: on_success
needs:
- job: allure
artifacts: true
tags:
- testrunner
before_script:
- source test_stats.env # Load environment variables
- echo "Tests passed count-" $TESTS_PASSED #this value is printed as 15 in console
variables:
CUSTOM_INPUT: ":gitlab:\n
- Tests Branch: `$CI_COMMIT_REF_NAME` \n
- *App*: $values__app__tag \n
- *Applite*: $values__applite__image__tag \n
- *Url*: <$CI_PIPELINE_URL|*here*> \n
- *Passed Tests*: '${TESTS_PASSED}'\n
- *Failed Tests*: ${TESTS_FAILED}\n
- *Results URL*: <$CI_PIPELINE_URL/test_report|*here*> \n "
So everything works, slack is message as given in the variable except the "TESTS_PASSED" and "TESTS_FAILED" variable that is sent from the previous job. These are being sent in the message as empty values. How to fix this?
$values__app__tag this variable is sent in as part of the pipleline.
Any guidance is highly appreciated.
The problem is the variables section is evaluated before the before_script runs. The before_script is setting the TESTS_PASSED and TESTS_FAILED variables correctly, as can be seen from the echo statement. However the variables section is evaluated first.
What you can do to get around this is set the CUSTOM_INPUT variable in the before_script like;
send-slack-message:
before_script:
- |
source test_stats.env
export CUSTOM_INPUT=":gitlab:\n
- Tests Branch: `$CI_COMMIT_REF_NAME` \n
- *App*: $values__app__tag \n
- *Applite*: $values__applite__image__tag \n
- *Url*: <$CI_PIPELINE_URL|*here*> \n
- *Passed Tests*: '${TESTS_PASSED}'\n
- *Failed Tests*: ${TESTS_FAILED}\n
- *Results URL*: <$CI_PIPELINE_URL/test_report|*here*> \n "
If you add echo $CUSTOM_INPUT
to the script in .slack-send-message-custom
you will see that CUSTOM_INPUT has been set.