drone的pipeline原理与代码分析

一个项目原因,需要实现一个工作任务流(task pipeline),基于之前CICD的经验,jenkins pipeline和drone的pipeline进入候选:

对比项 jenkins drone
pipeline定义   编写jenkinsfile   编写流程yml  
运行方式   在一个pod里运行   每一步骤起对应的container,通过挂载volume实现数据共享  
运行环境   物理机或者容器环境,包括K8S   docker容器环境  
开发语言   java   golang  

drone pipeline好处是相对更轻量级,yml定义也相对简洁清晰,按照功能来划分容器,可以方便的实现task的复用,而jenkins则是完全打包到一个镜像,会造成单个镜像体积过大,比如jenkins的单个镜像超过2G。

drone的pipeline,是基于https://github.com/cncd/pipeline 实现的,这里简单分析下其原理。

编译和执行 drone pipeline

要了解一个程序的原理,先从输入输出讲起。

先安装:

go get -u github.com/cncd/pipeline go install github.com/cncd/pipeline/pipec

然后测试

cd $GOPATH/github.com/cncd/pipeline/samples/sample_1 # ll total 28 drwxr-xr-x 2 root root 4096 Jan 22 11:44 ./ drwxr-xr-x 13 root root 4096 Jan 22 11:02 ../ -rw-r--r-- 1 root root 549 Jan 22 11:02 .env -rw-r--r-- 1 root root 6804 Jan 22 16:30 pipeline.json -rw-r--r-- 1 root root 229 Jan 22 11:02 pipeline.yml -rw-r--r-- 1 root root 138 Jan 22 11:02 README.md

pipeline.yml 定义文件

pipeline.json 编译后的配置文件

.env 环境变量

先来查看pipeline.yml 定义

workspace: base: /go path: src/github.com/drone/envsubst clone: git: image: plugins/git depth: 50 pipeline: build: image: golang:1.7 commands: - go get -t ./... - go build - go test -v

上面的yml定义了:

工作目录workspace

初始化工作,git clone仓库,仓库地址在.env里定义

然后是定义pipeline,

pipeline下面是step数组,这里只有一个build

使用golang:1.7镜像

构建命令在commands数组里定义

通过pipec compilecompile配置文件:

# pipec compile Successfully compiled pipeline.yml to pipeline.json

查看编译后的pipeline.json

{ "pipeline": [ { "name": "pipeline_clone_0", "alias": "git", "steps": [ { "name": "pipeline_clone_0", "alias": "git", "image": "plugins/git:latest", "working_dir": "/go/src/github.com/drone/envsubst", "environment": { "CI": "drone", "CI_BUILD_CREATED": "1486119586", "CI_BUILD_EVENT": "push", "CI_BUILD_NUMBER": "6", "CI_BUILD_STARTED": "1486119585", "CI_COMMIT_AUTHOR": "bradrydzewski", "CI_COMMIT_AUTHOR_NAME": "bradrydzewski", "CI_COMMIT_BRANCH": "master", "CI_COMMIT_MESSAGE": "added a few more test cases for escaping behavior", "CI_COMMIT_REF": "refs/heads/master", "CI_COMMIT_SHA": "d0876d3176965f9552a611cbd56e24a9264355e6", "CI_REMOTE_URL": "https://github.com/drone/envsubst.git", "CI_REPO": "drone/envsubst", "CI_REPO_LINK": "https://github.com/drone/envsubst", "CI_REPO_NAME": "drone/envsubst", "CI_REPO_REMOTE": "https://github.com/drone/envsubst.git", "CI_SYSTEM": "pipec", "CI_SYSTEM_ARCH": "linux/amd64", "CI_SYSTEM_LINK": "https://github.com/cncd/pipec", "CI_SYSTEM_NAME": "pipec", "CI_WORKSPACE": "/go/src/github.com/drone/envsubst", "DRONE": "true", "DRONE_ARCH": "linux/amd64", "DRONE_BRANCH": "master", "DRONE_BUILD_CREATED": "1486119586", "DRONE_BUILD_EVENT": "push", "DRONE_BUILD_LINK": "https://github.com/cncd/pipec/drone/envsubst/6", "DRONE_BUILD_NUMBER": "6", "DRONE_BUILD_STARTED": "1486119585", "DRONE_COMMIT": "d0876d3176965f9552a611cbd56e24a9264355e6", "DRONE_COMMIT_AUTHOR": "bradrydzewski", "DRONE_COMMIT_BRANCH": "master", "DRONE_COMMIT_MESSAGE": "added a few more test cases for escaping behavior", "DRONE_COMMIT_REF": "refs/heads/master", "DRONE_COMMIT_SHA": "d0876d3176965f9552a611cbd56e24a9264355e6", "DRONE_JOB_STARTED": "1486119585", "DRONE_REMOTE_URL": "https://github.com/drone/envsubst.git", "DRONE_REPO": "drone/envsubst", "DRONE_REPO_LINK": "https://github.com/drone/envsubst", "DRONE_REPO_NAME": "envsubst", "DRONE_REPO_OWNER": "drone", "DRONE_REPO_SCM": "git", "DRONE_WORKSPACE": "/go/src/github.com/drone/envsubst", "PLUGIN_DEPTH": "50" }, "volumes": [ "pipeline_default:/go" ], "networks": [ { "name": "pipeline_default", "aliases": [ "git" ] } ], "on_success": true, "auth_config": {} } ] }, { "name": "pipeline_stage_0", "alias": "build", "steps": [ { "name": "pipeline_step_0", "alias": "build", "image": "golang:1.7", "working_dir": "/go/src/github.com/drone/envsubst", "environment": { "CI": "drone", "CI_BUILD_CREATED": "1486119586", "CI_BUILD_EVENT": "push", "CI_BUILD_NUMBER": "6", "CI_BUILD_STARTED": "1486119585", "CI_COMMIT_AUTHOR": "bradrydzewski", "CI_COMMIT_AUTHOR_NAME": "bradrydzewski", "CI_COMMIT_BRANCH": "master", "CI_COMMIT_MESSAGE": "added a few more test cases for escaping behavior", "CI_COMMIT_REF": "refs/heads/master", "CI_COMMIT_SHA": "d0876d3176965f9552a611cbd56e24a9264355e6", "CI_REMOTE_URL": "https://github.com/drone/envsubst.git", "CI_REPO": "drone/envsubst", "CI_REPO_LINK": "https://github.com/drone/envsubst", "CI_REPO_NAME": "drone/envsubst", "CI_REPO_REMOTE": "https://github.com/drone/envsubst.git", "CI_SCRIPT": "CmlmIFsgLW4gIiRDSV9ORVRSQ19NQUNISU5FIiBdOyB0aGVuCmNhdCA8PEVPRiA+ICRIT01FLy5uZXRyYwptYWNoaW5lICRDSV9ORVRSQ19NQUNISU5FCmxvZ2luICRDSV9ORVRSQ19VU0VSTkFNRQpwYXNzd29yZCAkQ0lfTkVUUkNfUEFTU1dPUkQKRU9GCmNobW9kIDA2MDAgJEhPTUUvLm5ldHJjCmZpCnVuc2V0IENJX05FVFJDX1VTRVJOQU1FCnVuc2V0IENJX05FVFJDX1BBU1NXT1JECnVuc2V0IENJX1NDUklQVAp1bnNldCBEUk9ORV9ORVRSQ19VU0VSTkFNRQp1bnNldCBEUk9ORV9ORVRSQ19QQVNTV09SRAoKZWNobyArICJnbyBnZXQgLXQgLi8uLi4iCmdvIGdldCAtdCAuLy4uLgoKZWNobyArICJnbyBidWlsZCIKZ28gYnVpbGQKCmVjaG8gKyAiZ28gdGVzdCAtdiIKZ28gdGVzdCAtdgoK", "CI_SYSTEM": "pipec", "CI_SYSTEM_ARCH": "linux/amd64", "CI_SYSTEM_LINK": "https://github.com/cncd/pipec", "CI_SYSTEM_NAME": "pipec", "CI_WORKSPACE": "/go/src/github.com/drone/envsubst", "DRONE": "true", "DRONE_ARCH": "linux/amd64", "DRONE_BRANCH": "master", "DRONE_BUILD_CREATED": "1486119586", "DRONE_BUILD_EVENT": "push", "DRONE_BUILD_LINK": "https://github.com/cncd/pipec/drone/envsubst/6", "DRONE_BUILD_NUMBER": "6", "DRONE_BUILD_STARTED": "1486119585", "DRONE_COMMIT": "d0876d3176965f9552a611cbd56e24a9264355e6", "DRONE_COMMIT_AUTHOR": "bradrydzewski", "DRONE_COMMIT_BRANCH": "master", "DRONE_COMMIT_MESSAGE": "added a few more test cases for escaping behavior", "DRONE_COMMIT_REF": "refs/heads/master", "DRONE_COMMIT_SHA": "d0876d3176965f9552a611cbd56e24a9264355e6", "DRONE_JOB_STARTED": "1486119585", "DRONE_REMOTE_URL": "https://github.com/drone/envsubst.git", "DRONE_REPO": "drone/envsubst", "DRONE_REPO_LINK": "https://github.com/drone/envsubst", "DRONE_REPO_NAME": "envsubst", "DRONE_REPO_OWNER": "drone", "DRONE_REPO_SCM": "git", "DRONE_WORKSPACE": "/go/src/github.com/drone/envsubst", "HOME": "/root", "SHELL": "/bin/sh" }, "entrypoint": [ "/bin/sh", "-c" ], "command": [ "echo $CI_SCRIPT | base64 -d | /bin/sh -e" ], "volumes": [ "pipeline_default:/go" ], "networks": [ { "name": "pipeline_default", "aliases": [ "build" ] } ], "on_success": true, "auth_config": {} } ] } ], "networks": [ { "name": "pipeline_default", "driver": "bridge" } ], "volumes": [ { "name": "pipeline_default", "driver": "local" } ], "secrets": null }

简单分析结构:

pipeline 定义了执行的stage,每个stage有一个或者多个step

networks、volumes、secrets 分别定义网络、存储和secrets

通过network,实现container互通

通过volumes实现数据共享

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpfyjz.html