创建流水线
填写您的流水线名称、流水线描述、git仓库地址(如果为私有仓库,可以在右侧输入账号及密码)
Force's Blog Yaml配置(仅供参考)
version: 1.0
vars:
backupName: gokins.zip
productDirName: force-blog
stages:
- stage:
displayName: build
name: deploy
steps:
- step: shell@sh
displayName: sh
name: build
commands:
- rm -rf ${{backupPath}}/${{backupName}}
- cp -r ${{productPath}} ${{backupPath}}/${{productDirName}}
- rm -rf ${{backupPath}}/${{productDirName}}/.git && rm -rf ${{backupPath}}/${{productDirName}}/.idea
- cd ${{backupPath}} && zip -r ${{backupName}} ${{productDirName}}
- chmod 777 ${{backupPath}}/${{backupName}}
- rm -rf ${{backupPath}}/${{productDirName}}
artifacts:
- scope: repo #archive
repository: d5uvqoxb
name: gokins
path: ./
- step: shell@ssh
displayName: ssh
name: ssh
wait: ["build"]
input:
host: ${{SITE_HOST}} #端口必填
user: ${{SITE_USER}}
pass: ${{SITE_PASS}}
workspace: /www/backup/site/workspace #为空就是 $HOME 用户目录
useArtifacts:
- scope: repo
repository: d5uvqoxb
name: gokins
alias: publicdir
isUrl: true
path: ./
commands:
- rm -rf public.zip
- wget -O public.zip $ARTIFACT_DOWNURL_publicdir
- echo $ARTIFACT_DOWNURL_publicdir
- rm -rf public
- unzip -o -O UTF-8 public.zip
- php /build.php $ARTIFACT_DOWNURL_publicdir
- rm -rf public.zip
本yaml分为两个部分一个是build构建(备份、清理),另一个是ssh(连接远程服务器执行部署)
其中ssh段中php /build.php $ARTIFACT_DOWNURL_publicdir
,调用php脚本处理一些定制化的程序
Force's Blog 自定义部署脚本
#!/usr/bin/php
<?php
date_default_timezone_set('Asia/shanghai');
class build {
private $url;
private $work_space = '/www/backup/site/workspace';
private $product = '/www/wwwroot';
public function __construct($argv) {
$this->url = $argv[1];
}
public function run () {
//获取构建ID
$build_id = explode('/', $this->url)[7];
$build_id = str_replace(["\n", "\r" , "\t", " "], '', $build_id);
if(empty($build_id)) {
echo 'build id explode error'; exit;
}
//备份静态资源
$this->command("rm -rf {$this->work_space}/uploads");
$this->command("cp -r {$this->product}/force-blog/usr/uploads {$this->work_space}/");
//部署更新
$this->command("rm -rf {$this->product}/force-blog/admin");
$this->command("rm -rf {$this->product}/force-blog/user");
$this->command("rm -rf {$this->product}/force-blog/usr");
$this->command("rm -rf {$this->product}/force-blog/var");
$this->command("mv {$this->work_space}/{$build_id}/repo/admin {$this->product}/force-blog/admin");
$this->command("mv {$this->work_space}/{$build_id}/repo/user {$this->product}/force-blog/user");
$this->command("mv {$this->work_space}/{$build_id}/repo/usr {$this->product}/force-blog/usr");
$this->command("mv {$this->work_space}/{$build_id}/repo/var {$this->product}/force-blog/var");
//恢复静态资源
$this->command("cp -r {$this->work_space}/uploads {$this->product}/force-blog/usr/uploads");
$this->command("chmod -R 777 {$this->product}/force-blog/usr/uploads");
//清理gokins部署
$this->command("rm -rf {$this->work_space}/{$build_id}");
}
private function command($command = '') {
if(empty($command)) {
return false;
}
echo "- {$command}\n";
exec($command);
}
}
$build = new build($argv);
$build->run();