Hello all.
We have an issue with CircleCI version: 2.1 as we cannot export properly the variables from within the container to Circle (slack orb ) . We receive blank spot in the slack notification ; I can see from the output from build results that variable has value in container before exporting.
Idea of doing this is to get results in the slack notification message directly as we do plenty of builds daily and to see at which job the build has failed .
I’ve used as example :
- Echo/Evaluate external variable to slack orb
2.Leveraging CircleCI API to include build logs in Slack notifications
Have used from Circle documentation : For alpine image
version: 2.1
jobs:
build:
shell: /bin/sh -leo pipefail
environment:
- BASH_ENV: /etc/profile
As suggested there , we also tried
echo “export SLACK_MESSAGE=‘${SLACK_MESSAGE}’” >> $BASH_ENV
We tried redirecting to :
1.>> ~/.bash_profile
2./etc/profile ( had permission issues so i had to adapt using : echo -e ‘export COMMITTER_NAME_VAR=“${COMMITTER_NAME}”’ | sudo tee -a $BASH_ENV )
3. ~/.profile ( as .bash_profile and .bash_login are missing )
Here SLACK_ERROR_MESSAGE is the custom variable while all the rest are the built-in from circleCI (https://circleci.com/docs/2.0/env-vars/#built-in-environment-variables) .
We have 2 cases: one from failed build and other from success.
Blockquote
jobs:
build:
<<: *defaults
shell: /bin/bash
environment:
- BASH_ENV: /etc/profile
…
Blockquote
Blockquote
version: 2.1
slack-fail-post-step: &slack-fail-post-step
post-steps:
- slack/notify:
channel: ‘builds, test-deploys’
custom: |
{
“text”: “”,
“blocks”: [
{
“type”: “section”,
“text”: {
“type”: “mrkdwn”,
“text”: “ Failure #${CIRCLE_BUILD_NUM}${CIRCLE_PROJECT_REPONAME}
on${CIRCLE_BRANCH}
,${SLACK_ERROR_MESSAGE}
executed by${CIRCLE_USERNAME}
”
}
},
{
“type”: “actions”,
“elements”: [
{
“type”: “button”,
“text”: {
“type”: “plain_text”,
“text”: “View Job”
},
“url”: “${CIRCLE_BUILD_URL}”
}
]
}
]
}
event: fail
orbs:
slack: circleci/slack@4.4.2
jira: circleci/jira@1.0.6
Blockquote
post-steps:
- slack/notify:
channel: ‘test-deploys’
custom: |
{
“text”: “”,
“blocks”: [
{
“type”: “section”,
“text”: {
“type”: “mrkdwn”,
“text”: “ Success #${CIRCLE_BUILD_NUM}${CIRCLE_PROJECT_REPONAME}
on${CIRCLE_BRANCH}
${COMMITTER_NAME_VAR}, ${COMMITTER_NAME} executed by${CIRCLE_USERNAME}
”
}
},
{
“type”: “actions”,
“elements”: [
{
“type”: “button”,
“text”: {
“type”: “plain_text”,
“text”: “View Job”
},
“url”: “${CIRCLE_BUILD_URL}”
}
]
}
]
}
event: always
Blockquote
- run:
name: Update and install jq and curl
command: |
sudo apt-get update
sudo apt-get install jq curl
- run:
name: Generate last log lines for slack message
command: |
echo circlebuild=$CIRCLE_BUILD_URL
API_URL=$(echo $CIRCLE_BUILD_URL | cut -d/ -f4-7)
echo apiurl=$API_URL
FAILED_STEP=$(curl “https://circleci.com/api/v1.1/project/${API_URL}?circle-token=${CIRCLE_API_TOKEN}” | jq ‘.steps | . | flatten | map(select(.status? == “failed”)) | . | {allocation_id, step, name}’)
echo failedstep=$FAILED_STEP
ALLOCATION_ID=$(echo “${FAILED_STEP}” | jq -r ‘.allocation_id’)
echo allocationid=$ALLOCATION_ID
STEP=$(echo “${FAILED_STEP}” | jq -r ‘.step’)
echo step=$STEP
COMMITTER_NAME=$(curl “https://circleci.com/api/v1.1/project/${API_URL}?circle-token=${CIRCLE_API_TOKEN}” | tail -n23 | grep committer_name | cut -f2- -d: | grep -o -P ‘(?<=“).*(?=”)’)
curl -o slack_response.txt “https://circleci.com/api/v1.1/project/${API_URL}/output/${STEP}/0?file=true&allocation-id=${ALLOCATION_ID}&circle-token=$CIRCLE_API_TOKEN”
sed -i ‘s/\r$//g’ slack_response.txt
SLACK_MESSAGE=$(cat slack_response.txt | tail -n 10 | sed ‘$!s/$/\n/’ | tr -d ‘\n’)
echo slackmessage=“$SLACK_MESSAGE”
echo commit_n=$COMMITTER_NAME
declare -n COMMIT=“John Doe”
export COMMIT_N=${COMMIT}
echo $COMMIT
echo $COMMIT_N
echo ‘export SLACK_ERROR_MESSAGE=“${SLACK_MESSAGE}”’ >> ~/.bash_profile
echo ‘export COMMITTER_NAME_VAR=“```${COMMITTER_NAME}```”’ >> ~/.bash_profile
set > env.out
source /etc/profile
source ~/.bash_profile
echo $COMMITTER_NAME_VAR
Latest tested code :
Blockquote
- run:
name: Update and install jq and curl
command: |
sudo apt-get update
sudo apt-get install jq curl
- run:
name: Generate last log lines for slack message
command: |
echo circlebuild=$CIRCLE_BUILD_URL
API_URL=$(echo $CIRCLE_BUILD_URL | cut -d/ -f4-7)
echo apiurl=$API_URL
FAILED_STEP=$(curl “https://circleci.com/api/v1.1/project/${API_URL}?circle-token=${CIRCLE_API_TOKEN}” | jq ‘.steps | . | flatten | map(select(.status? == “failed”)) | . | {allocation_id, step, name}’)
echo failedstep=$FAILED_STEP
ALLOCATION_ID=$(echo “${FAILED_STEP}” | jq -r ‘.allocation_id’)
echo allocationid=$ALLOCATION_ID
STEP=$(echo “${FAILED_STEP}” | jq -r ‘.step’)
echo step=$STEP
COMMITTER_NAME=$(curl “https://circleci.com/api/v1.1/project/${API_URL}?circle-token=${CIRCLE_API_TOKEN}” | tail -n23 | grep committer_name | cut -f2- -d: | grep -o -P ‘(?<=“).*(?=”)’)
echo $COMMITTER_NAME
curl -o slack_response.txt “https://circleci.com/api/v1.1/project/${API_URL}/output/${STEP}/0?file=true&allocation-id=${ALLOCATION_ID}&circle-token=$CIRCLE_API_TOKEN”
sed -i ‘s/\r$//g’ slack_response.txt
SLACK_MESSAGE=$(cat slack_response.txt | tail -n 10 | sed ‘$!s/$/\n/’ | tr -d ‘\n’)
echo slackmessage=“$SLACK_MESSAGE”
echo -e ‘export SLACK_ERROR_MESSAGE=“${SLACK_MESSAGE}”’ | sudo tee -a $BASH_ENV
echo -e ‘export COMMITTER_NAME_VAR=“${COMMITTER_NAME}”’ | sudo tee -a $BASH_ENV
echo ‘export commit=“John”’ | sudo tee -a $BASH_ENV
cat ~/.profile
echo $0
set > env.out
source /etc/profile
source ~/.profile
echo $COMMITTER_NAME_VAR
[[ $- == i ]] && echo ‘Interactive’ || echo ‘Not interactive’
shopt -q login_shell && echo ‘Login shell’ || echo ‘Not login shell’
echo $BASH_ENV
cat /etc/profile
- <<: *persist_workspace
- run:
name: Check for the variables
command: |
cat /etc/profile
echo $SLACK_MESSAGE
echo $COMMITTER_NAME
echo $SLACK_ERROR_MESSAGE
echo $COMMITTER_NAME_VAR
echo $commit
ls -a /home/circleci
echo $0
Results from Check for the variables step
Blockquote
“# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
“# and Bourne compatible shells (bash(1), ksh(1), ash(1), …).
if [ “id -u
” -eq 0 ]; then
PATH=”/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin”
else
PATH=“/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games”
fi
export PATH
if [ “${PS1-}” ]; then
if [ “${BASH-}” ] && [ “$BASH” != “/bin/sh” ]; then
# The file bash.bashrc already sets the default PS1.
# PS1=‘\h:\w$ ’
if [ -f /etc/bash.bashrc ]; then
. /etc/bash.bashrc
fi
else
if [ “id -u
” -eq 0 ]; then
PS1=’# ’
else
PS1='$ ’
fi
fi
fi
if [ -d /etc/profile.d ]; then
for i in /etc/profile.d/*.sh; do
if [ -r $i ]; then
. $i
fi
done
unset i
fi
export SLACK_ERROR_MESSAGE=“${SLACK_MESSAGE}”
export COMMITTER_NAME_VAR=“${COMMITTER_NAME}”
export commit=“John”
John
. .bash_logout .cache .local .node_repl_history .profile
… .bashrc .gitconfig .mozilla .npmrc .ssh
/bin/bash
Blockquote
echo $SLACK_MESSAGE
echo $COMMITTER_NAME
echo $SLACK_ERROR_MESSAGE
echo $COMMITTER_NAME_VAR
Give empty value already, while export commit=“John” is displayed but not exported to slack.
I am available to provide any other information needed . Thank you in advance.