如何在Circle CI中设置环境变量

时间:2019-02-02 13:05:40

标签: shell aws-cli circleci circleci-2.0 circleci-workflows

我正在使用ci上的Maven liquibase插件运行数据库作业。我需要从AWS Parameter store中读取用户名,密码,dburl等参数,但是当我尝试将aws cli返回的值设置为自定义变量时,它始终为空/空。我知道该值存在,因为在MAC终端返回相同的命令的值。

我使用的是bash脚本job.When我在sh文件我看到的价值,但是当我赞同它在我的config.yml我看空白空值显示密码与圆CI安装AWS CLI。我也尝试使用AWS SSM与config.yml文件,但即使有值为空空来获取值。

我的Config.yml

version: 2

references:
defaults: &defaults
working_directory: ~/tmp

environment:
  PROJECT_NAME: DB Job

build-filters: &filters 
filters:
  tags:
    only: /^v[0-9]{1,2}.[0-9]{1,2}.[0-9]{1,2}-(dev)/
  branches:
    ignore: /.*/
jobs:
checkout-code:
<<: *defaults

docker:
  - image: circleci/openjdk:8-jdk-node

steps: 
  - attach_workspace:
       at: ~/tmp

  - checkout

  - restore_cache:
      key: cache-{{ checksum "pom.xml" }}

  - save_cache:
      paths:
        - ~/.m2
      key: cache-{{ checksum "pom.xml" }}

  - persist_to_workspace:
      root: ~/tmp
      paths: ./

build-app:
<<: *defaults

docker:
  - image: circleci/openjdk:8-jdk-node


steps:
  - attach_workspace:
       at: ~/tmp

  - restore_cache:
      key: cache-{{ checksum "pom.xml" }}

  - run: chmod 700 resources/circleci/*.sh   
  - run: 
      name: Getting DB password
      command: resources/circleci/env-setup.sh

  - run: echo 'export ENV="$(echo $CIRCLE_TAG | cut -d '-' -f 2)"' >> $BASH_ENV
  - run: echo $ENV
  - run: echo $dbPasswordDev
  - run: export PASS=$(aws ssm get-parameters --names "/enterprise/org/dev/spring.datasource.password" --with-decryption --query "Parameters[0].Value" | tr -d '"') >> $BASH_ENV
  - run: echo $PASS
  - run: mvn resources:resources liquibase:update -P$ENV,pre-release


workflows:
version: 2
build-deploy:
jobs:
  - checkout-code:
      <<: *filters



  - build-app:
      requires:
        - checkout-code

      <<: *filters

env-setup.sh

sudo apt-get update
sudo apt-get install -y python-pip python-dev
sudo pip install awscli


aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID
aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY
aws configure set aws_region $AWS_DEFAULT_REGION


dbPassword=`aws ssm get-parameters --names "/enterprise/org/dev/spring.datasource.password" --with-decryption --query "Parameters[0].Value" | tr -d '"' `
echo "dbPassword = ${dbPassword}"

export dbPasswordDev=$dbPassword >> $BASH_ENV
echo $"Custom = $dbPasswordDev"

当我在env-set.sh中回显$ dbPasswordDev时,我看到了该值,但是在config.yml中却看不到该值,并且看到了空白/空字符串。此外,当我试图呼应与config.yml我希望看到,但是我看到了价值$ PASS空白空字符串

2 个答案:

答案 0 :(得分:1)

根据 their official documentation,您需要回显“导出foo = bar”到$ BASH_ENV(这只是在bash会话开始时运行的文件):

在您的env-setup.sh文件中:

echo "export dbPasswordDev=$dbPassword" >> $BASH_ENV

答案 1 :(得分:0)

这是更高级的情况,可以从.env文件中加载变量。

version: 2
aliases:

  - &step_process_dotenv
      run:
        name: Process .env file variables
        command: echo "export $(grep -v '^#' .env | xargs)" >> $BASH_ENV

jobs:
  build:
    working_directory: ~/project
    docker:
      - image: php
    steps:
      - checkout
      - *step_process_dotenv

带有测试的源存储库:https://github.com/integratedexperts/circleci-sandbox/tree/feature/dotenv

CI运行结果:https://circleci.com/gh/integratedexperts/circleci-sandbox/14