Home > Enterprise >  Unable to login in EC2 instance from github repo via gitaction
Unable to login in EC2 instance from github repo via gitaction

Time:09-09

I have an EC2 instance with a downloaded repository that runs some commands via githubaction. I get the following error and can't seem to understand what I need to do.

Here is my github action

name: Deploy to x-web

on:
  push:
    branches: [ master ]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout
      uses: actions/checkout@v2

    - name: Register Private Key
      uses: webfactory/[email protected]
      with:
          ssh-private-key: ${{ secrets.PRIVATE_KEY }}
          
    - name: Deploy to EC2  
      env:
        HOSTNAME : XX.XX.X.XX
        USER_NAME : ec2-user
      run: |
          ssh -o StrictHostKeyChecking=no ${USER_NAME}@${HOSTNAME} '
          cd ./x-web
          eval $(ssh-agent -s)
          ssh-add /home/ec2-user/.ssh/github
          git checkout -b foo
          git branch -D master
          git checkout -b master origin/master-f
          git fetch --all -f &&
          git pull origin master -f &&
          git branch -D foo
          npm i --force && npm run build &&
          pm2 restart Frontend
          '

The error that I get is the following ....

Agent pid 16351
Identity added: /home/ec2-user/.ssh/github (/home/ec2-user/.ssh/github)
fatal: a branch named 'foo' already exists
error: Cannot delete branch 'master' checked out at '/home/ec2-user/x-web'
fatal: a branch named 'master' already exists
fatal: could not read Username for 'https://github.com': No such device or address
npm WARN using --force Recommended protections disabled.
...

CodePudding user response:

I don't quite follow what you are try to achieve, but to help you with a few of the errors:

fatal: a branch named 'foo' already exists is caused by your use of the -b flag which will attempt to create a new branch, but it looks like your repo has one already. Use git checkout foo instead to switch to it.

error: Cannot delete branch 'master' checked out at '/home/ec2-user/x-web' since your checkout failed you didn't switch off of master and you cant delete the branch you are on.

fatal: could not read Username for 'https://github.com': No such device or address I'm not 100% sure of but I suspect that in your manipulation of ssh or setting of env HOSTNAME or USER_NAME has caused this I would also recommend updating your first step to actions/checkout@v3

CodePudding user response:

If git version on your ECS is recent enough (2.23 or more), use git switch instead of git checkout.

That way you can replace

git checkout -b foo
git branch -D master
git checkout -b master origin/master-f

with:

git switch -C foo
git switch -f -C master origin/master

Also check your git remote -v on that instance. If it starts with https://, your SSH key would not be used at all.

A git config --global [email protected]:.insteadOf https://github.com/ would help ensuring you are using SSH.
(Or, if you know the full URL, git remote set-url origin [email protected]:<user>/<repo>)

  • Related