Jenkins SSH shell closes before executing remote commands - jenkins

I have a Jenkins job with the following commands under "Execute shell":
ssh jenkins#172.31.12.58
pwd
I want the Jenkins server to connect via SSH to the remote server then run a command on the remote server.
Instead, Jenkins connects to the remote server, disconnects immediately, then runs the pwd command locally as can be seen in the output:
Started by user Johanan Lieberman
Building in workspace /var/lib/jenkins/jobs/Test Github build/workspace
[workspace] $ /bin/sh -xe /tmp/hudson266272646442487328.sh
+ ssh jenkins#172.31.12.58
Pseudo-terminal will not be allocated because stdin is not a terminal.
+ pwd
/var/lib/jenkins/jobs/Test Github build/workspace
Finished: SUCCESS
Edit: Any idea why the subsequent commands after the ssh command aren't run inside the SSH shell, but rather run locally instead?

If you're not running interactively, SSH does not create an interactive session (thus the "Pseudo-terminal" error message you see), so it's not quite the same as executing a sequence of commands in an interactive terminal.
To run a specific command through an SSH session, use:
ssh jenkins#YOUR_IP 'uname -a'
The remote command must be quoted properly as a single argument to the ssh command. Or use the bash here-doc syntax for a simple multi-line script:
ssh jenkins#YOUR_IP <<EOF
pwd
uname -a
EOF

I think you can use the Publish Over SSH plugin to execute commands on a slave with SSH:
If the Source files field is mandatory, maybe you can transfer a dummy file.
Update:
Another solution is to use the SSH plugin. Maybe it's a better solution compare to the other plugin :)

Related

docker exec -> permission denied when executing through ssh

I am trying to execute a command on a docker container that is running in a remote server. I am running most commands through ssh and they all work correctly. However, this command modifies the file /etc/environment and I get a "permission denied" error.
The command in question is docker exec container_id echo 'WDS_SOCKET_PORT=XXXXX' >> /etc/environment
If I run the command from the docker host, it works
If I run a simple command remotely using ssh user#ip docker exec container_id ls, it works
If I run this command remotely using ssh user#ip docker exec container_id echo 'WDS_SOCKET_PORT=XXXXX' >> /etc/environment I get sh: 1: cannot create /etc/environment: Permission denied
I tried adding the option -u 0 to the docker exec command with no luck.
I don't mind making changes to the Dockerfile since I can kill, remove or recreate this container with no problem.
The error isn't coming from docker or ssh, it's coming from your shell that parses the command you want to run. You are trying to modify the file on your host. To do io redirection inside the container, you need to run a shell there and parse the command with that shell.
ssh user#ip "docker exec container_id /bin/sh -c 'echo \"WDS_SOCKET_PORT=XXXXX\" >> /etc/environment'"
EDIT: Note that the whole docker command should be surrounded by quotes. I believe this is because ssh might otherwise parse different parts of the command as parameters of the docker command. This way, each sub-command is clearly delimited.

how to run docker build command in jenkins shell prompt

I want to run docker build command in Jenkins shell prompt.
Have already installed docker and add Jenkins user in docker usergroup.
But when i hit docker build command it shows me permission denied issue and when i am using sudo prefix it ask for password with -S argument.
I am running all commands on Jenkins master, earlier i used on other node server not master.
So what is best way to resolve this.

running 'docker-compose exec ...' within GitLab CI

I'm trying to run a command on container like this docker-compose exec xyz from .gitlab-ci.yml file.
The error, which I don't understand, reads the input device is not a TTY and then it exits out.
How can I troubleshoot this ?
TTY is effectively STDIN, you're executing a command (I'm guessing with the -it) flag that expects some input after the exec command from STDIN (Like typing a password, or executing bash commands in a running container). As it's a build pipeline it errors because you haven't provided anything. Otherwise can you please provide some more info about your input?

Run `bash` command on Jenkins in windows hang forever

I am running Jenkins job on a windows10 machine. And I have installed ubuntu shell on the windows. The Jenkins job works fine with windows batch commands. But it stuck there when I try to run bash command. For example, I created an Execute Windows batch command build step, if I put command bash -c ls, this command never finishes.
It works fine if I run the same command in the windows directly.
How can I configure Jenkins job to work with bash command?
Below is the build step configuration. There are two commands. The first one is used to install dependencies on my nodejs application. The second command is used to run a command in bash shell. The problem is that the build is stuck on the second command.
Have you made any Global configurations in jenkins. try giving the command with the home path of Ubuntu shell and re run the command once.

run docker commands in non-interactive shell

I'm running docker from Jenkins and the console output is not reflecting the actual state of operation because docker is using interactive shell.
How can I make docker output to stdout in non-interactive mode? (similar to mvn -B flag).
Thanks!
--progress plain should do the trick.

Resources