I'm trying to store ENV variable using CircleCi boxes. I usually do it like this:
echo 'export FILE=$(ls bin | head -n 1)' >> $BASH_ENV
That works perfectly for simple commands and it will produce output in $BASH_ENV
export FILE=$(ls bin | head -n 1)
Now for extended commands where I need to just run it once, this is complicated and it doesn't work.
echo 'export INSTANCE_ID=$(aws ec2 run-instances --instance-type t3.large\
--image-id $AMI --key-name circleci-key --count 1 --security-group-ids $SG\
--subnet-id $SUBNET --network-interfaces "{\"AssociatePublicIpAddress\": true, \"DeviceIndex\": 0, \"SubnetId\": \"$SUBNET\", \"Groups\": [\"$SG\"]}"\
--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=instance-from-circleci}]'\
| jq '.Instances[0].InstanceId' --raw-output)' >> $BASH_ENV
This code once added to $BASH_ENV is executed everytime I run source $BASH_ENV
.
Is there a way to simply store this value once to a variable and then add it to the echo line already in its final form?
Something like:
echo 'export $INSTANCE_ID' >> $BASH_ENV
bin/
: do not parsels
, it's bound to run into a problem when names with spaces or newlines appear. What's the idea behindls bin | head -n1
? – Marcus Müller Jan 26 '22 at 15:24$BASH_ENV
? It feels like something that's never useful for an interactive shell. – Marcus Müller Jan 26 '22 at 15:28"
instead of'
. – ctx Jan 26 '22 at 15:40