Last active
October 11, 2021 03:25
-
-
Save iflamed/2d5aced9148714dcebe0c4918bcc917f to your computer and use it in GitHub Desktop.
Drone CI: git clone with ssh in the clone step
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
kind: pipeline | |
type: docker | |
name: default | |
clone: | |
disable: true | |
steps: | |
- name: clone | |
image: alpine/git | |
pull: if-not-exists | |
environment: | |
SSH_KEY: | |
from_secret: SSH_KEY | |
SSH_HOST: | |
from_secret: SSH_HOST | |
SSH_PORT: | |
from_secret: SSH_PORT | |
commands: | |
- mkdir $HOME/.ssh && echo "$SSH_KEY" > $HOME/.ssh/id_rsa && chmod 600 $HOME/.ssh/id_rsa | |
- | | |
[[ -n "$${SSH_PORT}" ]] && [[ -n "$${SSH_HOST}" ]] && ssh-keyscan -p $${SSH_PORT} $${SSH_HOST} >> $HOME/.ssh/known_hosts | |
- | | |
[[ -z "$${SSH_PORT}" ]] && [[ -n "$${SSH_HOST}" ]] && ssh-keyscan $${SSH_HOST} >> $HOME/.ssh/known_hosts | |
- git clone ${DRONE_GIT_SSH_URL} . | |
- git checkout ${DRONE_COMMIT_BRANCH} | |
- name: greeting | |
image: alpine | |
pull: if-not-exists | |
commands: | |
- echo "hello, world" | |
- ls -ahl | |
- pwd |
需要在Drone 中对应的 Repository 中配置三个 Secret 变量,如下图:
其中SSH_KEY
为 Git Repository 中用来 Clone 的 ssh private key,SSH_HOST
为 Git Server 的 IP 地址或者域名,SSH_PORT
为 Git Server 的SSH
端口,默认为22
可以不用填写。关于这个我有个更好的解决方法,将通用的环境变量写在drone-runner的docker-compose.yml中。这样就不用每个Repository配置一遍了。
docker-compose.yml 是要加入git repo 的吧,不安全。
如果要放到git上的确实会不安全,我这里没放,都是直接在本地编辑的。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
关于这个我有个更好的解决方法,将通用的环境变量写在drone-runner的docker-compose.yml中。这样就不用每个Repository配置一遍了。
- mkdir $HOME/.ssh && echo -e "$SSH_KEY" > $HOME/.ssh/id_rsa && chmod 600 $HOME/.ssh/id_rsa
. echo的时候加上 -e。解决