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.pswp.cn/news/388692.shtml 繁體地址,請注明出處:http://hk.pswp.cn/news/388692.shtml 英文地址,請注明出處:http://en.pswp.cn/news/388692.shtml
如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!