当前Serverless热度越来越高,越来越多的开发者们开始抛弃传统开发模式基于Serverless来搭建自己的产品服务。在享受腾讯云Serverless产品SCF的免运维、低成本优势的同时,也要适应SCF与传统开发模式的差异。其中DevOps便是很重要的一环,因为SCF特有的runtime,使得开发者无法复用现有DevOps平台完成CI/CD流程。
为此,腾讯云中间件团队基于Jenkins、CODING企业版以及蓝盾三种DevOps方案进行了整合打通,本文意图描述在SCF场景下,如何基于Jenkins搭建自己的CI/CD流程。后续,将会介绍基于其他两种方案的DevOps实现。
前言
本文核心目的在于描述SCF与现有DevOps平台结合的实践,以Jenkins为例。因此不会介绍过多背景知识。因此假设你已经具备以下技能。
- Jenkins或类似平台使用背景。
- 了解SCF产品
- scf cli,SCF的命令行工具:https://github.com/tencentyun/scfcli
源码介绍
git地址:
https://github.com/NevenMoore/scf_devops_demo
- Jenkinsfile, jenkins的pipeline描述文件。
pipeline {
agent any
stages {
stage('Checkout') {
steps {
echo "Checkout"
git 'https://github.com/NevenMoore/scf_devops_demo.git'
}
}
stage('Build') {
steps {
echo 'Building'
sh "pip install -r requirements.txt"
}
}
stage('Test') {
steps {
echo 'Testing'
script {
ret = sh(script: "scf native invoke -t ./template.yaml --no-event", returnStatus: true)
if (ret != 0) {
echo '[Test] Failed'
currentBuild.result = 'FAILURE'
return
}
}
}
}
stage('Deploy - Staging') {
steps {
echo 'Deploy - Staging'
}
}
stage('Sanity check') {
steps {
input "Does the staging environment look ok?"
}
}
stage('Deploy - Production') {
steps {
echo 'Deploy - Production'
script {
ret = sh(script: "scf package -t ./template.yaml", returnStatus: true)
if (ret != 0) {
echo '[Deploy] Failed'
currentBuild.result = 'FAILURE'
return
}
ret = sh(script: "scf deploy -t ./deploy.yaml -f", returnStatus: true)
if (ret != 0) {
echo '[Deploy] Failed'
currentBuild.result = 'FAILURE'
return
}
}
}
}
}
post {
success {
echo 'I succeeeded!'
}
unstable {
echo 'I am unstable :/'
}
failure {
echo 'I failed :('
}
changed {
echo 'Things were different before...'
}
}
}
2. index.py。就是scf cli init出来的helloworld模版,为了演示build阶段,特意import flask。
代码语言:javascript复制# -*- coding: utf8 -*-
import flask
def main_handler(event, context):
print(str(event))
return "hello world"
3. template.yaml, scf的元信息文件,例如runtime,memsize等信息,可参见scf cli文档。
Jenkins pipeline配置
比较简单,将上面的jenkinsfile内容粘贴下配置即可完成。
触发&构建
>>>>
触发
简单起见在jenkins上手动出发流程(当然你可以选择webhooks)自动触发。
>>>>
阶段视图
可以看见前面的checkout->build->test>Deploy - Staging阶段已自动化完成,因为上面配置了人工确认,手动确认后pipeline会将scf发布到腾讯云现网环境。
>>>>
完整输出
代码语言:javascript复制Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/scf_devops_demo
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Checkout)
[Pipeline] echo
Checkout
[Pipeline] git
No credentials specified
> git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
> git config remote.origin.url https://github.com/NevenMoore/scf_devops_demo.git # timeout=10
Fetching upstream changes fromhttps://github.com/NevenMoore/scf_devops_demo.git
> git --version # timeout=10
> git fetch --tags --progress https://github.com/NevenMoore/scf_devops_demo.git refs/heads/*:refs/remotes/origin/*
> git rev-parse refs/remotes/origin/master^{commit} # timeout=10
> git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision a500383602e314911b62a74045295f0008b0288f (refs/remotes/origin/master)
> git config core.sparsecheckout # timeout=10
> git checkout -f a500383602e314911b62a74045295f0008b0288f
> git branch -a -v --no-abbrev # timeout=10
> git branch -D master # timeout=10
> git checkout -b master a500383602e314911b62a74045295f0008b0288f
Commit message: "flask"
First time build. Skipping changelog.
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] echo
Building
[Pipeline] sh
pip install -r requirements.txt
Requirement already satisfied: flask in /usr/local/lib/python3.6/site-packages (from -r requirements.txt (line 1)) (1.0.2)
Requirement already satisfied: click>=5.1 in /usr/local/lib/python3.6/site-packages (from flask->-r requirements.txt (line 1)) (6.7)
Requirement already satisfied: itsdangerous>=0.24 in/usr/local/lib/python3.6/site-packages (from flask->-r requirements.txt (line 1)) (1.1.0)
Requirement already satisfied: Werkzeug>=0.14 in/usr/local/lib/python3.6/site-packages (from flask->-r requirements.txt (line 1)) (0.14.1)
Requirement already satisfied: Jinja2>=2.10 in /usr/local/lib/python3.6/site-packages (from flask->-r requirements.txt (line 1)) (2.10)
Requirement already satisfied: MarkupSafe>=0.23 in/usr/local/lib64/python3.6/site-packages (from Jinja2>=2.10->flask->-r requirements.txt (line 1)) (1.1.1)
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] echo
Testing
[Pipeline] script
[Pipeline] {
[Pipeline] sh
scf native invoke -t ./template.yaml --no-event
START RequestId: 59d1d0b0-c206-4a6d-a025-ebd364952bc9
{}
END RequestId: 59d1d0b0-c206-4a6d-a025-ebd364952bc9
REPORT RequestId: 59d1d0b0-c206-4a6d-a025-ebd364952bc9 Duration: 0 ms BilledDuration: 100 ms Memory Size: 128 MB Max Memory Used: 32 MB
"hello world"
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Deploy - Staging)
[Pipeline] echo
Deploy - Staging
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Sanity check)
[Pipeline] input
Does the staging environment look ok?
Proceed or Abort
Approved by 帅哥
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Deploy - Production)
[Pipeline] echo
Deploy - Production
[Pipeline] script
[Pipeline] {
[Pipeline] sh
scf package -t ./template.yaml
Generate deploy file 'deploy.yaml' success
[Pipeline] sh
scf deploy -t ./deploy.yaml -f
deploy default begin
default scf_devops_demo already exists, update it now
Deploy function 'scf_devops_demo' success
deploy default end
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] echo
Things were different before...
[Pipeline] echo
I succeeeded!
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
>>>>
控制台检验
scf_devops_demo函数已经正确上传。
总结
从阶段视图可以看出SCF环境下的DevOps并没有什么不同。唯一的区别在于SCF有自己的runtime,需要官方提供的scf cli来模拟线上运行环境。deploy阶段直接用scf cli相比写代码云API也简单了许多。
对于一些有特殊需求的SCF用户(私网CI/CD),本文有一定借鉴作用。
最后,pip install scf。下载一个scf cli,上车。