Skip to content

Instantly share code, notes, and snippets.

@AndreasDickow
Created February 28, 2023 15:00
Show Gist options
  • Save AndreasDickow/efbbed745cb5e441ede92024a60fd70f to your computer and use it in GitHub Desktop.
Save AndreasDickow/efbbed745cb5e441ede92024a60fd70f to your computer and use it in GitHub Desktop.
deployment from gitlab-ci with password protected ssh private key file
deploy to staging:
image: ${CI_DEPENDENCY_PROXY_DIRECT_GROUP_IMAGE_PREFIX}/alpine:3.17.2
stage: deploy
before_script:
- "apk add openssh-client"
- eval $(ssh-agent -s)
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
- echo '#!/bin/sh' > ~/.ssh/tmp
- echo 'echo $SSH_PRIV_KEY_PASS' >> ~/.ssh/tmp && chmod 700 ~/.ssh/tmp
- echo "$SSH_PRIVATE_KEY" | base64 -d | tr -d '\r' | DISPLAY=None SSH_ASKPASS=~/.ssh/tmp ssh-add -
- echo -e "Host *\n\tStrictHostKeyChecking no\n\n" >> ~/.ssh/config
script:
- ssh <myuser>@<myserver> "docker service update --image $CI_REGISTRY_IMAGE:$CI_COMMIT_TAG <myproject>"
when: manual
@AndreasDickow
Copy link
Author

the $SSH_PRIVATE_KEY CI variable has to be base64 encoded

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment