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領域的新手,那么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 workflow

    on —觸發工作流的事件

  • runs-on — the machine each job should run

    runs-on —每個作業應運行的機器

  • jobs — the jobs that make the workflow

    jobs -構成工作流程的工作

  • steps —the tasks each job should run

    steps -每個作業應執行的任務

  • run —the command the step should run

    run -該步驟應運行的命令

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網站上的“操作”選項卡,您將看到新的工作流程,檢查所有運行并查看輸出:

Image for post
The outputs of our fist workflow
拳頭工作流程的輸出

在我們的第一個工作流程中使用動作 (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語法是操作定義的輸入參數的映射。 結果如下:

Image for post
The result of the ASCII art Action
ASCII art Action的結果

在工作流程中使用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-artifactdownload-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下載文件:

Image for post
Downloading the artifact
下載工件

創建您的第一個動作 (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.

動作的三個主要組成部分是runsinputsoutputs

  • 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 runtime

    inputs - (可選)輸入參數使您可以指定操作期望在運行時使用的數據

  • outputs (optional) Output parameters allow you to declare data that an action sets

    outputs (可選)輸出參數使您可以聲明操作設置的數據

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.ymlaction.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,一經查實,立即刪除!

相關文章

java returnaddress,JVM之數據類型

《Java虛擬機規范》閱讀筆記-數據類型1.概述Java虛擬機的數據類型可分為兩大類:原始類型(Primitive Types,也稱為基本類型)和引用類型(Reference Types)。Java虛擬機用不同的字節碼指令來操作不同的數據類型[1] 。2.原始類型原始類型是最基本的元素&…

C# matlab

編譯環境:Microsoft Visual Studio 2008版本 9.0.21022.8 RTMMicrosoft .NET Framework版本 3.5已安裝的版本: ProfessionalMicrosoft Visual Basic 2008 91986-031-5000002-60050Microsoft Visual Basic 2008Microsoft Visual C# 2008 91986-031-5000002-60050…

洛谷P3273 [SCOI2011] 棘手的操作 [左偏樹]

題目傳送門 棘手的操作 題目描述 有N個節點,標號從1到N,這N個節點一開始相互不連通。第i個節點的初始權值為a[i],接下來有如下一些操作: U x y: 加一條邊,連接第x個節點和第y個節點A1 x v: 將第x個節點的權值增加vA2 x…

基于容器制作鏡像

一。鏡像基礎 一。基于容器制作鏡像 1. 查看并關聯運行的容器 [ghlocalhost ~]$ docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4da438fc9a8e busybox …

照顧好自己才能照顧好別人_您必須照顧的5個基本數據

照顧好自己才能照顧好別人I am pretty sure that on your data journey you came across some courses, videos, articles, maybe use cases where someone takes some data, builds a classification/regression model, shows you great results, you learn how that model wo…

matlab數字仿真實驗,DVR+備用電源自動投入的MATLAB數字仿真實驗仿真實驗

一、動態電壓恢復器(DVR)的數字仿真實驗動態電壓恢復器(Dynamic Voltage Restorer,DVR)是一種基于電力電子技術的串聯補償裝置,通常安裝在電源與敏感負荷之間,其作用在于:保證電網供電質量,補償供電電網產生的電壓跌落…

c#,xp系統,Matlab6.5

編譯環境:c#,xp系統,Matlab6.5 新建一個窗體項目,添加matlab引用。 然后試了四種方式調用matlab: 第一種 view plaincopy to clipboardprint?MLApp.MLAppClass matlab new MLApp.MLAppClass(); matlab.Visible 1;…

java script 對象

java script 對象 1.創建方式 1)通過字面量的形式創建 例;var stt{x:1,y:2,y:3}; 或;var stt{ x:1, y:2, for:3 } 注意關鍵字必須放到引號中間 2)通過new創建對象 例:var new stt(); stt.name 小魚; stt.age 20…

認識數據分析_認識您的最佳探索數據分析新朋友

認識數據分析Visualization often plays a minimal role in the data science and model-building process, yet Tukey, the creator of Exploratory Data Analysis, specifically advocated for the heavy use of visualization to address the limitations of numerical indi…

架構探險筆記10-框架優化之文件上傳

確定文件上傳使用場景 通常情況下,我們可以通過一個form(表單)來上傳文件,就以下面的“創建客戶”為例來說明(對應的文件名是customer_create.jsp),需要提供一個form,并將其enctype屬…

matlab飛行數據仿真,基于MATLAB的飛行仿真

收稿日期: 2005 - 05 - 15   第 23卷  第 06期 計  算  機  仿  真 2006年 06月    文章編號: 1006 - 9348 (2006) 06 - 0057 - 05 基于 MATLAB的飛行仿真 張鐳 ,姜洪洲 ,齊潘國 ,李洪人 (哈爾濱工業大學電液伺服仿真及試驗系統研究所 ,黑龍江 哈爾濱 150001) 摘要:該…

Windows Server 2003 DNS服務安裝篇

導讀-- DNS(Domain Name System,域名系統)是一種組織成層次結構的分布式數據庫,里面包含有從DNS域名到各種數據類型(如IP地址)的映射“貴有恒,何必三更起五更勤;最無益,只怕一日曝十日寒。”前一段時間巴哥因為一些生活瑣事而中止…

正則表達式matlab,正則表達式中一個word的匹配?@MATLAB - 優秀的Free?OS(Linux)版 - 北大未名BBS...

我目前想做的就是判斷一個str是否可以被認為是有效的MATLAB index。最好的方法是直接運行,然后看運行結果或報錯類型,但是我不打算在不知道是什么類型的東西之前運行它,所以可以預先parse一下,簡單判斷是否“長得跟有效的MATLAB i…

arima模型怎么擬合_7個統計測試,用于驗證和幫助擬合ARIMA模型

arima模型怎么擬合什么是ARIMA? (What is ARIMA?) ARIMA models are one of the most classic and most widely used statistical forecasting techniques when dealing with univariate time series. It basically uses the lag values and lagged forecast error…

jQuery禁止Ajax請求緩存

一 現象 get請求在有些瀏覽器中會緩存。瀏覽器不會發送請求,而是使用上次請求獲取到的結果。 post請求不會緩存。每次都會發送請求。 二 解決 jQuery提供了禁止Ajax請求緩存的方法: $.ajax({type: "get",url: "http://www.baidu.com?_&…

python 實例

參考 http://developer.51cto.com/art/201804/570408.htm 轉載于:https://www.cnblogs.com/artesian0526/p/9552510.html

[WPF]ListView點擊列頭排序功能實現

[WPF]ListView點擊列頭排序功能實現 這是一個非常常見的功能,要求也很簡單,在Column Header上顯示一個小三角表示表示現在是在哪個Header上的正序還是倒序就可以了。微軟的MSDN也已經提供了實現方式。微軟的方法中,是通過ColumnHeader Templ…

天池幸福感的數據處理_了解幸福感與數據(第1部分)

天池幸福感的數據處理In these exceptional times, the lockdown left many of us with a lot of time to think. Think about the past and the future. Think about our way of life and our achievements. But most importantly, think about what has been and would be ou…

標線markLine的用法

series: [{markLine: {itemStyle: {normal: { lineStyle: { type: solid, color:#000 },label: { show: true, position:left } }},data: [{name: 平均線,// 支持 average, min, maxtype: average},{name: Y 軸值為 100 的水平線,yAxis: 100},[{// 起點和終點的項會共用一個 na…

php pfm 改端口,羅馬2ESF和PFM 修改建筑 軍團 派系 兵種等等等很多東西的教程

本帖最后由 clueber 于 2013-10-5 12:30 編輯本人是個羅馬死忠加修改黨,恩,所以分享一下自己的修改心得修改工具為ESF1.0.7和PFM3.0.3首先是ESF修改。ESF可以用來改開局設定和存檔,修改開局設定是startpos.esf文件,在存檔在我這里…