Git : Using actions/checkout@v2 instead of appleboy/ssh-action@master to clone repository to a server

Viewed 5909

I'm trying to clone a repository from GitHub to a remote server.

My solution using appleboy/ssh-action GitHub action was working but I was told the same can be achieved using actions/checkout@v2 GitHub action. I tried to just change - uses: value to actions/checkout@V2` but then the code doesn't work.

I can't find any templates on how to do it using actions/checkout@v2. Any advice would be much appreciated.

name: deploy to a server on push

on:
  push:
    branches: [ master ]

jobs:
  deploy-to-server:
    runs-on: ubuntu-latest
    steps:

  - uses: appleboy/ssh-action@master 
    with:
      host: 123.132.123.132
      username: tomas
      key: ${{ secrets.PRIVATE_KEY }}
      port: 59666
      script:
        git clone https://github.com/Tomas-R/website.git
1 Answers

As the documentation of actions/checkout@v2 says

This action checks-out your repository under $GITHUB_WORKSPACE, so your workflow can access it.

   steps:
      - name: Checkout the repo
        uses: actions/checkout@v2
        with: 
          # This will create a directory named `my-repo` and copy the repo contents to it
          # so that you can easily upload it to your remote server
          path: my-repo

To copy this checked-out repo to a remote server, you may use scp command as follows.

   # Runs a set of commands using the runners shell
     - name: Upload repo to remote server
       env:
           SSH_AUTH_SOCK: /tmp/ssh_agent.sock
       run: |
         ssh-agent -a $SSH_AUTH_SOCK > /dev/null
         ssh-add - <<< "${{ secrets.PRIVATE_KEY }}"
         scp -o StrictHostKeyChecking=no -r -P 59666 my-repo tomas@123.132.123.132:/target/directory

By using the above commands we,

  • Start ssh-agent and bind it to a known location.
  • Import the private key from the secret to the ssh-agent.
  • Copy contents from my-repo to the target directory on your remote server.

This way, the private key is never written to the disk / being exposed.

There is yet another easier way to run scp using the Copy via ssh GitHub action.

   - name: Copy folder content recursively to remote
     uses: garygrossgarten/github-action-scp@release
     with:
       local: my-repo
       remote: ~/target/directory
       host: 123.132.123.132
       port: 59666
       username: tomas
       privateKey: ${{secrets.PRIVATE_KEY}}
Related