我正在使用Circle ci上的Maven liquibase插件运行数据库作业。我需要从AWS Parameter store中读取用户名,密码,dburl等参数,但是当我尝试将aws cli返回的值设置为自定义变量时,它始终为空/空。我知道该值存在,因为Mac终端上的同一命令返回一个值。
[我正在使用Bash脚本安装带有Circle ci job的AWS CLI。当我在.sh文件中回显密码时,我看到了该值,但是当我在config.yml中回显它时,我看到了一个空白的空值。我也尝试使用aws ssm和config.yml文件来获取值,但是即使那里的值也为空。
我的配置.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时,我希望看到该值,但是我看到空白的空字符串
根据their official documentation,您需要将[export foo = bar] echo放入$ BASH_ENV(这只是在bash会话启动时运行的文件):
因此在您的env-setup.sh文件中:
echo "export dbPasswordDev=$dbPassword" >> $BASH_ENV
这是更高级的情况,可以从.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