circlecicircleci-2.0circleci-workflows

CircleCI Using Sed with Parameters


I am trying to setup my project to deploy a go applicaiton. When I try to use sed with a parameter that is passed in from the job - it works fine When I try to use sed with a paramater that is passed in from the job which is referenced as a context variable it is not working. See this example

version: 2.1
jobs:
  deploy-application:
    docker:
      - image: circleci/golang:1.13
        environment:
          GO111MODULE: "on"

    working_directory: /go/src/github.com/me/go-circle-elasticbeanstalk
    parameters:
      ebAppName:
        description: "The name to set the elastic beanstalk application to"
        default: "null"
        type: string
      elkHost:
        description: "The host to send logs to for elastic search"
        default: "null"
        type: string
    steps:
      - run: sudo apt-get -y -qq update
      - run: sudo apt-get install python-pip python-dev build-essential
      - run: sudo pip install awsebcli --upgrade
      - checkout
      - run: sed -i 's/MY-FLIGHTS-APP-PLACEHOLDER/<< parameters.ebAppName >>/g' .elasticbeanstalk/config.yml
      - run: sed -i 's/FILEBEAT-HOST-PLACEHOLDER/<< parameters.elkHost >>/g' .ebextensions/01_filebeat.config
      - run: ls -latr
      - run: go env
      - run: export GO111MODULE=on
      - run: go env
      - run: go build ./...
      - run: go build -o application server.go
      - run: ls -latr
      - run: ls -latr .elasticbeanstalk
      - run: zip my-server.zip application -r .ebextensions damien.test
      - run: ls -latr
      - run: eb deploy   
workflows:
    deploy-go-server:
      jobs:
      - deploy-application:
          name: deploy-dev
          ebAppName: my-flights-dev
          elkHost: $ELK_HOST
          filters:
            branches:
              only: develop
          context: my-dev
      - deploy-application:
          name: deploy-stage
          ebAppName: my-flights
          elkHost: $ELK_HOST
          filters:
            branches:
              only: stage
          context: my-stage

The value for ebAppName is always set correctly However, the value of elkHost always shows as $ELK_HOST and is never reading the organization context value

Any help on what I can do here to resolve this? I have used this approach on another project and it works fine I noticed that the value of elkHost is fine if I output to a file but just doesnt work with sed

Any help is appreciated

Cheers Damien


Solution

  • Use type env_var_name if you want to send parameter as var name, or resolve the parameter i.e ${EB_APP_NAME} when running the workflow if the parameter type is string.

    version: 2.1
    jobs:
       build:
         parameters:
           elkHost:
             type: env_var_name
             default: ELK_DEFAULT_HOST # Note: replace with env var (without $) or remove it if the param is mandatory.
           ebAppName:
             type: string
             default: 'my-app-name'
         docker:
           - image: ubuntu:latest
         steps:
           - run: |
               echo "--elkHost ${<< parameters.elkHost >>} \\
                     --ebAppName << parameters.ebAppName >>"
    workflows:
      workflow:
        jobs:
          - build:
              elkHost: ELK_HOST
              ebAppName: ${EB_APP_NAME}