On your runner server
sudo gitlab-runner register \
--url "https://git.example.ru/" \
--registration-token "ExAmLealLWgeu" \
--description "karpulix/gitlabci_ssh_run" \
--executor "docker" \
--docker-image karpulix/gitlabci_ssh_runsudo gitlab-runner restartDisable checkbox Lock tp current project in /admin/runners (for sharing this runner)
mkdir -p ~/cicd-keys
ssh-keygen ~/cicd-keys/projectnamecat ~/cicd-keys/projectnameYou Gitlab Project → Settings → CI/CD → Variables (Use Type file) let it be $SSH_RUN_PRIVATE_KEY
cat ~/cicd-keys/projectname.pub# use ssh-keygen for quick creation .ssh folder (it makes keys also)
# ssh-keygen
# enter, enter, enter ...
echo "ssh-rsa AAAAB3NzaC....SIDsBxSSR" >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keysjob:deploy:
tags:
- gitlab_ssh_run
image: karpulix/gitlab_ssh_run
script:
# Initit current connection
- ssh_init -h ssh.targethost.com -u you_username -k $SSH_RUN_PRIVATE_KEY
# Run simple command
- ssh_run whoami
# Run command with params
- ssh_run git --version
# Run several commands with &&
- ssh_run cd /var/www/src && ls -la
# Run several command with YAML multiline block scalar indicator
- >
ssh_run
'
cd ~/.ssh &&
ls -la &&
touch ~/ILLBEBACK
'if use ~, you shoild use quotation marks for your command else ~ will be interpreatated in host machine's context
script:
- ssh_init -h ssh.targethost.com -u you_username -k $SSH_RUN_PRIVATE_KEY
- ssh_run 'touch ~/.ssh/ILLBEBACK'ssh_init [-h host] [-k key_path] [-u user] [-p port]
Description:
-u user | Optional, default: root
Set username for a ssh connection.
-p port | Optional, default: 22
Set port for a ssh connection.
-h host | Required
Set hostname or IP for a ssh connection.
-k path | Required
Set path to private key for a ssh connection.
ssh_run [command]
Description: Usage any command string. See examples on this page.