peterkimzz / aws-ssm-send-command

Github Actions for using AWS SSM Send-Command
MIT License
61 stars 32 forks source link

AWS SSM command gets failed always #8

Closed Jigar3 closed 3 years ago

Jigar3 commented 3 years ago

So, I have an Express server which I want to run on the EC2 instance.

Below is the deploy.yml file.

name: Deploy using AWS SSM Send-Command

on:
  push:
    branches: [master]
  pull_request:
    branches: [master]

jobs:
  start:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - name: AWS SSM Send Command
        uses: peterkimzz/aws-ssm-send-command@1.0.4
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY  }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY  }}
          aws-region: ${{ secrets.AWS_REGION }}
          instance-ids: ${{ secrets.INSTANCE_ID  }}
          comment: Deploy the master branch
          working-directory: /home/ubuntu/my-server
          command: /bin/sh deploy.sh

Whenever I push any updates to my repo, the build gets successful.

When I check the response of the command by invoking

aws ssm list-command-invocations \                                                                                                   00:08:49
     --command-id "THE_COMMAND_ID_FROM_GITHUB_ACTIONS_TAB"\
     --details

It always returns

"CommandPlugins": [
                {
                    "Name": "aws:runShellScript",
                    "Status": "Failed",
                    "StatusDetails": "Failed",
                    "ResponseCode": 1,
                    "ResponseStartDateTime": 1599936014.182,
                    "ResponseFinishDateTime": 1599936014.382,
                    "Output": "\n----------ERROR-------\ngit@github.com: Permission denied (publickey).\r\nfatal: Could not read from remote repository.\n\nPlease make sure you have the correct access rights\nand the repository exists.\nfailed to run commands: exit status 1",
                    "StandardOutputUrl": "",
                    "StandardErrorUrl": "",
                    "OutputS3Region": "us-east-2",
                    "OutputS3BucketName": "",
                    "OutputS3KeyPrefix": ""
                }
            ],

It says I have some permission errors, but I have correctly setup the SSH keys, and I can successfully run the /bin/sh deploy.sh command and everything runs as expected without any errors.

The deploy.sh file contains

#!/bin/sh
git pull origin master && sudo docker-compose up --build -d

Any suggestions on what I might be doing wrong?

Jigar3 commented 3 years ago

I was able to resolve this myself. For anybody else who gets stuck at this, I changed deploy.sh to

#!/bin/sh
git config core.sshCommand 'ssh -i /home/ubuntu/.ssh/id_rsa' && \
git pull origin master && \
sudo docker-compose up --build -d