Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
github
github_CI_action
CI/CD_持续集成部署
Github Actions 的使用方法,构建属于你的 CI
1.00
发布于 2021-03-11 17:07:02 浏览:1634
订阅该版
[tocm] ## 什么是 CI (CONTINUOUS INTEGRATION) 在持续集成环境中,开发人员将会频繁的提交代码到主干。这些新提交在最终合并到主线之前,都需要通过编译和[自动化](http://www.ttlsa.com/auto/)测试流进行验证。这样做是基于之前 持续集成过程中很重视自动化测试验证结果,以保障所有的提交在合并主线之后的质量问题,对可能出现的一些问题进行预警 ## GitHub Actions > GitHub Actions makes it easy to automate all your software workflows, now with world-class CI/CD. Build, test, and deploy your code right from GitHub. Make code reviews, branch management, and issue triaging work the way you want. ## 使用方法 ### 创建工作流程 - 从 GitHub 上的仓库,在 `.github/workflow` 目录中创建一个名为 `action.yml` 的新文件。 更多信息请参阅“[创建新文件](https://docs.github.com/cn/free-pro-team@latest/github/managing-files-in-a-repository/creating-new-files)”。 - 将以下 YAML 内容复制到 `action.yml` 文件中。 **注:** 如果默认分支不是 `main`,请更新 `DEFAULT_BRANCH` 的值以匹配仓库的默认分支名称。目前 Github 新建的仓库主分支默认为 main。 ```c name: action-test # Run this workflow every time a new commit pushed to your repository on: push jobs: # Set the job key. The key is displayed as the job name # when a job name is not provided action-test: # Name the Job name: action test # Set the type of machine to run on runs-on: ubuntu-latest steps: # Checks out a copy of your repository on the ubuntu-latest machine - name: Checkout code uses: actions/checkout@v2 # Runs the test - name: Run test shell: bash run: echo "hello, world" ``` - 在仓库中提交工作流程文件会触发 `push` 事件并运行工作流程。 ### 查看工作流程 - 在 GitHub 上,导航到仓库的主页面。 - 在仓库名称下,单击 **Actions(操作)**。  - 在左侧边栏中,单击您想要查看的工作流程。  - 从工作流程运行列表中,单击要查看的运行的名称。  - 在左侧边栏中,单击 **action-test**作业。  - 查看 CI 构建流程以及日志。  ## 高阶用法 ### 定时 可以使用 [POSIX cron 语法](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/crontab.html#tag_20_25_07)安排工作流程在特定的 UTC 时间运行。 ```c # Runs at 16:00 UTC on the 1st of every month schedule: - cron: '0 16 1 * *' ``` 自动触发 CI 构建:  ### env 设置环境变量 ```c env: SERVER: production ``` - 在工作流程中使用 env, 此时设置的环境变量只能作用于当前作业步骤: ```c steps: - name: Hello world run: echo Hello world $FIRST_NAME $middle_name $Last_Name! env: FIRST_NAME: Mona middle_name: The Last_Name: Octocat ``` - 也可以使用 `GITHUB_ENV`, 此时设置的环境变量可用于此步骤和以下的所有作业的步骤: ```c steps: - name: Hello world1 run: echo "TEST_ENV=true" >> $GITHUB_ENV - name: Hello world2 run: echo $TEST_ENV ``` ### 构建矩阵 #### 使用多个 Python ```c strategy: matrix: python: [3.5, 3.6, 3.7, 3.8] steps: - uses: actions/setup-node@v1 with: python-version: ${{ matrix.python }} ``` #### 使用多个操作系统 ```c runs-on: ${{ matrix.os }} strategy: matrix: os: [ubuntu-16.04, ubuntu-18.04] python: [3.5, 3.6, 3.7, 3.8] steps: - uses: actions/setup-node@v1 with: python-version: ${{ matrix.python }} ``` #### 在矩阵中使用环境变量 在 RT-Thread 的 CI 中,就是用了这种方法,来编译仓库中的 bsp:  Example: [RT-Thread action](https://github.com/RT-Thread/rt-thread/blob/master/.github/workflows/action.yml) ```c strategy: # 设置为 true 时,如果任何 matrix 作业失败,GitHub 将取消所有进行中的作业。 默认值:true fail-fast: false matrix: legs: - {RTT_BSP: "CME_M7", RTT_TOOL_CHAIN: "sourcery-arm"} - {RTT_BSP: "apollo2", RTT_TOOL_CHAIN: "sourcery-arm"} - {RTT_BSP: "asm9260t", RTT_TOOL_CHAIN: "sourcery-arm"} - {RTT_BSP: "at91sam9260", RTT_TOOL_CHAIN: "sourcery-arm"} - {RTT_BSP: "allwinner_tina", RTT_TOOL_CHAIN: "sourcery-arm"} - {RTT_BSP: "efm32", RTT_TOOL_CHAIN: "sourcery-arm"} - {RTT_BSP: "gd32e230k-start", RTT_TOOL_CHAIN: "sourcery-arm"} - {RTT_BSP: "gd32303e-eval", RTT_TOOL_CHAIN: "sourcery-arm"} - {RTT_BSP: "gd32450z-eval", RTT_TOOL_CHAIN: "sourcery-arm"} - {RTT_BSP: "gkipc", RTT_TOOL_CHAIN: "sourcery-arm"} - {RTT_BSP: "imx6sx/cortex-a9", RTT_TOOL_CHAIN: "sourcery-arm"} - {RTT_BSP: "imxrt/imxrt1052-atk-commander", RTT_TOOL_CHAIN: "sourcery-arm"} - {RTT_BSP: "imxrt/imxrt1052-fire-pro", RTT_TOOL_CHAIN: "sourcery-arm"} - {RTT_BSP: "imxrt/imxrt1052-nxp-evk", RTT_TOOL_CHAIN: "sourcery-arm"} - {RTT_BSP: "lm3s8962", RTT_TOOL_CHAIN: "sourcery-arm"} - {RTT_BSP: "lm3s9b9x", RTT_TOOL_CHAIN: "sourcery-arm"} steps: - uses: actions/checkout@v2 - name: Set up Python uses: actions/setup-python@master with: python-version: 3.8 ``` ### IF 语法 Example: [RT-ThreadStudio action](https://github.com/RT-Thread-Studio/sdk-index/blob/master/.github/workflows/action.yml) 可以使用 `if` 条件阻止作业在条件得到满足之前运行。 可以使用任何支持上下文和表达式来创建条件 在 RT-Thread 仓库 CI 文件中,根据矩阵中设置的环境变量,来判断当前 BSP 的架构,拉取对应的工具链:  #### 主分支触发 检查触发工作流程的分支本仓库并且上一个步骤成功时才会执行,否则跳过该步骤: ```c - name: Generate-Import-Compile shell: bash if: ${{ github.ref != 'refs/heads/master' && success() }} run: | cd ${{ github.workspace }} ``` #### 合并时触发 当本仓库触发 merge_request 动作时, CI 就会被激活: ```c if: github.event.pull_request.merged ``` ## Marketplace 你可以在 Github 的 Marketplace 发现很多 Actions, 将这些已经写好的 action 添加到你的 CI 文件中,会大大减少写 CI 的工作量。 #### Checkout V2 拉取当前仓库代码。 [链接](https://github.com/actions/checkout) > This action checks-out your repository under `$GITHUB_WORKSPACE`, so your workflow can access it. ```c - uses: actions/checkout@v2 ``` #### setup-python V2 设置 Python 环境。 [链接](https://github.com/actions/setup-python) > This action sets up a Python environment for ```c - name: Set up Python uses: actions/setup-python@master with: python-version: 3.8 ``` #### Upload-Artifact v2 上传产物。 [链接](https://github.com/actions/upload-artifact) > This uploads artifact from your workflow allowing you to share data between jobs and store data once a workflow is complete. ```c - name: Generate txt run: echo "hellowold" > test.txt - name: Upload Results if: ${{ github.ref != 'refs/heads/master' && success() }} uses: actions/upload-artifact@v2 continue-on-error: True with: name: check-report path: ${{ github.workspace }}/test.txt ``` #### file-existence-action 判断文件是否存在。 [链接](https://github.com/andstor/file-existence-action/) > This is a GitHub Action to check for the existence of files. It can be used for conditionally running workflow steps based on file(s) existence. ```c name: "File existence check" on: [push, pull_request] jobs: file_existence: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v1 - name: Check file existence id: check_files uses: andstor/file-existence-action@v1 with: files: "package.json, LICENSE, README.md" - name: File exists if: steps.check_files.outputs.files_exists == 'true' # Only runs if all of the files exists run: echo All files exists! ``` #### Close Stale Issues and PRs 自动关闭陈旧的 Issues 和 PR。 [链接](https://github.com/actions/stale) > Warns and then closes issues and PRs that have had no activity for a specified amount of time. ```c name: "Close stale issues" on: schedule: - cron: "30 1 * * *" jobs: stale: runs-on: ubuntu-latest steps: - uses: actions/stale@v3 with: repo-token: ${{ secrets.GITHUB_TOKEN }} stale-issue-message: 'Message to comment on stale issues. If none provided, will not mark issues stale' stale-pr-message: 'Message to comment on stale PRs. If none provided, will not mark PRs stale' ```
2
条评论
默认排序
按发布时间排序
登录
注册新账号
关于作者
Papalymo
请勿打扰
文章
25
回答
85
被采纳
17
关注TA
发私信
相关文章
1
RT_Thread ENV 下载问题
2
github下载时总是中断失败
3
提高github的响应速度
4
RT-thread ENV 工具配置软件包时总是无法连上git网址,无法下载?
5
软件包下载问题解决经验
6
无法下载littlevgl 软件包
7
软件包无法下载(已解决)
8
RT-Thread studio 如何更换git下载源?
9
【github】大家在用github有哪些问题
10
github登录不了,重置密码也不行
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
国产MCU移植系列教程汇总,欢迎查看!
4
机器人操作系统 (ROS2) 和 RT-Thread 通信
5
五分钟玩转RT-Thread新社区
6
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
DMA
USB
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
rt-smart
FAL
I2C_IIC
UART
ESP8266
cubemx
WIZnet_W5500
ota在线升级
PWM
BSP
flash
freemodbus
packages_软件包
潘多拉开发板_Pandora
定时器
ADC
GD32
flashDB
编译报错
socket
中断
Debug
rt_mq_消息队列_msg_queue
keil_MDK
ulog
SFUD
msh
C++_cpp
MicroPython
本月问答贡献
RTT_逍遥
10
个答案
3
次被采纳
xiaorui
3
个答案
3
次被采纳
踩姑娘的小蘑菇
6
个答案
2
次被采纳
winfeng
2
个答案
2
次被采纳
三世执戟
8
个答案
1
次被采纳
本月文章贡献
catcatbing
3
篇文章
5
次点赞
lizimu
2
篇文章
9
次点赞
jinchanchan
2
篇文章
2
次点赞
alight
1
篇文章
5
次点赞
swet123
1
篇文章
4
次点赞
回到
顶部
发布
问题
投诉
建议
回到
底部