专栏首页Tencent Serverless 官方专栏SCF场景下的DevOps实现--Jenkins篇

SCF场景下的DevOps实现--Jenkins篇

当前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

  1. 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...'
}
}
}
  1. Checkout:检出,从github拉取代码。
  2. Build: 用pip安装项目依赖。
  3. Test:测试,SCF有自己的runtime,SCF命令行可模拟线上环境。
  4. Deploy - Staging: 灰度发布,当前云上灰度能力未完全开放,暂且跳过。
  5. Sanity check:发布审批。
  6. Deploy - Production:发布上线,这里利用了scf cli的发布能力。

2. index.py。就是scf cli init出来的helloworld模版,为了演示build阶段,特意import flask。

# -*- 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发布到腾讯云现网环境。

>>>>

完整输出

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,上车。

本文分享自微信公众号 - ServerlessCloudNative(ServerlessGo),作者:李帅

原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。

原始发表时间:2019-07-03

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • Serverless 动态博客开发趟“坑”记 | 社区精选文章

    本文为 Serverless 社区成员撰稿。作者云洋,从事信息管理工作,多年电子政务信息系统建设管理经验,对 Serverless 技术和架构有浓厚兴趣。供稿...

    腾讯云serverless团队
  • DevOps 的分与合

    抽象的 DevOps “ DevOps 是使软件开发和 IT 团队之间的流程自动化的一组实践,以便他们可以更快,更可靠地构建,测试和发布软件。DevOps 的...

    腾讯云serverless团队
  • Serverless 实战:3 分钟实现文本敏感词过滤

    敏感词过滤是随着互联网社区一起发展起来的一种阻止网络犯罪和网络暴力的技术手段,通过对可能存在犯罪或网络暴力的关键词进行有针对性的筛查和屏蔽,能够防患于未然,将...

    腾讯云serverless团队
  • Jenkins高级教程 - Pipeline 安装

    Pipeline,简而言之,就是一套运行于Jenkins上的工作流框架,将原本独立运行于单个或者多个节点的任务连接起来,实现单个任务难以完成的复杂流程编排与可视...

    晓晨
  • Jenkins之声明式pipeline基础

    Jenkins 2.0的到来,pipline进入了视野,jenkins2.0的核心特性。也是最适合持续交付的feature。

    苦叶子
  • Jenkins Pipeline插件十大最佳实践!

    Jenkins Pipeline 插件对于 Jenkins 用户来说可以让用户能够改变游戏规则。基于 Groovy 中的领域特定语言(DSL),Pipeline...

    程序猿DD
  • Spark硬件配置推荐

    1、存储系统   如果可以的话,把Spark的hadoop的节点安装在一起,最容易的方式是用standalone的模式安装,用mapred.child.java...

    岑玉海
  • 从0开始的Python学习005运算符与表达式

    Happy、Liu
  • WordPress 中禁止文章自动保存和修订版本的方法

    为了防止发生在WordPress的默认编辑器中写文章遇到诸如断网,不小心关闭浏览器等等而导致文章消失的事情,WordPress很人性化地引入了文章自动保存和修订...

    Jeff
  • 极光推送案例

    参考:http://docs.jpush.io/server/php_sdk/ Thinkphp推送类(需要下载vendor) push() ...

    苦咖啡

扫码关注云+社区

领取腾讯云代金券