Skip to content
Snippets Groups Projects
Commit a917f84d authored by Robotka István Adrián's avatar Robotka István Adrián
Browse files

implementation of the executor

parent 834e3109
Branches
No related tags found
No related merge requests found
...@@ -3,6 +3,36 @@ ...@@ -3,6 +3,36 @@
The original SSH executor can only be used for a single/static host described The original SSH executor can only be used for a single/static host described
in the runner config. This is not as flexible as our use-case's needs. in the runner config. This is not as flexible as our use-case's needs.
# Sources # Install
```sh
cd /opt
git clone git@git.sch.bme.hu:kszk/opensource/ssh-executor.git ssh-executor
cd ssh-executor
./install/executor.sh
```
Generate an SSH key for the root user. This will be used for the authentication of the ssh executor.
Setup a runner in the `/etc/gitlab-runner/config.toml` file like the following:
```yaml
[[runners]]
name = "kszk-deploy"
url = "https://git.sch.bme.hu/"
token = "**********"
executor = "custom"
builds_dir = "/tmp/kszk-deploy/builds" # remote deploy/repo dir (ssh)
cache_dir = "/tmp/kszk-deploy/cache" # remote cache dir (ssh)
[runners.custom_build_dir]
[runners.cache]
[runners.cache.s3]
[runners.cache.gcs]
[runners.custom]
prepare_exec = "/opt/ssh-executor/prepare.sh" # suctom executor script
run_exec = "/opt/ssh-executor/run.sh" # suctom executor script
```
Profit.
# Sources/credits
- <https://docs.gitlab.com/runner/executors/ssh.html> - <https://docs.gitlab.com/runner/executors/ssh.html>
- <https://docs.gitlab.com/runner/executors/custom_examples/lxd.html> - <https://docs.gitlab.com/runner/executors/custom_examples/lxd.html>
#!/usr/bin/env bash
#!/usr/bin/env bash
cp ssh_config /root/.ssh/config
chown -R root:root .
chmod 664 /root/.ssh/config
#!/usr/bin/env bash
# this will make your script exit if any command in a pipeline errors.
set -eo pipefail
# trap any error, and mark it as a system failure.
trap "exit $SYSTEM_FAILURE_EXIT_CODE" ERR
if [ -z "$CUSTOM_ENV_SSH_HOST" ]; then
echo "SSH_HOST variable is NOT present. Please provide it in the .gitlab-ci.yml file in the variables directive."
exit 1
fi
SSH_HOST=$CUSTOM_ENV_SSH_HOST
SSH_USER=${CUSTOM_ENV_SSH_USER:=kszk-gitlab-deployer}
SSH_PORT=${CUSTOM_ENV_SSH_PORT:=22}
# This will run the script generated by GitLab Runner
SSH_CMD="ssh $SSH_USER@$SSH_HOST -p $SSH_PORT -T"
echo "Prepare SSH command: $SSH_CMD"
echo "$SSH_CMD" > ssh_command
#!/usr/bin/env bash #!/usr/bin/env bash
JOB_ID="runner-$CUSTOM_ENV_CI_RUNNER_ID-project-$CUSTOM_ENV_CI_PROJECT_ID-concurrent-$CUSTOM_ENV_CI_CONCURRENT_PROJECT_ID-$CUSTOM_ENV_CI_JOB_ID" # This script will be executed MULTIPLE times
# see: https://docs.gitlab.com/runner/executors/custom.html#run
# Two arguments:
# $1: The path to the script that GitLab Runner creates for the Custom executor to run.
# $2: Name of the stage.
SCRIPT_PATH="$1"
STAGE="$2"
# Sub stages (in sequential order):
# - prepare_script
# - get_sources
# - restore_cache
# - download_artifacts
# - step_*
# - build_script
# - step_*
# - after_script
# - archive_cache
# - upload_artifacts_on_success OR upload_artifacts_on_failure
# this will make your script exit if any command in a pipeline errors.
set -eo pipefail set -eo pipefail
# trap any error, and mark it as a system failure. # trap any error, and mark it as a system failure.
trap "exit $SYSTEM_FAILURE_EXIT_CODE" ERR trap "echo FAIL_HERE; exit $SYSTEM_FAILURE_EXIT_CODE" ERR
# debug sub-stages:
#echo "Running run.sh with args: $*"
echo "Running in $JOB_ID" # see: prepare.sh
SSH_CMD=$(cat ssh_command)
# This will run the script generated by GitLab Runner by sending the # This will run the script generated by GitLab Runner on the remote host
# content of the script to the container via STDIN. # leave exit 0 at the end, it causes exit 1 with the ssh command
head -n -1 "${1}" | $SSH_CMD
cat < "${1}"
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
# Exit using the variable, to make the build as failure in GitLab CI. # Exit using the variable, to make the build as failure in GitLab CI.
exit $BUILD_FAILURE_EXIT_CODE exit $BUILD_FAILURE_EXIT_CODE
fi fi
exit
### DO NOT EDIT, it will be overwriten
### DO NOT EDIT, it will be overwriten
### DO NOT EDIT, it will be overwriten
# Edit only in repo: https://git.sch.bme.hu/kszk/opensource/ssh-executor
Host *
KexAlgorithms curve25519-sha256@libssh.org,diffie-hellman-group-exchange-sha256
MACs hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-512,hmac-sha2-256,umac-128@openssh.com
Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com,aes256-ctr,aes192-ctr,aes128-ctr
ServerAliveInterval 10
ControlMaster auto
ControlPersist yes
ControlPath ~/.ssh/socket-%r@%h:%p
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment