我正在尝试使用gitlab yml文件在服务器上设置自动git克隆/拉取。
如何使用yml脚本检查git存储库是否存在,如果存在,则git将其拉出,否则将进行git克隆。我想要以下输出:
If GIT REPO NOT EXIST then
git clone
else
no action下面是我的yml文件,有人能帮我吗?
cache:
paths:
- vendor/
before_script:
- php -v
- pwd
- mkdir -p /var/www/html
- if [ ! -d /var/www/html/.git ] then
- git clone http://username:password@XX.XX.XX.XXX/root/myproject.git /var/www/html
- fi
stages:
- deploy
deploy_staging:
stage: deploy
script:
- echo "Deploy to staging server"
environment:
name: staging
url: http://XX.XX.XX.XXX/
script:
- cd $webroot
- git pull发布于 2017-01-13 21:26:30
我假设你得到了一个“文件意外结束”的错误。首先,你错过了if的分号,它应该变成if [ ! -d ... ]; then。
如果我没记错的话,每个命令都是在自己的shell中执行的。所以GitLab会执行:
sh -c 'php -v'
sh -c 'pwd'
sh -c 'mkdir -p /var/www/html'
sh -c 'if [ ! -d /var/www/html/.git ] then'
sh -c 'git clone http://username:password@XX.XX.XX.XXX/root/myproject.git /var/www/html'
sh -c 'fi'另一个问题是if、git clone和fi没有连接。因此,您可以使用多行字符串,也可以使用一些速记:
before_script:
- php -v
- pwd
- mkdir -p /var/www/html
- [ -d /var/www/html/.git ] || git clone http://username:password@XX.XX.XX.XXX/root/myproject.git /var/www/html这就是说:如果目录检查失败([ -d在找不到目录时失败),则执行git clone。||操作符是shell在命令失败时执行的操作符,&&操作符是在命令成功时执行的操作符。
https://stackoverflow.com/questions/41632107
复制相似问题