I am trying to create an .env file on my remote AWS instance using a Github Workflow file. I am using the following command to set the contents of the .env file:
- name: Setting Github env vars to variable
run: |
echo "DEV_ENV_VAR<<EOF" >> $GITHUB_ENV
echo "$VARS_JSON" | jq -r 'keys[] as $k | "\($k)=\(.[$k])"' >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
This creates a multiline string of data that needs to be put into an .env file. I am then trying to do the following command:
unbuffer aws ssm start-session --parameters command="cd /web/app; git pull; yarn install; echo ${{ env.DEV_ENV_VAR }} >> .env; yarn build;" --target '${{ env.MY_TARGET }}' --document-name "AWS-StartInteractiveCommand";
But it doesn't seem to write anything into the .env file, and gives a syntax error if I am using a -e parameter for echo command.
How do I correctly achieve sending the multiline string in the parameters? Is there perhaps another way of achieving my goal?
My whole workflow file looks like this right now:
name: Update Frontend
on:
push:
branches:
- develop
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
permissions:
id-token: write
contents: read
jobs:
deploy:
runs-on: ${{ matrix.operating-system }}
strategy:
matrix:
operating-system: [ubuntu-20.04]
environment: dev
env:
DEPLOY_USERNAME: ubuntu
DEPLOY_SERVER: (my AWS instance ID here)
DEPLOY_PATH: /deploy/path
VARS_JSON: ${{ toJSON(vars) }}
steps:
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v2
with:
aws-region: eu-west-1
role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }}
- name: Setting Github env vars to variable
run: |
echo "DEV_ENV_VAR<<EOF" >> $GITHUB_ENV
echo "$VARS_JSON" | jq -r 'keys[] as $k | "\($k)=\(.[$k])"' >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
- name: Running commands on server
run: |
sudo apt-get update;
sudo apt-get install expect -qq > /dev/null;
echo "Starting execution of commands";
unbuffer aws ssm start-session --parameters command="cd ${{ env.DEPLOY_PATH }}; git pull; yarn install --frozen-lockfile; echo ${{ env.DEV_ENV_VAR }} >> .env; yarn build; pm2 restart all;" --target '${{ env.DEPLOY_SERVER }}' --document-name "AWS-StartInteractiveCommand";
.envin one line. Next step should be able to see that. That's what I'm referring to.echo "$VARS_JSON" | jq -r 'keys[] as $k | "\($k)=\(.[$k])"' >> .env, it'll only be one line and you don't have addecho ${{ env.DEV_ENV_VAR }} >> .envin the next step.