Wednesday, August 31, 2022

[SOLVED] bash: -c: line 0: unexpected EOF while looking for matching ``'

Issue

I am trying to build a deployment pipeline (similar to GitHub actions) for a python Django project managed in Bitbucket. The pipeline consists of two steps (actions),

  1. Build a docker image and push it to Amazon ECR (No issues here)
  2. SSH into the deployment server, pull and deploy the image

Here is my deployment code that executes in the SSH session,

# deployment.sh

( echo $AWS_ACCESS_KEY_ID & echo $AWS_SECRET_ACCESS_KEY & echo $AWS_REGION & echo "" ) | aws configure

docker login -u AWS -p $( aws ecr get-login-password --region $AWS_REGION ) $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com
docker pull $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/usyd-sams

rm -r -f sams

if [[ $DEPLOYMENT == "staging" ]]
then
    git clone -b main --single-branch $GIT_URL
fi

if [[ $DEPLOYMENT == "production" ]]
then
    git clone -b develop --single-branch $GIT_URL
fi

( cd sams && docker-compose up -d)

Traceback,

INFO: Executing the pipe...
INFO: Using default ssh key
INFO: Executing script deployment.sh on ec2-0-0-0-0.west.compute.amazonaws.com
ssh -i /root/.ssh/pipelines_id -o StrictHostKeyChecking=no -p 22 [email protected] DEPLOYMENT=staging AWS_REGION=west AWS_ACCOUNT_ID=$AWS_ACCOUNT_ID AWS_ACCESS_KEY_ID=$AWS_KEY AWS_SECRET_ACCESS_KEY=$AWS_SECRET POSTGRES_USER=someuser POSTGRES_PASSWORD=$POSTGRES_PASSWORD POSTGRES_DB=somedb bash -s
Warning: Permanently added the ED25519 host key for IP address '0.0.0.0' to the list of known hosts.
bash: -c: line 0: unexpected EOF while looking for matching ``'
bash: -c: line 1: syntax error: unexpected end of file

The first step works flawlessly but the second seems to raise an error that I've been stuck with for hours now. Here is my pipeline configuration,

      - step:
          deployment: staging
          script:
            - pipe: atlassian/ssh-run:0.4.1
              variables:
                SSH_USER: $SSH_USER
                SERVER: $SSH_ADDRESS
                MODE: 'script'
                COMMAND: 'deployment.sh'
                ENV_VARS: >-
                  DEPLOYMENT=staging
                  AWS_REGION=${AWS_REGION}
                  AWS_ACCOUNT_ID=${AWS_ACCOUNT_ID}
                  AWS_ACCESS_KEY_ID=${AWS_KEY}
                  AWS_SECRET_ACCESS_KEY=${AWS_SECRET}
                  POSTGRES_USER=${POSTGRES_USER}
                  POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
                  POSTGRES_DB=${POSTGRES_DB}

Solution

This error:

bash: -c: line 0: unexpected EOF while looking for matching ``'
bash: -c: line 1: syntax error: unexpected end of file

Clearly indicates that whatever command is supposed to be feeding "deployment.sh" to bash, is not doing it correctly.

I would replace the whole "deployment.sh" script with a single echo "foobar" or something equally simple just to make sure that the script runs. Then, I would dump all the environment variables env to see if there's something funky.

Eventually check piece by piece that everything in "deployment.sh" is working correctly, and for starters others have already pointed out you have an issue at the beginning:

This:

echo $AWS_ACCESS_KEY_ID & echo $AWS_SECRET_ACCESS_KEY & echo $AWS_REGION & echo ""

Runs four commands in parallel, and the order of the output isn't guaranteed, which clearly would mess with aws configure.

Instead use a heredoc:

aws configure <<EOF
$AWS_ACCESS_KEY_ID
$AWS_SECRET_ACCESS_KEY
$AWS_REGION

EOF

This shouldn't affect the rest of the script though.



Answered By - FelipeC
Answer Checked By - David Goodson (WPSolving Volunteer)