Regarding the behaviour you saw when using an alpine image, was it still happening after you added these two parameters to your job?
Now about the Slack message issue, I believe the syntax "text": "```$MESSAGE```" is incorrect. It should be "text": "$MESSAGE"; the backticks are already included if you follow the example I shared. And you’ll also need to make sure all potentially problematic characters are escaped.
Thank you for the response. Indeed I saw little change after applying this solution. The file message.txt appears to be normal text file unlike before where command file message.txt showed that the file is of type that I didn’t didn’t understand.
With regard to Alpine linux, YES, I included those lines at the job level. The result was the same.
Anyway, I am not really concerned about that anymore. For the solution that you shared, the result remain the same. I have not been able to get this message sent to slack. Here is my complete config.
config.yml
version: 2.1
orbs:
slack: circleci/slack@4.4.4
jobs:
terraform-plan:
docker:
- image: circleci/node:14.17-browsers
steps:
- checkout
- run:
name: Install terraform
working_directory: terraform
command: |
# Download Terraform version 0.12.31
wget https://releases.hashicorp.com/terraform/0.12.31/terraform_0.12.31_linux_amd64.zip
unzip terraform_0.12.31_linux_amd64.zip && rm terraform_0.12.31_linux_amd64.zip
sudo mv terraform /usr/bin/terraform
- run:
name: Create Service Account key file from environment variable
working_directory: terraform
command: mkdir .keys && echo ${TF_ACCOUNT_KEY} > .keys/sa.json
- run:
name: Show Terraform version
command: terraform version
- run:
name: Download required Terraform plugins
working_directory: terraform
command: terraform init
- run:
name: Validate Terraform configuration
working_directory: terraform
command: terraform validate
- run:
name: Run Terraform plan and show
working_directory: terraform
command: |
terraform plan -out /tmp/tf.plan
terraform show /tmp/tf.plan | grep -E "\#" | awk '{printf "%s\\n", $0}'|sed 's/\"/\\"/g' > message.txt
#echo 'export MESSAGE="Changes to be applied from terraform: \`\`\`$(cat message.txt)\`\`\`" ' >> $BASH_ENV
echo 'export MESSAGE="Changes to be applied from terraform: $(cat message.txt)" ' >> $BASH_ENV
source $BASH_ENV
- run:
name: Echo detailed-tfplan and summary-tfplan
working_directory: terraform
command: |
printf "Echo Detailed TFPLAN\n"
echo ${MESSAGE} #Note that this step was successfully echoed out meaning `$BASH_ENV` persist data across `run` steps
- slack/notify:
channel: seun_cci_notify
event: pass
custom: |
{"blocks": [{"type": "section", "text": {"type": "mrkdwn", "text": "$MESSAGE" }}]}
- slack/notify:
channel: seun_cci_notify
event: fail
template: basic_fail_1
workflows:
main:
jobs:
- terraform-plan:
context: seun_cci_notify
I am not very sure what I am missing here. I do not know of any other character that need to be escaped. I really will appreciate if this more details could get you closer to figure this out. I am warned out trying to figure out this.
Just came here to say thanks @yannCI, that helped us diagnose a similar issue with one of our projects (a missing double quote that had been inadvertently removed in a past commit). For anyone facing the same problem, tldr: paste your JSON into the Slack block kit builder and it will show you where you messed up!