GitHub Actions can be a little confusing if you’re new to DevOps and the CI/CD world, so in this article, we’re going to explore some features and see what we can do using the tool.
如果您是DevOps和CI / CD领域的新手,那么GitHub Actions可能会使您感到困惑,因此在本文中,我们将探索一些功能,并了解如何使用该工具。
From the point of view of CI/CD, the main goal of GitHub Actions and Workflows is to test our software every time something changes. This way we can spot bugs and correct things as soon as the errors come up.
从CI / CD的角度来看,GitHub Actions和Workflows的主要目标是每当发生变化时就测试我们的软件 。 这样,我们可以发现错误并在错误出现后立即纠正问题。
Today we’ll learn more about GitHub Actions and learn how to use them using Bash scripting and Python. Nice, let’s get started!
今天,我们将了解有关GitHub Actions的更多信息,并学习如何通过Bash脚本和Python使用它们。 好的,让我们开始吧!
GitHub动作与工作流程 (GitHub Actions vs. Workflows)
First, it’s good to state the difference between GitHub Actions and Workflows. As the GitHub Actions Documentation states, actions are “individual tasks that you can combine to create jobs and customize your workflow”. On the other hand, Workflows are “custom automated processes that you can set up in your repository to build, test, package, release, or deploy any project on GitHub”. In other words:
首先,最好说明GitHub Actions和Workflows之间的区别。 如GitHub Actions 文档所述,动作是“您可以组合以创建作业和自定义工作流程的单个任务 ”。 另一方面, 工作流是“您可以在存储库中设置的自定义自动化流程 ,以在GitHub上构建,测试,打包,发布或部署任何项目”。 换一种说法:
Workflows: automated processes that run on your repository; workflows can have many GitHub Actions
工作流 :在您的存储库上运行的自动化流程; 工作流可以有很多GitHub动作
GitHub Actions: individual tasks; they can be written using Docker, JavaScript and now also shell scrips with the new Composite Run Steps; you can write your own actions or use an action someone else created
GitHub动作 :单个任务; 它们可以使用Docker,JavaScript编写,现在还可以使用新的Composite Run Steps编写shell脚本; 您可以编写自己的动作或使用其他人创建的动作
编写我们的第一个工作流程 (Writing our first Workflow)
Let’s first create a workflow with no action just to understand how it works. Workflows are defined using YAML files and you must store them in the .github/workflows
directory in the root of your repository.
让我们首先创建一个没有任何动作的工作流,只是为了了解它是如何工作的。 工作流是使用YAML文件定义的,您必须将它们存储在存储库根目录的.github/workflows
目录中。
To create a workflow we need to define these things:
要创建工作流程,我们需要定义以下内容:
The event that triggers the workflow
触发工作流程 的事件
The machine each job should run
每个作业应运行 的机器
The jobs that make the workflow (jobs contain a set of steps that perform individual tasks and run in parallel by default)
使工作流程 的作业 (作业包含了一组执行单独的任务步骤,在默认情况下并行运行)
The steps each job should run
每个作业应执行 的步骤
The basic syntax for a workflow is:
工作流程的基本语法为:
on
— the event that triggers the workflowon
—触发工作流的事件runs-on
— the machine each job should runruns-on
—每个作业应运行的机器jobs
— the jobs that make the workflowjobs
-构成工作流程的工作steps
—the tasks each job should runsteps
-每个作业应执行的任务run
—the command the step should runrun
-该步骤应运行的命令
First, we need to define the event that triggers the workflow. In this example, we want to say greetings every time someone pushes to the repository.
首先,我们需要定义触发工作流的事件。 在此示例中,我们每次有人推送到存储库时都要打个招呼。
A workflow run is made up of one or more jobs. Each job runs in a machine specified by runs-on
. The machine can be either a GitHub-hosted runner, or a self-hosted runner. We’re going to use the ubuntu-latest
GitHub-hosted runner.
工作流程运行由一个或多个作业组成 。 每个作业都在runs-on
指定的机器上runs-on
。 该机器可以是GitHub托管的运行器,也可以是自托管的运行器。 我们将使用ubuntu-latest
GitHub托管运行器。
A job contains a sequence of tasks called steps. Steps can run commands, run setup tasks, or run an action. Each step has can have a run command that uses the operating system’s shell. Here we’re going to echo a message.
作业包含一系列称为步骤的任务。 步骤可以运行命令,运行设置任务或运行操作。 每个步骤都有一个使用操作系统外壳程序的运行命令。 在这里,我们将回显一条消息。
Confusing? Let’s see how it works. Go ahead and create a first_workflow.yml
file inside .github/workflows
on your repo, then paste this code:
令人困惑? 让我们看看它是如何工作的。 继续,在.github/workflows
上的.github/workflows
内部创建first_workflow.yml
文件,然后粘贴以下代码:
# your-repo-name/.github/workflows/first_workflow.ymlname: First Workflow on: push jobs:
first-job:
name: Say hi
runs-on: ubuntu-latest
steps:
- name: Print a greeting
run: echo Hi from our first workflow!
Now if you commit this file to your repository, push it and go to the Actions tab on the GitHub website you can see the new workflow, check all the runs and view the outputs:
现在,如果将此文件提交到存储库中,将其推送并转到GitHub网站上的“操作”选项卡,您将看到新的工作流程,检查所有运行并查看输出:
在我们的第一个工作流程中使用动作 (Using an Action in our first workflow)
Actions are individual tasks and we can use them from three sources:
动作是单独的任务 ,我们可以从三个来源使用它们:
Actions defined in the same repository as the workflow
在与工作流相同的存储库中定义的操作
Actions defined in a public repository
公共存储库中定义的操作
Actions defined in a published Docker container image
在已发布的Docker容器映像中定义的操作
They run as a step in our workflow. To call them we use the uses
syntax. In this example, we’re going to use an Action that prints ASCII art text, written by Mike Coutermarsh. Since it’s defined in a public repository we just need to pass it’s name:
它们是我们工作流程中的一步 。 打电话给他们,我们使用uses
语法。 在此示例中,我们将使用由Mike Coutermarsh编写的可打印ASCII艺术文本的 Action 。 由于它是在公共存储库中定义的,因此我们只需传递其名称即可:
# your-repo-name/.github/workflows/first_workflow.ymlname: First Workflowon: push jobs:
first-job:
name: Say hi
runs-on: ubuntu-latest
steps:
- name: Print a greeting
run: echo Hi from our first workflow!
- name: Show ASCII greeting
uses: mscoutermarsh/ascii-art-action@master
with:
text: 'HELLO!'
The with
syntax is a map of input parameters defined by the action. Here is the result:
with
语法是操作定义的输入参数的映射。 结果如下:
在工作流程中使用Python (Using Python with Workflows)
As Data Scientists we use a lot of Python in our day to day, so it’s a good idea to learn how to use it in our workflows. Setting a specific version of Python or PyPy is the recommended way of using Python with GitHub Actions because it “ensures consistent behavior across different runners and different versions of Python”. To do that we’ll use an action: setup-python
.
作为数据科学家,我们每天都会使用大量的Python,因此,学习如何在工作流程中使用它是一个好主意。 推荐使用特定版本的Python或PyPy,这是将Python与GitHub Actions结合使用的推荐方法,因为它“确保跨不同的运行器和不同版本的Python保持一致的行为”。 为此,我们将使用一个操作: setup-python
。
After that, we can run the commands as we usually do in a machine. Here we’ll install Scrapy and run a script to get some TDS posts about GitHub Actions, just to see how it works. Since we want to run a script from our own repository we also need to use the checkout
action to access it. Let’s create a new job to run the script:
之后,我们可以像在计算机中通常那样运行命令。 在这里,我们将安装Scrapy并运行脚本以获取有关GitHub Actions的TDS帖子,以了解其工作原理。 由于我们要从自己的存储库中运行脚本,因此我们还需要使用checkout
操作来访问它。 让我们创建一个新作业来运行脚本:
# your-repo-name/.github/workflows/first_workflow.ymlname: First Workflowon: push jobs:
first-job:
name: Say hi
runs-on: ubuntu-latest steps:
- name: Print a greeting
run: echo Hi from our first workflow!
- name: Show ASCII greeting
uses: mscoutermarsh/ascii-art-action@master
with:
text: 'HELLO!' get-posts-job: name: Get TDS posts
runs-on: ubuntu-latest steps:
- name: Check-out the repo under $GITHUB_WORKSPACE
uses: actions/checkout@v2
- name: Set up Python 3.8
uses: actions/setup-python@v2
with:
python-version: '3.8'
- name: Install Scrapy
run: pip install scrapy
- name: Get TDS posts about GitHub Actions
run: scrapy runspider posts_spider.py -o posts.json
I want to save the scraped posts to a file, so let’s learn how to do it.
我想将抓取的帖子保存到文件中,所以让我们学习如何做。
持续的工作流程数据 (Persisting workflow data)
An artifact is a file or collection of files produced during a workflow run. We can pass an artifact to another job in the same workflow or download it using the GitHub UI.
工件是在工作流程运行期间生成的文件或文件集合。 我们可以将工件传递给同一工作流程中的另一个作业,或者使用GitHub UI下载它。
Let’s download the JSON file with the posts to see how it works. To work with artifacts we use the upload-artifact
and download-artifact
actions. To upload an artifact we need to specify the path to the file or directory and the name of the artifact:
让我们下载包含这些帖子的JSON文件,以了解其工作原理。 为了处理工件,我们使用了upload-artifact
和download-artifact
动作。 要上传工件,我们需要指定文件或目录的路径以及工件的名称:
# your-repo-name/.github/workflows/first_workflow.yml# [...]get-posts-job:
name: Get TDS posts
runs-on: ubuntu-latest steps:
- name: Check-out the repo under $GITHUB_WORKSPACE uses: actions/checkout@v2
- name: Set up Python 3.8
uses: actions/setup-python@v2
with:
python-version: '3.8'
- name: Install Scrapy
run: pip install scrapy
- name: Get TDS posts about GitHub Actions
run: scrapy runspider posts_spider.py -o posts.json - name: Upload artifact
uses: actions/upload-artifact@v2
with:
name: posts
path: posts.json
And now we can download the file using the GitHub UI:
现在我们可以使用GitHub UI下载文件:
创建您的第一个动作 (Creating your first Action)
Actions can be created using Docker containers, JavaScript or you can create an action using shell scripts (a composite run steps action). The main use case for the composite run steps action is when you have a lot of shell scripts to automate tasks and writing a shell script to combine them is easier than JavaScript or Docker.
可以使用Docker容器,JavaScript创建操作,也可以使用Shell脚本创建操作(复合运行步骤操作)。 组合运行步骤操作的主要用例是,当您有很多可以自动执行任务的shell脚本,并且编写比这些JavaScript或Docker更容易组合它们的shell脚本时。
The three main components of an action are runs
, inputs
and outputs
.
动作的三个主要组成部分是runs
, inputs
和outputs
。
runs
— (required) Configures the path to the action’s code and the application used to execute the code.runs
— (必需)配置操作代码和用于执行代码的应用程序的路径。inputs
— (optional) Input parameters allow you to specify data that the action expects to use during runtimeinputs
- (可选)输入参数使您可以指定操作期望在运行时使用的数据outputs
— (optional) Output parameters allow you to declare data that an action setsoutputs
— (可选)输出参数使您可以声明操作设置的数据
The filename for an action must be either action.yml
or action.yaml
. Let’s use the example from GitHub Docs and create an action that prints a “Hey [user]” message. Since it’s a composite action we’ll use the using: "composite"
syntax:
动作的文件名必须是action.yml
或action.yaml
。 让我们使用GitHub Docs中的示例,并创建一个打印“ Hey [user]”消息的操作。 由于这是一个复合操作,因此我们将使用using: "composite"
语法:
# name: 'Hey From a GitHub Action'description: 'Greet someone'inputs:
user: # id of input
description: 'Who to greet'
required: true
default: 'You'runs:
using: "composite"
steps:
- run: echo Hey ${{ inputs.user }}.
shell: bash
If an action is defined in the same repository as the workflow we can refer to it using ./path-to-action-file
. In this case, we need to access the file inside the repository, so we also need to use the checkout
action.
如果在与工作流相同的存储库中定义了动作,则可以使用./path-to-action-file
引用该./path-to-action-file
。 在这种情况下,我们需要访问存储库中的文件,因此我们还需要使用checkout
操作。
If the action is defined in a public repository we can refer to it using a specific commit, a version of a release or a branch.
如果操作是在公共存储库中定义的,则可以使用特定的提交,发行版或分支来引用该操作。
从同一存储库运行操作 (Running an action from the same repository)
# .github/workflows/use-action-same-repo.ymlname: Action from the same repoon: push
jobs:
use-your-own-action:
name: Say Hi using your own action
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: ./ # the action.yml file is in the root folder
with:
user: 'Déborah'
从公共存储库运行操作 (Running an action from a public repository)
# .github/workflows/use-action-public-repo.ymlname: Action from a public repositoryon: push
jobs:
use-your-own-action:
name: Say Hi using your own action
runs-on: ubuntu-latest
steps:
- uses: dmesquita/github-actions-tutorial@master
with:
user: 'Déborah'
And that’s if for our tour. You can also use variables and secrets in your workflow, cache dependencies to speed up them and connect databases and service containers to manage workflow tools.
那就是我们的旅行。 您还可以在工作流中使用变量和机密 , 缓存依赖项以加快它们的速度,并连接数据库和服务容器以管理工作流工具。
In this guide, we didn’t use one of the main cool things of CI which is to test often so we can spot bugs early on in the process. But I think it’s good to understand how the actions and workflow work before doing that. I hope now it’ll be easier to start using CI tools, I plan to do that on the next tutorials.
在本指南中,我们没有使用CI的主要优点之一,它是经常测试,因此我们可以在过程的早期发现bug。 但是我认为在执行操作之前先了解操作和工作流程是如何工作的。 我希望现在开始使用CI工具会更容易,我计划在下一个教程中做到这一点。
You can see all the code we used here: https://github.com/dmesquita/github-actions-tutorial
您可以在此处查看我们使用的所有代码: https : //github.com/dmesquita/github-actions-tutorial
That’s it for today, thanks for reading!
今天就这样,感谢您的阅读!
翻译自: https://towardsdatascience.com/introduction-to-github-actions-7fcb30d0f959
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/388692.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!