食品品牌网站策划,九江市区,门户网站栏目规范化建设,江西省兴赣建设监理咨询有限公司网站Github Actions 中 Service Container 的使用Intro之前写过一个 StackExchange.Redis 的一个扩展#xff0c;测试项目依赖 redis#xff0c;所以之前测试一直只是在本地跑一下#xff0c;最近通过 Github Action 中的 Service Container 来通过 CI 来跑测试#xff0c;分享… Github Actions 中 Service Container 的使用Intro之前写过一个 StackExchange.Redis 的一个扩展测试项目依赖 redis所以之前测试一直只是在本地跑一下最近通过 Github Action 中的 Service Container 来通过 CI 来跑测试分享一下如何使用 service container 来跑测试不仅仅是 Redis数据库等依赖也可以使用这样的方式来测试Redis Service Container Samplejobs:# Label of the runner jobrunner-job:# You must use a Linux environment when using service containers or container jobsruns-on: ubuntu-latest# Service containers to run with runner-jobservices:# Label used to access the service containerredis:# Docker imageimage: redis:alpine# Set health checks to wait until redis has startedoptions: ---health-cmd redis-cli ping--health-interval 10s--health-timeout 5s--health-retries 5ports:# Maps port 6379 on service container to the host- 6379:6379
上面是一个 redis service container 配置示例的一部分需要注意的是使用 service container 的时候必须要使用 Linux 环境因为 service container 的本质就是 docker run 了一个 container通过一定的规则配置来实现在跑 CI 的环境可以访问的这个 service上面的示例配置了一个 redis 的 service container并将容器服务的 6379 端口映射到 host 的 6379 端口这样 host 上的服务就可以通过 127.0.0.1:6379/localhost:6379 访问到使用 docker 跑起来的 redis 服务(redis service container)了steps:# Downloads a copy of the code in your repository before running CI tests- name: Check out repository codeuses: actions/checkoutv2# Performs a clean installation of all dependencies in the package.json file# For more information, see https://docs.npmjs.com/cli/ci.html- name: Install dependenciesrun: npm ci- name: Connect to Redis# Runs a script that creates a Redis client, populates# the client with data, and retrieves datarun: node client.js# Environment variable used by the client.js script to create# a new Redis client.env:# The hostname used to communicate with the Redis service containerREDIS_HOST: localhost# The default Redis portREDIS_PORT: 6379
Container Job Sample上面的这种形式是在 host 上跑的也就是直接在跑 CI 的服务器上跑的有些情况下环境的配置比较麻烦的情况下也可以直接在指定的 docker 镜像为基础的 docker container 里跑 CI需要注意的是 docker container 里跑 CI 的时候和直接在 host 上跑 CI 网络上有区别 host 可能就是直接访问 localhostcontainer 访问就是 service 名称来看下面的 container 的一个示例jobs:# Label of the container jobcontainer-job:# Containers must run in Linux based operating systemsruns-on: ubuntu-latest# Docker Hub image that container-job executes incontainer: node:10.18-jessie# Service containers to run with container-jobservices:# Label used to access the service containerredis:# Docker Hub imageimage: redis# Set health checks to wait until redis has startedoptions: ---health-cmd redis-cli ping--health-interval 10s--health-timeout 5s--health-retries 5
可以看到大部分是一样的只是多了一个 container 的配置这样实际的 CI 就是在这个 container 里执行的创建的执行 CI 的 container 和 service container 是在同一个 network 下可以直接通过服务名称来访问steps:# Downloads a copy of the code in your repository before running CI tests- name: Check out repository codeuses: actions/checkoutv2# Performs a clean installation of all dependencies in the package.json file# For more information, see https://docs.npmjs.com/cli/ci.html- name: Install dependenciesrun: npm ci- name: Connect to Redis# Runs a script that creates a Redis client, populates# the client with data, and retrieves datarun: node client.js# Environment variable used by the client.js script to create a new Redis client.env:# The hostname used to communicate with the Redis service containerREDIS_HOST: redis# The default Redis portREDIS_PORT: 6379
Sample提供一个我目前在用的一个 service container和上面的示例基本是类似的有需要的可以参考一下name: dotnetcoreon: [push]jobs:# Label of the container jobredis-integration-test:# Containers must run in Linux based operating systemsruns-on: ubuntu-latest# # Docker image that job executes in# container: mcr.microsoft.com/dotnet/sdk:5.0# Service containers to run with container-job# https://docs.github.com/en/free-pro-teamlatest/actions/guides/creating-redis-service-containersservices:# Label used to access the service containerredis:# Docker Hub imageimage: redis:alpine# Set health checks to wait until redis has startedoptions: ---health-cmd redis-cli ping--health-interval 10s--health-timeout 5s--health-retries 5ports:# Maps port 6379 on service container to the host- 6379:6379steps:- uses: actions/checkoutv1- name: Setup .NET Coreuses: actions/setup-dotnetv1with:dotnet-version: 5.0.x- name: dotnet inforun: dotnet --info- name: buildrun: bash build.sh --targettest
CI 执行日志从日志上我们可以看出来比普通的 CI 执行会多出两个步骤一个是初始化 container一个是清理 container完整的CI 日志可以在这里看到https://github.com/WeihanLi/WeihanLi.Redis/runs/1400006789?check_suite_focustrueMore虽然我的场景是 redis但是不仅仅是 redis很多应用外的依赖比如说数据库甚至MQ等都是可以通过 service container 来做一个完善的集成测试没有尝试过的快去试试吧~~Referenceshttps://docs.github.com/en/free-pro-teamlatest/actions/guides/about-service-containershttps://docs.github.com/en/free-pro-teamlatest/actions/guides/creating-redis-service-containershttps://github.com/WeihanLi/WeihanLi.Redis/blob/dev/.github/workflows/dotnetcore.yml