0

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";
12
  • Why not just create the file instead of setting it as an env var? Is the next command under the same job? Please add your workflow keeping its structure intact (only the relevant steps). Commented Jul 4, 2023 at 3:53
  • I can't send a file using aws-ssm command, can I? Commented Jul 4, 2023 at 6:48
  • Added the whole workflow, as suggested. Commented Jul 4, 2023 at 6:52
  • Not sure about that. Given your question, you can simply create an .env in one line. Next step should be able to see that. That's what I'm referring to. Commented Jul 4, 2023 at 6:52
  • With echo "$VARS_JSON" | jq -r 'keys[] as $k | "\($k)=\(.[$k])"' >> .env, it'll only be one line and you don't have add echo ${{ env.DEV_ENV_VAR }} >> .env in the next step. Commented Jul 4, 2023 at 6:53

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.