写点什么

如何尝试用 GitHub Actions 项目编写容器应用?

  • 2019-01-23
  • 本文字数:2465 字

    阅读完需:约 8 分钟

如何尝试用GitHub Actions项目编写容器应用?

早前,GitHub 发布 GitHub Actions 项目,开发者可通过 GitHub Actions 存储和搜索代码,部分代码可直接运行。本文尝试使用 AWS Lambda 和 API Gateway 作为基本 API,编写应用程序原型并用名为 gourmet 的容器运行函数,虽然这可能不会让代码易于管理,但至少不需要写 API 或者 Web 应用程序。


正如 Lambda 函数在 AWS 中运行一样,Github Actions 是一种强大的管理方式,可直接扩展应用。使用 AWS Lambda,可将代码挂接到几乎任何事件上,比如 EC2 创建、终止、DNS 记录更改等,不需要运行服务器,只需加载代码就能正常工作。


本文作者针对此做了一些尝试,但需要 CI 服务器。为了拥有可测试的 kubernetes 集群,作者自建私有存储库,目前因内部有些混乱暂不准备开源。


无论如何,以下是项目文件夹:


├── .github│   ├── actions│   │   ├── deploy│   │   │   ├── deploy│   │   │   └── Dockerfile│   │   └── dryrun│   │       ├── Dockerfile│   │       └── dryrun│   └── main.workflow└── kubernetes    ├── digitalocean.yaml    ├── external-dns.yaml    ├── micro.yaml    ├── namespaces.yaml    ├── nginx.yaml    └── openvpn.yaml
复制代码


kubernetes 目录包含集群安装的所有东西。对于此存储库的每次新推送,需要检查是否可用命令 kubectl apply -f./kubernetes --dryrun 将其应用于 kubernetes 集群,并且当合并 PR 时,应用更改。


因此,作者在.github/main.workflow 中创办了工作流:


## Workflow defines what we want to call a set of actions.## For every new push check if the changes can be applied to kubernetes ## using the action called: kubectl dryrunworkflow "after a push check if they apply to kubernetes" {  on = "push"  resolves = ["kubectl dryrun"]}## When a PR is merged trigger the action: kubectl deploy. To apply the new code to master.workflow "on merge to master deploy on kubernetes" {  on = "pull_request"  resolves = ["kubectl deploy"]}## This is the action that checks if the push can be applied to kubernetesaction "kubectl dryrun" {  uses = "./.github/actions/dryrun"  secrets = ["KUBECONFIG"]}## This is the action that applies the change to kubernetesaction "kubectl deploy" {  uses = "./.github/actions/deploy"  secrets = ["KUBECONFIG"]}
复制代码


secrets 是一组环境变量,可从外部设置值。如果帐户启用 GitHub Action,则每个存储库的 Setting 都会有一个名为 secrets 的新标签。


本例,作者将 KUBECONFIG 设置为 kubeconfig 文件的 base64,允许 GitHub Action 授权给 Kubernetes 集群。


两个操作类似,第一个操作位于 .github/actions/dryrun 目录:


├── .github    ├── actions        └── dryrun            ├── Dockerfile            └── dryrun

复制代码


包含一个 Dockerfile


FROM alpine:latest
## The action name displayed by GitHubLABEL "com.github.actions.name"="kubectl dryrun"## The description for the actionLABEL "com.github.actions.description"="Check the kubernetes change to apply."## https://developer.github.com/actions/creating-github-actions/creating-a-docker-container/#supported-feather-iconsLABEL "com.github.actions.icon"="check"## The color of the action iconLABEL "com.github.actions.color"="blue"
RUN apk add --no-cache \ bash \ ca-certificates \ curl \ git \ jq
RUN curl -L -o /usr/bin/kubectl https://storage.googleapis.com/kubernetes-release/release/v1.13.0/bin/linux/amd64/kubectl && \ chmod +x /usr/bin/kubectl && \ kubectl version --client
COPY dryrun /usr/bin/dryrunCMD ["dryrun"]

复制代码


如上所示,只需要一个 Dockerfile,工作原理和 docker 类似。Cmd dryrun 在这里是复制的 bash 脚本:


#!/bin/bash
main(){ echo ">>>> Action started" # Decode the secret passed by the action and paste the config in a file. echo $KUBECONFIG | base64 -d > ./kubeconfig.yaml echo ">>>> kubeconfig created" # Check if the kubernetes directory has change diff=$(git diff --exit-code HEAD~1 HEAD ./kubernetes) if [ $? -eq 1 ]; then echo ">>>> Detected a change inside the kubernetes directory" # Apply the changes with --dryrun just to validate them kubectl apply --kubeconfig ./kubeconfig.yaml --dry-run -f ./kubernetes else echo ">>>> No changed detected inside the ./kubernetes folder. Nothing to do." fi}
main "$@"
复制代码


第二个操作和此几乎一样,Dockerfile 是相同的,但 CMD 看起来是这样的:


#!/bin/bash
main(){ # Decode the secret passed by the action and paste the config in a file. echo $KUBECONFIG | base64 -d > ./kubeconfig.yaml # Check if it is an event generated by the PR is a merge merged=$(jq --raw-output .pull_request.merged "$GITHUB_EVENT_PATH") # Retrieve the base branch for the PR because I would like to apply only PR merged to master baseRef=$(jq --raw-output .pull_request.base.ref "$GITHUB_EVENT_PATH")
if [[ "$merged" == "true" ]] && [[ "$baseRef" == "master" ]]; then echo ">>>> PR merged into master. Shipping to k8s!" kubectl apply --kubeconfig ./kubeconfig.yaml -f ./kubernetes else echo ">>>> Nothing to do here!" fi}
main "$@"
复制代码


除此之外,工作流文件还有一个生成器,似乎效果不错。secrets 允许开箱即用,并与第三方服务集成,也可用 bash 做任何想做的事情!


参考链接:https://gianarb.it/blog/kubernetes-github-action


2019-01-23 17:535900
用户头像
赵钰莹 极客邦科技 总编辑

发布了 894 篇内容, 共 679.5 次阅读, 收获喜欢 2694 次。

关注

评论

发布
暂无评论
发现更多内容

丝滑的打包部署,一套带走

Java你猿哥

Java Docker ssm 部署

百度阳泉智算中心全面升级 支持文心一言大规模智能算力需求

极客天地

信通院MLOps旗舰级评测,业内首批通过!

百度开发者中心

人工智能 深度学习‘’ 文心一言

软件测试/测试开发丨聊聊后端Web开发框架(Python)的简单使用

测试人

软件测试 自动化测试 测试开发

OPPO StarFire全新升级,异构计算突破算力瓶颈

安第斯智能云

人工智能 机器学习 云存储 智能云 端云协同

安全防火墙软件:Little Snitch 5 激活版

真大的脸盆

Mac 防火墙 Mac 软件 防火墙工具

软件测试常用的工具都有哪些-测试常用工具

测吧(北京)科技有限公司

测试

什么是软件测试?

测吧(北京)科技有限公司

测试

直播回顾 | 告警全生命周期管理的思路与落地实践

嘉为蓝鲸

IT

一文带你掌握轻量化模型设计原则和训练技巧!

Openlab_cosmoplat

工业互联网 开源社区 轻量化模型 openlab

强大的字体设计工具:Glyphs 3激活版

真大的脸盆

Mac 字体 字体设计 设计字体 字体管理工具

百度文心大模型开发者斩获CCF BDCI大赛唯一『最佳算法能力奖』

百度开发者中心

人工智能 数字经济 文心一言

OPPO大数据诊断平台设计与实践

安第斯智能云

大数据 诊断

软件测试/测试开发丨测试右移之logstash完整配置实例

测试人

软件测试 自动化测试 测试开发

云原生安全,会有一个较大的潜在市场丨统信软件孟杰

统信软件

云原生 安全 数字化

唯一入选的制品库!嘉为蓝鲸CPack制品管理平台成功入选!

嘉为蓝鲸

IT

【广州银行信用卡中心】5分钟实现一键发布!

嘉为蓝鲸

IT

嘉为科技与工银科技正式启动数字研运一体化合作项目

嘉为蓝鲸

软件测试 | 黑盒测试方法—场景法

测吧(北京)科技有限公司

测试

量化api现货合约跟单软件开发源代码

开发微hkkf5566

加速发展的低代码市场

力软低代码开发平台

研云运一体,嘉为蓝鲸助力中国智造扬帆出海!

嘉为蓝鲸

IT

浙商银行升鑫赢B-1号人民币理财产品

andy

自动化测试是什么?

测吧(北京)科技有限公司

测试

12306系统架构:抢票引起的对大型高并发系统架构的思考

【中远海运特运】WeOps产品为业务系统安全稳定保驾护航!

嘉为蓝鲸

打造江西数智产业高地,百度飞桨人工智能产业赋能中心落户南昌青山湖

百度开发者中心

人工智能 百度飞桨

WeOpsV3.15持续拓展监控能力,支持硬件设备IPMI智能监控

嘉为蓝鲸

IT

软件测试 | 黑盒测试方法—因果图法

测吧(北京)科技有限公司

测试

如何尝试用GitHub Actions项目编写容器应用?_语言 & 开发_赵钰莹_InfoQ精选文章