Github Action教程

799 字
4 分钟
Github Action教程

GitHub Action简介#

GitHub Actions 是一种持续集成和持续交付 (CI/CD) 平台,可用于自动执行生成、测试和部署管道。
并且GitHub提供了一系列的工作流模版,可以直接为GitHub action的新手用户提供直接的方案:

GitHub Action如何使用#

创建第一个工作流#

  1. 需要在个人仓库里面创建 .github/workflows(两级均为目录) 的结构,然后在里面添加 .github/workflows/github-actions-demo.yml(或者其他名称的.yml/.yaml文件)
  2. YAML文件中添加一下内容:
name: GitHub Actions Demo
run-name: ${{ github.actor }} is testing out GitHub Actions 🚀
on: [push]
jobs:
Explore-GitHub-Actions:
runs-on: ubuntu-latest
steps:
- run: echo "🎉 The job was automatically triggered by a ${{ github.event_name }} event."
- run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by GitHub!"
- run: echo "🔎 The name of your branch is ${{ github.ref }} and your repository is ${{ github.repository }}."
- name: Check out repository code
uses: actions/checkout@v6
- run: echo "💡 The ${{ github.repository }} repository has been cloned to the runner."
- run: echo "🖥️ The workflow is now ready to test your code on the runner."
- name: List files in the repository
run: |
ls ${{ github.workspace }}
- run: echo "🍏 This job's status is ${{ job.status }}."
  1. 最后将仓库commit 并且 push 上云,action就可以了
    GitHub action
    GitHub action
    在上图所在位置既可以查看action的执行结果了

GitHub Action工作流的内容解析#

工作流的触发器#

GitHub action的工作流触发器可以是一下的事件:

  • 工作流程存储库中发生的事件
  • 在 GitHub 外部发生并在 GitHub 上触发 repository_dispatch 事件的事件
  • 预定时间
  • 手动

对有与 on: 对于的触发器,在满足条件时就会启动
以刚才写的举例, on: [push] 指仓库被push更新时启动该action

GitHub Action的上下文#

上下文是一种访问工作流运行、变量、运行器环境、作业及步骤相关信息的方式。 每个上下文都是一个包含属性的对象,属性可以是字符串或其他对象。
您可以使用表达式语法访问上下文。 有关详细信息,请参阅 GitHub Actions 的工作流语法

${{ <context> }}

类似,上文中的 ${{ github.actor }} Btw,GitHub Actions的变量,可以在 env 中定义,但是请不要在其中放敏感信息

env:
MY_ENV_VAR: ${{ <expression> }}

GitHub Actions的执行器#

执行器是执行工作流中 GitHub Actions 作业的机器。 例如,运行程序可以在本地克隆存储库,安装测试软件,然后运行评估代码的命令。 GitHub 提供可用于运行作业的运行程序,或者你可以托管自己的运行程序,除单 CPU 运行器外,每个 GitHub 托管的运行器都是由 GitHub 托管的新虚拟机(VM) 可以使用的系统镜像可以查看: 系统镜像 在以上的实例中,其就是 runs-on: ubuntu-latest
以及这些系统预装的工具,都可以在上述的网址中找到

补充#

uses: actions/checkout@v6 这段的作用是拉取官方action的脚本来此处运行的意思, 结构是: 作者/仓库@版本

文章分享

如果这篇文章对你有帮助,欢迎分享给更多人!

Github Action教程
https://www.cacablog.top/posts/GithubAction/
作者
CaCa
发布于
2026-06-19
许可协议
CC BY-NC-SA 4.0

评论区

Profile Image of the Author
CaCa
Hello!远到而来的朋友,不如来我的酒馆里歇息一下吧!
公告
欢迎来到我的博客!这是一则示例公告。
音乐
封面

音乐

暂未播放

0:00 0:00
暂无歌词
分类
标签
站点统计
文章
9
分类
8
标签
11
总字数
8,722
运行时长
0
最后活动
0 天前

文章目录