ssh-action icon indicating copy to clipboard operation
ssh-action copied to clipboard

Execute local script over ssh

Open davegravy opened this issue 2 years ago • 9 comments

I have a bash script (.sh file) on my Github runner that I want to be executed on a remote host via SSH. I see that I can pass discrete commands using ssh-action, but how can an entire file be executed?

This would be similar to ssh user@hostname < my-local-script.sh

davegravy avatar Oct 02 '22 05:10 davegravy

hi, i think you should be able to upload it on your github repository, so you can pull it on the server and run it

devvspaces avatar Oct 07 '22 11:10 devvspaces

  1. place .sh file on repo
  2. ssh-action into the server
  3. pull repo on the server
  4. run .sh script

instead of step 3 i think you can also download the file to the server using the absolute download link from github repo. That would be better, instead of pulling an entire git repo

devvspaces avatar Oct 07 '22 11:10 devvspaces

@devvspaces That's a pretty ugly work-around.

To re-iterate and expand on the OP's request, ultimately the script exists in the workspace as a result of a checkout. What OP (and I) are looking for is something like:

...
      - name: Execute local script
        uses: appleboy/ssh-action@master
        with:
          host: foo-240
          username: root
          key: ${{ secrets.POC }}
          script-file: script/that/lives/in/workspace.sh

Having to check out the whole repository on the remote node (which means sshing into it, setting it up to be able to do git operations and then checking out the whole repo again on that server just to get the one script I want to execute there is really quite a non-starter.

brianjmurrell avatar Nov 16 '22 18:11 brianjmurrell

It's also really ugly to try to write more than a few lines of sh script in that script: tag, which has to be written as if it were all on a single line with ; between commands, etc. That script code cannot be linted for example. If it cout be put into a file instead and the file executed with a < style redirection much of this problem goes away.

brianjmurrell avatar Nov 16 '22 18:11 brianjmurrell

For those really wanting to be able to keep scripts in a separate file, there is this (admittedly ugly) hack:

      - name: Read script contents
        id: script
        uses: andstor/file-reader-action@v1
        with:
          path: script/that/lives/in/workspace.sh
      - name: Execute local script
        uses: appleboy/ssh-action@master
        with:
          host: foo-240
          username: root
          key: ${{ secrets.POC }}
          script: ${{ steps.script.outputs.contents }}

brianjmurrell avatar Nov 16 '22 18:11 brianjmurrell

try appleboy/scp-action + appleboy/ssh-action

appleboy avatar Apr 13 '23 08:04 appleboy

try appleboy/scp-action + appleboy/ssh-action

But then I also have to have a job that cleans up those remotely copied scripts or leave litter on the remote machine. Frankly I find this solution even worse than the ugly hack I posted previously.

I would request that this ticket be reopened as it does not satisfy the OP's (or my) request to be able to execute the script remotely without all kinds of palaver to work around what should be straightforward and not require pre or post work of the actual step.

I'm not suggesting that it's a top priority or that you personally work on it, but AFAIC, it's still an incomplete feature request that could be worked on by anyone so it should remain open so that a potential contributor can see it and decide to pick it up.

brianjmurrell avatar Apr 13 '23 11:04 brianjmurrell

@brianjmurrell I got your point. I added the enhancement label now.

appleboy avatar Apr 13 '23 14:04 appleboy