Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiline SSH commands interpreted as single lines #75

Closed
adamkdean opened this issue Jul 29, 2020 · 19 comments
Closed

Multiline SSH commands interpreted as single lines #75

adamkdean opened this issue Jul 29, 2020 · 19 comments
Assignees
Labels
bug Something isn't working

Comments

@adamkdean
Copy link

adamkdean commented Jul 29, 2020

There may be a simple solution to this but so far I've been unable to find it.

The follow script has three commands: docker stop, docker rm, and docker run. Unfortunately, the backslash \ at the end of docker run is ignored, and so each of the params that follow are interpreted as their own commands.

script: |
  docker stop example
  docker rm example
  docker run \
    --detach \
    --restart always \
    --publish 80:80 \
    --publish 443:443 \
    --name example \
    --network example-network \
    --volume example-volume:/var/example/volume \
    example/image-name

While all of this could fit on one line, it'll make more complex workflows unreadable and harder to maintain.

@KerberosMorphy
Copy link

KerberosMorphy commented Jul 31, 2020

script is a variable, whatever you pass will be interpreted by the runner before being run.

Example:

script: |
  echo $(ls) # Will echo the Runner directory and probable raise an error somewhere
  echo \$(ls) # Will echo the remote directory

If you want multiline command you should escaping the \ so \\ (Tested and work):

script: |
  ls \\
    -lah

@adamkdean
Copy link
Author

Ah! Of everything I tried, I never thought to escape the backslash. I'll give that a go. Thanks @KerberosMorphy!

@adamkdean
Copy link
Author

adamkdean commented Aug 3, 2020

Interestingly, @KerberosMorphy, I've just tried the multiline ls command and it's not worked.

======CMD======
ls \\
  -al

======END======
err: ls: cannot access '\': No such file or directory
2020/08/03 10:30:54 Process exited with status 2

This is using appleboy/ssh-action@master, so maybe I need to test with one of the releases. Which version did you use? Thanks in advance

ETA: Same issue with appleboy/ssh-action@v0.1.2

@adamkdean
Copy link
Author

I've tried blank lines, \, \\, \n, and \\n without any luck. This is definitely something which will require some work with drone-ssh I'm afraid @appleboy.

@KerberosMorphy
Copy link

Sorry, my bad, I had adapted this action to work with Bitbucket/GitLab/GitHub. When talking to you I was testing on bitbucket and it's works there.

For GitHub I use directly:

jobs:
  ssh_job:
    runs-on: ubuntu-latest
    steps:
      - uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.HOST }}
          username: ${{ secrets.USERNAME }}
          port: ${{ secrets.PORT }}
          passphrase: ${{ secrets.PASSPHRASE }}
          key: ${{ secrets.KEY }}
          script: |
            ls \
              -lah

And it's working.

======CMD======
ls \
  -lah

======END======
out: total 36K
out: drwxr-xr-x  5 user user 4.0K Apr 14 18:58 .
out: drwxr-xr-x 31 root       root       4.0K Jul 28 15:18 ..
out: -rw-r--r--  1 user user  220 Aug 31  2015 .bash_logout
out: -rw-r--r--  1 user user 3.7K Aug 31  2015 .bashrc
out: drwx------  3 user user 4.0K Apr 14 19:09 .cache
out: -rw-r--r--  1 user user 655 Jun 24  2016 .profile
out: drwx------  2 user user 4.0K Apr 14 13:23 .ssh
out: drwxrwxr-x  5 user user 4.0K Apr 14 18:58 .vscode-server
out: -rw-rw-r--  1 user user 185 Apr 14 18:59 .wget-hsts
==============================================
✅ Successfully executed commands to all host.
==============================================

@appleboy
Copy link
Owner

appleboy commented Aug 3, 2020

It also works for me. See https://github.com/appleboy/ssh-action/runs/940852614?check_suite_focus=true

@adamkdean
Copy link
Author

How bizarre! Good news though, means I'm the problem, and that's something I can solve.

Thanks both, I'll get back to testing, see if I can't work out specifically what is causing this.

@KerberosMorphy
Copy link

@adamkdean If you find out tell us. Also I think you can close this issue if solve.

@adamkdean
Copy link
Author

Hi sorry folks, I found the issue but was waylaid mid-typing. Looks like the culprit is script_stop: true. All my jobs have that added so that they fail fast, but adding that seems to cause something in the script to break. I think the issue may stem from this particular line: https://github.com/appleboy/drone-ssh/blob/7344ac6529e663fe6555fb760cc766e0a81b9a2f/plugin.go#L201

Sorry I can't investigate further at the moment, I'll be available for more testing tomorrow (UK based here.)

Thanks again for the support (and of course the fantastic library!)

@appleboy
Copy link
Owner

appleboy commented Aug 4, 2020

@adamkdean Thanks for your report. You use the script_stop: true and multiple commands. right?

script_stop: true
script: |
  docker stop example
  docker rm example
  docker run \
    --detach \
    --restart always \
    --publish 80:80 \
    --publish 443:443 \
    --name example \
    --network example-network \
    --volume example-volume:/var/example/volume \
    example/image-name

@adamkdean
Copy link
Author

@appleboy That's right, with the above being a simple example showing where the multiple lines would be useful.

@appleboy
Copy link
Owner

appleboy commented Aug 8, 2020

I will bump the new version to fix the issue.

@appleboy
Copy link
Owner

appleboy commented Aug 8, 2020

@adamkdean Please try the https://github.com/appleboy/ssh-action/releases/tag/v0.1.3 version

@adamkdean
Copy link
Author

Works perfectly. Thanks dude. 👏

@fankins
Copy link

fankins commented Aug 26, 2020

Sorry, my bad, I had adapted this action to work with Bitbucket/GitLab/GitHub. When talking to you I was testing on bitbucket and it's works there.

For GitHub I use directly:

jobs:
  ssh_job:
    runs-on: ubuntu-latest
    steps:
      - uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.HOST }}
          username: ${{ secrets.USERNAME }}
          port: ${{ secrets.PORT }}
          passphrase: ${{ secrets.PASSPHRASE }}
          key: ${{ secrets.KEY }}
          script: |
            ls \
              -lah

And it's working.

======CMD======
ls \
  -lah

======END======
out: total 36K
out: drwxr-xr-x  5 user user 4.0K Apr 14 18:58 .
out: drwxr-xr-x 31 root       root       4.0K Jul 28 15:18 ..
out: -rw-r--r--  1 user user  220 Aug 31  2015 .bash_logout
out: -rw-r--r--  1 user user 3.7K Aug 31  2015 .bashrc
out: drwx------  3 user user 4.0K Apr 14 19:09 .cache
out: -rw-r--r--  1 user user 655 Jun 24  2016 .profile
out: drwx------  2 user user 4.0K Apr 14 13:23 .ssh
out: drwxrwxr-x  5 user user 4.0K Apr 14 18:58 .vscode-server
out: -rw-rw-r--  1 user user 185 Apr 14 18:59 .wget-hsts
==============================================
✅ Successfully executed commands to all host.
==============================================

can u provide adapted for gitlab-ci action example?

@appleboy
Copy link
Owner

@fankins Do you mean https://github.com/appleboy/gitlab-ci-action ?

@KerberosMorphy
Copy link

KerberosMorphy commented Aug 26, 2020

@fankins, you can migrate almost any GitHub action to GitLab CI or Bitbucket Pipeline if it's base on a Docker.

I got some trouble with this action with GitLab CI, I don't remember if it as finally work. I choose to create my own cause I needed to pass some additional flags to forward agent, but I wrote a little explanation of how to Convert GitHub Action.

@HunteRoi
Copy link

Sorry, my bad, I had adapted this action to work with Bitbucket/GitLab/GitHub. When talking to you I was testing on bitbucket and it's works there.

For GitHub I use directly:

jobs:
  ssh_job:
    runs-on: ubuntu-latest
    steps:
      - uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.HOST }}
          username: ${{ secrets.USERNAME }}
          port: ${{ secrets.PORT }}
          passphrase: ${{ secrets.PASSPHRASE }}
          key: ${{ secrets.KEY }}
          script: |
            ls \
              -lah

And it's working.

======CMD======
ls \
  -lah

======END======
out: total 36K
out: drwxr-xr-x  5 user user 4.0K Apr 14 18:58 .
out: drwxr-xr-x 31 root       root       4.0K Jul 28 15:18 ..
out: -rw-r--r--  1 user user  220 Aug 31  2015 .bash_logout
out: -rw-r--r--  1 user user 3.7K Aug 31  2015 .bashrc
out: drwx------  3 user user 4.0K Apr 14 19:09 .cache
out: -rw-r--r--  1 user user 655 Jun 24  2016 .profile
out: drwx------  2 user user 4.0K Apr 14 13:23 .ssh
out: drwxrwxr-x  5 user user 4.0K Apr 14 18:58 .vscode-server
out: -rw-rw-r--  1 user user 185 Apr 14 18:59 .wget-hsts
==============================================
✅ Successfully executed commands to all host.
==============================================

hey @KerberosMorphy I see you mentionned using this project in a Gitlab pipeline (if I have not misunderstood). Would you be so kind to tell me if it is any different than for GitHub? If possible, I'd be happy to see your pipeline's file 😄

@KerberosMorphy
Copy link

@HunteRoi, this is quite old, I don't have the code inside the specific pipeline, but I have my old version of this action. I made a gist with my alternative of appleboy ssh-commands-action.

If it can be useful to you or anyone:

https://gist.github.com/KerberosMorphy/40ad61bdf5de3943a1bca4567b186d44

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants