GitHub动作简介
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領(lǐng)域的新手,那么GitHub Actions可能會(huì)使您感到困惑,因此在本文中,我們將探索一些功能,并了解如何使用該工具。
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的角度來(lái)看,GitHub Actions和Workflows的主要目標(biāo)是每當(dāng)發(fā)生變化時(shí)就測(cè)試我們的軟件 。 這樣,我們可以發(fā)現(xiàn)錯(cuò)誤并在錯(cuò)誤出現(xiàn)后立即糾正問(wèn)題。
Today we’ll learn more about GitHub Actions and learn how to use them using Bash scripting and Python. Nice, let’s get started!
今天,我們將了解有關(guān)GitHub Actions的更多信息,并學(xué)習(xí)如何通過(guò)Bash腳本和Python使用它們。 好的,讓我們開(kāi)始吧!
GitHub動(dòng)作與工作流程 (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:
首先,最好說(shuō)明GitHub Actions和Workflows之間的區(qū)別。 如GitHub Actions 文檔所述,動(dòng)作是“您可以組合以創(chuàng)建作業(yè)和自定義工作流程的單個(gè)任務(wù) ”。 另一方面, 工作流是“您可以在存儲(chǔ)庫(kù)中設(shè)置的自定義自動(dòng)化流程 ,以在GitHub上構(gòu)建,測(cè)試,打包,發(fā)布或部署任何項(xiàng)目”。 換一種說(shuō)法:
Workflows: automated processes that run on your repository; workflows can have many GitHub Actions
工作流 :在您的存儲(chǔ)庫(kù)上運(yùn)行的自動(dòng)化流程; 工作流可以有很多GitHub動(dòng)作
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動(dòng)作 :單個(gè)任務(wù); 它們可以使用Docker,JavaScript編寫,現(xiàn)在還可以使用新的Composite Run Steps編寫shell腳本; 您可以編寫自己的動(dòng)作或使用其他人創(chuàng)建的動(dòng)作
編寫我們的第一個(gè)工作流程 (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.
讓我們首先創(chuàng)建一個(gè)沒(méi)有任何動(dòng)作的工作流,只是為了了解它是如何工作的。 工作流是使用YAML文件定義的,您必須將它們存儲(chǔ)在存儲(chǔ)庫(kù)根目錄的.github/workflows目錄中。
To create a workflow we need to define these things:
要?jiǎng)?chuàng)建工作流程,我們需要定義以下內(nèi)容:
The event that triggers the workflow
觸發(fā)工作流程 的事件
The machine each job should run
每個(gè)作業(yè)應(yīng)運(yùn)行 的機(jī)器
The jobs that make the workflow (jobs contain a set of steps that perform individual tasks and run in parallel by default)
使工作流程 的作業(yè) (作業(yè)包含了一組執(zhí)行單獨(dú)的任務(wù)步驟,在默認(rèn)情況下并行運(yùn)行)
The steps each job should run
每個(gè)作業(yè)應(yīng)執(zhí)行 的步驟
The basic syntax for a workflow is:
工作流程的基本語(yǔ)法為:
on — the event that triggers the workflow
on —觸發(fā)工作流的事件
runs-on — the machine each job should run
runs-on —每個(gè)作業(yè)應(yīng)運(yùn)行的機(jī)器
jobs — the jobs that make the workflow
jobs -構(gòu)成工作流程的工作
steps —the tasks each job should run
steps -每個(gè)作業(yè)應(yīng)執(zhí)行的任務(wù)
run —the command the step should run
run -該步驟應(yīng)運(yùn)行的命令
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.
首先,我們需要定義觸發(fā)工作流的事件。 在此示例中,我們每次有人推送到存儲(chǔ)庫(kù)時(shí)都要打個(gè)招呼。
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.
工作流程運(yùn)行由一個(gè)或多個(gè)作業(yè)組成 。 每個(gè)作業(yè)都在runs-on指定的機(jī)器上runs-on 。 該機(jī)器可以是GitHub托管的運(yùn)行器,也可以是自托管的運(yùn)行器。 我們將使用ubuntu-latest GitHub托管運(yùn)行器。
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.
作業(yè)包含一系列稱為步驟的任務(wù)。 步驟可以運(yùn)行命令,運(yùn)行設(shè)置任務(wù)或運(yùn)行操作。 每個(gè)步驟都有一個(gè)使用操作系統(tǒng)外殼程序的運(yùn)行命令。 在這里,我們將回顯一條消息。
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:
令人困惑? 讓我們看看它是如何工作的。 繼續(xù),在.github/workflows上的.github/workflows內(nèi)部創(chuàng)建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:
現(xiàn)在,如果將此文件提交到存儲(chǔ)庫(kù)中,將其推送并轉(zhuǎn)到GitHub網(wǎng)站上的“操作”選項(xiàng)卡,您將看到新的工作流程,檢查所有運(yùn)行并查看輸出:
The outputs of our fist workflow拳頭工作流程的輸出在我們的第一個(gè)工作流程中使用動(dòng)作 (Using an Action in our first workflow)
Actions are individual tasks and we can use them from three sources:
動(dòng)作是單獨(dú)的任務(wù) ,我們可以從三個(gè)來(lái)源使用它們:
Actions defined in the same repository as the workflow
在與工作流相同的存儲(chǔ)庫(kù)中定義的操作
Actions defined in a public repository
公共存儲(chǔ)庫(kù)中定義的操作
Actions defined in a published Docker container image
在已發(fā)布的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語(yǔ)法。 在此示例中,我們將使用由Mike Coutermarsh編寫的可打印ASCII藝術(shù)文本的 Action 。 由于它是在公共存儲(chǔ)庫(kù)中定義的,因此我們只需傳遞其名稱即可:
# 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語(yǔ)法是操作定義的輸入?yún)?shù)的映射。 結(jié)果如下:
The result of the ASCII art ActionASCII art Action的結(jié)果在工作流程中使用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.
作為數(shù)據(jù)科學(xué)家,我們每天都會(huì)使用大量的Python,因此,學(xué)習(xí)如何在工作流程中使用它是一個(gè)好主意。 推薦使用特定版本的Python或PyPy,這是將Python與GitHub Actions結(jié)合使用的推薦方法,因?yàn)樗按_保跨不同的運(yùn)行器和不同版本的Python保持一致的行為”。 為此,我們將使用一個(gè)操作: 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:
之后,我們可以像在計(jì)算機(jī)中通常那樣運(yùn)行命令。 在這里,我們將安裝Scrapy并運(yùn)行腳本以獲取有關(guān)GitHub Actions的TDS帖子,以了解其工作原理。 由于我們要從自己的存儲(chǔ)庫(kù)中運(yùn)行腳本,因此我們還需要使用checkout操作來(lái)訪問(wèn)它。 讓我們創(chuàng)建一個(gè)新作業(yè)來(lái)運(yùn)行腳本:
# 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.
我想將抓取的帖子保存到文件中,所以讓我們學(xué)習(xí)如何做。
持續(xù)的工作流程數(shù)據(jù) (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.
工件是在工作流程運(yùn)行期間生成的文件或文件集合。 我們可以將工件傳遞給同一工作流程中的另一個(gè)作業(yè),或者使用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動(dòng)作。 要上傳工件,我們需要指定文件或目錄的路徑以及工件的名稱:
# 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:
現(xiàn)在我們可以使用GitHub UI下載文件:
Downloading the artifact下載工件創(chuàng)建您的第一個(gè)動(dòng)作 (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創(chuàng)建操作,也可以使用Shell腳本創(chuàng)建操作(復(fù)合運(yùn)行步驟操作)。 組合運(yùn)行步驟操作的主要用例是,當(dāng)您有很多可以自動(dòng)執(zhí)行任務(wù)的shell腳本,并且編寫比這些JavaScript或Docker更容易組合它們的shell腳本時(shí)。
The three main components of an action are runs , inputs and outputs.
動(dòng)作的三個(gè)主要組成部分是runs , inputs和outputs 。
runs — (required) Configures the path to the action’s code and the application used to execute the code.
runs — (必需)配置操作代碼和用于執(zhí)行代碼的應(yīng)用程序的路徑。
inputs — (optional) Input parameters allow you to specify data that the action expects to use during runtime
inputs - (可選)輸入?yún)?shù)使您可以指定操作期望在運(yùn)行時(shí)使用的數(shù)據(jù)
outputs — (optional) Output parameters allow you to declare data that an action sets
outputs — (可選)輸出參數(shù)使您可以聲明操作設(shè)置的數(shù)據(jù)
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:
動(dòng)作的文件名必須是action.yml或action.yaml 。 讓我們使用GitHub Docs中的示例,并創(chuàng)建一個(gè)打印“ Hey [user]”消息的操作。 由于這是一個(gè)復(fù)合操作,因此我們將使用using: "composite"語(yǔ)法:
# 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.
如果在與工作流相同的存儲(chǔ)庫(kù)中定義了動(dòng)作,則可以使用./path-to-action-file引用該./path-to-action-file 。 在這種情況下,我們需要訪問(wèn)存儲(chǔ)庫(kù)中的文件,因此我們還需要使用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.
如果操作是在公共存儲(chǔ)庫(kù)中定義的,則可以使用特定的提交,發(fā)行版或分支來(lái)引用該操作。
從同一存儲(chǔ)庫(kù)運(yùn)行操作 (Running an action from the same repository)
# .github/workflows/use-action-same-repo.ymlname: Action from the same repoon: pushjobs:
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'
從公共存儲(chǔ)庫(kù)運(yùn)行操作 (Running an action from a public repository)
# .github/workflows/use-action-public-repo.ymlname: Action from a public repositoryon: pushjobs:
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.
那就是我們的旅行。 您還可以在工作流中使用變量和機(jī)密 , 緩存依賴項(xiàng)以加快它們的速度,并連接數(shù)據(jù)庫(kù)和服務(wù)容器以管理工作流工具。
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.
在本指南中,我們沒(méi)有使用CI的主要優(yōu)點(diǎn)之一,它是經(jīng)常測(cè)試,因此我們可以在過(guò)程的早期發(fā)現(xiàn)bug。 但是我認(rèn)為在執(zhí)行操作之前先了解操作和工作流程是如何工作的。 我希望現(xiàn)在開(kāi)始使用CI工具會(huì)更容易,我計(jì)劃在下一個(gè)教程中做到這一點(diǎn)。
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
總結(jié)
以上是生活随笔為你收集整理的GitHub动作简介的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 梦到鸡掉厕所什么预兆吗
- 下一篇: 照顾好自己才能照顾好别人_您必须照顾的5