Echo/Evaluate external variable to slack orb

@barywhyte,

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.

So in your case, I suggest:

  - run:
      name: Populate MESSAGE variable
      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

  - slack/notify:
      custom: |
        {"blocks": [{"type": "section", "text": {"type": "mrkdwn", "text": "$MESSAGE" }}]}

 
Let me know if this helps.

@yannCI ,

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

main.tf

terraform {
  required_version = "~> 0.12"
  backend "gcs" {
    bucket      = "terraform-state"
    prefix      = "terraform/state"
    credentials = ".keys/sa.json"
  }
}
provider "google" {
  credentials = file(".keys/sa.json")
  project     = "my-project"
  region      = "us-central1"
}

resource "google_container_cluster" "gke-cluster" {
  name                     = "ob-cluster"
  location                 = "us-central1-a"
  remove_default_node_pool = true
  initial_node_count = 1
}

resource "google_container_node_pool" "preemptible_node_pool" {
  name     = "default-pool"
  location = "us-central1-a"
  cluster  = google_container_cluster.gke-cluster.name
  node_count = 3

  node_config {
    preemptible  = true
    machine_type = "n1-standard-1"
    oauth_scopes = [
      "storage-ro",
      "logging-write",
      "monitoring"
    ]
  }
}

resource "google_storage_bucket" "static-site-orderbird" {
  name          = "static-site-bucket"
  location      = "US"

  uniform_bucket_level_access = true

  website {
    main_page_suffix = "index.html"
    not_found_page   = "404.html"
  }
}

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.

Thank you very much @yannCI for your help.

1 Like

Hi @barywhyte,

Just to let you know I’m working on the Support ticket you submitted.

1 Like

Hi @yannCI ,

Thank you. I already replied

1 Like