简介
您只需要一个现有的 GitHub 仓库来创建和运行 GitHub Actions 工作流程。 在本指南中,您将添加一个工作流程,使用 GitHub Super-Linter 操作链接多种编码语言。 每次有新的提交被推送到您的仓库时,工作流程都会使用 Super-Linter 验证源代码。
创建第一个工作流程
-
从 GitHub 上的仓库,在
.github/workflow
目录中创建一个名为superlinter.yml
的新文件。 更多信息请参阅“创建新文件”。 -
将以下 YAML 内容复制到
superlinter.yml
文件中。 注: 如果您的默认分支不是main
,请更新DEFAULT_BRANCH
的值以匹配您仓库的默认分支名称。YAML name: Super-Linter # 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 super-lint: # Name the Job name: Lint code base # 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 Super-Linter action - name: Run Super-Linter uses: github/super-linter@v3 env: DEFAULT_BRANCH: main GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
-
要运行您的工作流程, 滚动到页面底部,然后选择 为此提交创建一个新分支并开始拉取请求。 然后,若要创建拉取请求,请单击 Propose new file(提议新文件)。
在仓库中提交工作流程文件会触发 push
事件并运行工作流程。
查看工作流程结果
-
在 GitHub 上,导航到仓库的主页面。
-
在仓库名称下,单击 Actions(操作)。
-
在左侧边栏中,单击您想要查看的工作流程。
-
从工作流程运行列表中,单击要查看的运行的名称。
-
在 Jobs(作业)下或可视化图中,单击 Lint code base(Lint 代码基础)作业。
-
任何失败的步骤都会自动展开以显示结果。
More workflow templates
GitHub provides preconfigured workflow templates that you can customize to create your own continuous integration workflow. GitHub analyzes your code and shows you CI templates that might be useful for your repository. 例如,如果仓库包含 Node.js 代码,您就会看到 Node.js 项目的建议。 您可以使用工作流程模板作为基础来构建自定义工作流程,或按原样使用模板。
您可以在 actions/starter-workflows 仓库中浏览工作流程模板的完整列表。
后续步骤
只要代码推送到仓库,您刚才添加的 super-linter 工作流程就会运行,以帮助您发现代码中的错误和不一致。 但是,这只是您可以对 GitHub Actions 执行操作的开始。 您的仓库可以包含多个基于不同事件触发不同任务的工作流程。 GitHub Actions 可以帮助您自动执行应用程序开发过程的几乎每个方面。 准备好开始了吗? 以下是一些帮助您对 GitHub Actions 执行后续操作的有用资源:
- “了解 GitHub Actions”,以获取深入教程
- “指南”,以获取特定用例和示例
- github/super-linter,以获取有关配置 Super-Linter 操作的详细信息
Introduction
打印“Hello, World!”是探索新编程语言的基本设置和语法的好方法。 在本指南中,您将使用 GitHub Actions 在 GitHub 仓库的工作流程日志中打印“Hello, World!”。 您只需要一个您可以在其中轻松创建和运行示例 GitHub Actions 工作流程的 GitHub 仓库。 请随意为此快速入门创建新仓库,您可以使用它来测试此仓库和未来的 GitHub Actions 工作流程。
Creating your first workflow
-
从 GitHub 上的仓库,在
.github/workflows
目录中创建一个名为hello-world.yml
的新文件。 更多信息请参阅“创建新文件”。 -
将以下 YAML 内容复制到
hello-world.yml
文件中。YAML name: Say hello! # GitHub Actions Workflows are automatically triggered by GitHub events on: # For this workflow, we're using the workflow_dispatch event which is triggered when the user clicks Run workflow in the GitHub Actions UI workflow_dispatch: # The workflow_dispatch event accepts optional inputs so you can customize the behavior of the workflow inputs: name: description: 'Person to greet' required: true default: 'World' # When the event is triggered, GitHub Actions will run the jobs indicated jobs: say_hello: # Uses a ubuntu-latest runner to complete the requested steps runs-on: ubuntu-latest steps: - run: | echo "Hello ${{ github.event.inputs.name }}!"
-
滚动到页面底部,然后选择 Create a new branch for this commit and start a pull request(为此提交创建一个新分支并开始拉取请求)。 然后,若要创建拉取请求,请单击 Propose new file(提议新文件)。
-
合并拉取请求后,即可继续“触发工作流程”。
Trigger your workflow
- 在 GitHub 上,导航到仓库的主页面。
- 在仓库名称下,单击 Actions(操作)。
- In the left sidebar, click the workflow you want to run.
- 在右侧,单击 Run workflow(运行工作流程)下拉菜单,然后单击 Run workflow(运行工作流程)。 (可选)您可以在运行工作流程之前在“问候的人(Person to greet)”输入中输入自定义消息。
- 工作流程运行将出现在 "Say hello!" 工作流程运行列表顶部。 单击 "Say hello!" 可查看工作流程运行结果。
- 在左侧栏中,单击 "say_hello" 作业。
- 在工作流程日志中,展开 'Run echo "Hello World!"' 部分。
More workflow templates
GitHub provides preconfigured workflow templates that you can customize to create your own continuous integration workflow. GitHub analyzes your code and shows you CI templates that might be useful for your repository. 例如,如果仓库包含 Node.js 代码,您就会看到 Node.js 项目的建议。 您可以使用工作流程模板作为基础来构建自定义工作流程,或按原样使用模板。
您可以在 actions/starter-workflows 仓库中浏览工作流程模板的完整列表。
Next steps
The hello-world workflow you just added is a simple example of a manually triggered workflow. 这只是您可以对 GitHub Actions 执行操作的开始。 您的仓库可以包含多个基于不同事件触发不同任务的工作流程。 GitHub Actions 可以帮助您自动执行应用程序开发过程的几乎每个方面。 准备好开始了吗? 下面是一些可帮助您执行 GitHub Actions 后续步骤的有用资源:
- “了解 GitHub Actions”,用于深入的教程
- “Guides”,用于特定的使用情况和示例