Deployment job on gitlab - yaml

I'm trying to make job for deployment on gitlab. I'm writing yaml file for it.
image: maven:3-jdk-8
testjob:
script:
- "apt-get update"
- "apt-get install sshpass -y"
- "echo installed"
- "sshpass -p 'pass' ssh user#host"
- "echo login successful"
- "touch testfile.txt"
- "echo finished"
But when I'm trying to login with sshpass and i'm getting en error
Pseudo-terminal will not be allocated because stdin is not a terminal
What the problem?

You can try by disabling Pseudo-tty allocation with option -T.

Related

Gitalb pipeline : SSH to windows and execute script

i'm trying to setup a Gitlab pipeline, and one of the steps include running a .bat script on a Windows Server.
Windows Server has a SSH Daemon installed and configured.
I've tried the following command from a Unix host
sshpass -p <pwd> ssh -o StrictHostKeyChecking=no <user>#<ip>
"C:\Temp\test.bat"
and everything is working fine.
Gitlab job will be executed from a custom image as this:
build_and_deploy_on_integrazione:
stage: build
tags:
- maven
image: <custom_image>
script:
- apt-get update -y
- apt-get install -y sshpass
- sshpass -p <pwd> ssh -o StrictHostKeyChecking=no <user>#<ip>
"C:\Temp\test.bat"
- echo "Done"
just to be sure i've started a container of the custom image from command line on the same machine that is hosting the Gitlab Runner instance and executed the step of the script, and it's also running fine.
But when i run the pipeline from Gitlab the bat file is not executed, the only output i see is
Warning: Permanently added '<ip>' (RSA) to the list of known hosts.
and nothing else.
i've checked on the SSH Daemon log and the connection is executed correctly, so the "SSH" part of the script seems to be working, but the script is not executed.

sshpass not executing in bash script

I have a dockerfile: (these are the relevent commands)
RUN apk app --update bash openssh sshpass
CMD ["bin/sh", "/home/build/build.sh"]
Which my dockerfile gets ran by this command
docker run --rm -it -v $(pwd):/home <image-name>
and all of the commands within my bash script, that are within the mounted volume execute. These commands range from npm installs to using tar to zip up a file and I want to SFTP that tar.gz file.
I am using sshpass to automate logging in which I know isn't secured, but I'm not worried about that with this application.
sshpass -p <password> sftp -P <port> username#host << EOF
<command>
<command>
EOF
But the sshpass command is never executed. I've tested my docker run command by appending /bin/sh to it and trying it and it also does not run. The SFTP command by itself does.
And when I say it's never executed, I don't receive an error or anything.
Two possible reason
You apk command is wrong, it should be RUN apk add --update bash openssh sshpass, but I assume it typo
Seems like the known host entry is missing, you should check logs `docker logs -f , Also need to add entry in for known-host, check the suggested build script below.
Here is a working example that you can try
Dockerfile
FROM alpine
RUN apk add --update bash openssh sshpass
COPY build.sh /home/build/build.sh
CMD ["bin/sh", "/home/build/build.sh"]
build script
#!/bin/bash
echo "adding host to known host"
mkdir -p ~/.ssh
touch ~/.ssh/known_hosts
ssh-keyscan sftp >> ~/.ssh/known_hosts
echo "run command on remote server"
sshpass -p pass sftp foo#sftp << EOF
ls
pwd
EO
Now build the image, docker build -t ssh-pass .
and finally, the docker-compose for testing the above
version: '3'
services:
sftp-client:
image: ssh-pass
depends_on:
- sftp
sftp:
image: atmoz/sftp
ports:
- "2222:22"
command: foo:pass:1001
so you will able to connect the sftp container using docker-compose up

How do you execute commands over ssh on cloudbuild?

I am looking for ways to execute commands on to a remote server using ssh, when I am on cloudbuild.
Below is my current cloudbuild.yaml
steps:
- name: 'gcr.io/cloud-builders/gcloud'
args:
- kms
- decrypt
- --ciphertext-file=build.pem.encrypted
- --plaintext-file=build.pem
- --location=asia-southeast1
- --keyring=keyring
- --key=build-key
- name: 'ubuntu'
args: ['chmod', '400', './build.pem']
- name: 'ubuntu'
args: ['bash', './deploy.bash']
And my deploy.bash looks like this
#! /bin/bash
apt update
apt install -y openssh-client
mkdir ~/.ssh
touch ~/.ssh/known_hosts
ssh-keyscan -H somedomain.com >> ~/.ssh/known_hosts
ssh -i build.pem -T -v somedomain.com 'bash -s deploy1.bash'
And my deploy1.bash looks like
#! /bin/bash
echo "Hello World!"
echo "It works"
I have been trying out different ways to make it work. But could not.
If anybody could recommend how to make it work, I am very appreciated.
Currently I am it stuck at this step -
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
I manage to resolve my issue.
The issue was actually from, sshguard, it's actually blocking the ssh session.

./deploy.sh not working on gitlab ci

My problem is the bash script I created got this error "/bin/sh: eval: line 88: ./deploy.sh: not found" on gitlab. Below is my sample script .gitlab-ci.yml.
I suspect that gitlab ci is not supporting bash script.
image: docker:latest
variables:
IMAGE_NAME: registry.gitlab.com/$PROJECT_OWNER/$PROJECT_NAME
DOCKER_DRIVER: overlay
services:
- docker:dind
stages:
- deploy
before_script:
- docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN registry.gitlab.com
- docker pull $IMAGE_NAME:$CI_BUILD_REF_NAME || true
production-deploy:
stage: deploy
only:
- master#$PROJECT_OWNER/$PROJECT_NAME
script:
- echo "$PRODUCTION_DOCKER_FILE" > Dockerfile
- docker build --cache-from $IMAGE_NAME:$CI_BUILD_REF_NAME -t $IMAGE_NAME:$CI_BUILD_REF_NAME .
- docker push $IMAGE_NAME:$CI_BUILD_REF_NAME
- echo "$PEM_FILE" > deploy.pem
- echo "$PRODUCTION_DEPLOY" > deploy.sh
- chmod 600 deploy.pem
- chmod 700 deploy.sh
- ./deploy.sh
environment:
name: production
url: https://www.example.com
And this also my deploy.sh.
#!/bin/bash
ssh -o StrictHostKeyChecking=no -i deploy.pem ec2-user#targetIPAddress << 'ENDSSH'
// command goes here
ENDSSH
All I want is to execute deploy.sh after docker push but unfortunately got this error about /bin/bash thingy.
I really need your help guys. I will be thankful if you can solve my problem about gitlab ci bash script got error "/bin/sh: eval: line 88: ./deploy.sh: not found".
This is probably related to the fact you are using Docker-in-Docker (docker:dind). Your deploy.sh is requesting /bin/bash as the script executor which is NOT present in that image.
You can test this locally on your computer with Docker:
docker run --rm -it docker:dind bash
It will report an error. So rewrite the first line of deploy.sh to
#!/bin/sh
After fixing that you will run into the problem that the previous answer is addressing: ssh is not installed either. You will need to fix that too!
docker:latest is based on alpine linux which is very minimalistic and does not have a lot installed by default. For example, ssh is not available out of the box, so if you want to use ssh commands you need to install it first. In your before_script, add:
- apk update && apk add openssh
Thanks. This worked for me by adding bash
before_script:
- apk update && apk add bash
Let me know if that still doesn't work for you.

bash script execute commands after ssh

I am trying to execute a few commands via my first script but it's not working.
#!/bin/bash
#connect to server
echo "Connecting to the server..."
ssh -t root#IP '
#switch user to deploy
su - deploy
#switch path
echo "Switching the path"
cd /var/www/deploys/bin/app/config
#run deploy script
echo "Running deploy script"
/usr/local/bin/cap -S env=prod deploy
#restart apache
sudo /bin/systemctl restart httpd.service
bash -l
'
What is happening? I am successfully connected to the server, the user is changed and then I don't see nothing happening. When I press ctrl + c just like that in terminal, some output from the command that should be executed appears but there are some errors.
Why I don't see everything what is happening in terminal after launching the script? Am I doing it the wrong way?
BTW: when I try connect manually and run the commands myself, everything is working nicely.
Using CentOS 7.
Clean way to login through ssh and excecute a set of commands is
ssh user#ip << EOF
#some commands
EOF
here EOF acts as the delimitter for the command list
the script can be modified as
ssh -t root#IP << EOF
#switch user to deploy
su - deploy
#switch path
echo "Switching the path"
cd /var/www/deploys/bin/app/config
#run deploy script
echo "Running deploy script"
/usr/local/bin/cap -S env=prod deploy
#restart apache
sudo /bin/systemctl restart httpd.service
bash -l
EOF
will excecutes the command and closes the connection there after

Resources