github动作基础

它是什么? 而为什么需要它呢? (What is it? And why would you need it?)

You might be familiar with CI tools such as TeamCity, Jenkins, Concourse, Travis CI to mention a few. Well, this time we’ve got a new neighbor in town.

您可能熟悉CI工具,例如TeamCity,Jenkins,Concourse,Travis CI等。 好吧,这次我们在城里有了一个新邻居。

This new neighbor offers new capabilities to automating your workflows: GitHub Actions.

这个新邻居提供了自动化工作流程的新功能: GitHub Actions

GitHub Actions provides different ways to automate development tasks. Some of the things you could do are:

GitHub Actions提供了自动化开发任务的不同方法。 您可以做的一些事情是:

  • Build your applications and run tests on every code check-in

    在每个代码签入中构建您的应用程序并运行测试
  • Deploy your application to your production servers

    将应用程序部署到生产服务器
  • Perform different types of automated tasks when a pull request is created

    创建拉取请求时执行不同类型的自动化任务
  • Monitor the health of your application at a defined time interval

    在定义的时间间隔内监视应用程序的运行状况

These are just a few ideas, but as you can see, possibilities are endless….

这些只是一些想法,但是如您所见,可能性是无限的……。

In this article, I’ll be going over how you create a workflow to build your application.

在本文中,我将介绍如何创建工作流以构建应用程序。

工作流程,事件和一杯咖啡! (Workflows, events, and a cup of coffee!)

The first step will be to create a GitHub workflow file at the root of your project under .github/workflows. You will need to create a YAML file inside this path.

第一步是在项目的根目录.github/workflows下创建一个GitHub工作流文件。 您将需要在此路径内创建一个YAML文件。

Once you’ve created your workflow file, you can go ahead and start by adding a name using the name property.

创建工作流程文件后,可以继续使用name属性添加name

name: my-application

GitHub Actions is not your regular CI tool… it is very powerful as your actions can get triggered by different types of events. I’ll start with the most common one, which is when pushing to a branch.

GitHub Actions不是您常规的CI工具……它非常强大,因为您的操作可以被不同类型的事件触发。 我将从最常见的一个开始,即推送到分支时。

First, start by adding the on property inside your workflow file. Because we are going to want this to trigger once you push to a branch, we'll add the push property.

首先,首先在工作流程文件中添加on属性。 因为一旦推送到分支,我们将希望触发该事件,因此我们将添加push属性。

Next step? Specify what branches you want to trigger this action on. You can provide specific names such as master or provide wildcards such as feature/**. The feature/** wildcard will cover any branches that start with feature/.

下一步? 指定要在其上触发此操作的分支。 您可以提供特定名称(例如master或提供通配符(例如feature/**feature/**通配符将覆盖以feature/开头的所有分支。

name: my-projecton:
push:
branches:
- master
- 'feature/**'

Another event that can trigger this workflow could be when you push to a pull request. In this case, you will end up using the pull_request property instead of push.

另一个触发此工作流程的事件可能是当您推送请求请求时。 在这种情况下,您将最终使用pull_request属性而不是push

name: my-projecton:
pull_request:
branches:
- master
- 'feature/**'

You can create actions when someone comments on an issue, a release is published, an issue is opened and even you can schedule cronjobs. For these and more events you can visit this page.

当有人对问题发表评论,发布版本,打开问题甚至您可以安排cronjob时,您都可以创建操作。 对于这些以及更多事件,您可以访问此页面

So far your workflow has a name and an event, now… there are three more things to learn about… jobs, steps, and runners.

到目前为止,您的工作流程已有一个名称和一个事件,现在……还有三件事需要学习……工作,步骤和执行者。

跑步者和工作 (Runners and jobs)

Image for post
Bruno Nascimento Bruno Nascimento

The next step will be to create what are known as jobs. Jobs contain multiple steps and they are executed inside what is known as a runner.

下一步将是创建所谓的作业。 作业包含多个步骤,它们在称为运行程序的内部执行。

“A runner picks up a job and it runs the job’s actions and reports the progress, logs, and final results back to GitHub.”

“一个跑步者拿起一份工作,并运行该工作的动作,并将进度,日志和最终结果报告回GitHub。”

Now that I’ve gone over some of the required terminologies, let’s go over how we create jobs.

现在,我已经介绍了一些必需的术语,接下来让我们介绍一下如何创建工作。

For you to create a job, you will need to add the job property and provide a name to your job. Once you've done this, you will need to specify what operating system you need. For this, you'll be adding the runs-on property after your job's name. You will now need to specify the operating system and the version. Some valid values here could be ubuntu-latest, ubuntu-18.04, windows-latest or macos-10.15.

要创建作业,您将需要添加job属性并为您的作业提供名称。 完成此操作后,您将需要指定所需的操作系统。 为此,您将在作业名称后添加runs-on属性。 现在,您需要指定操作系统和版本。 这里的一些有效值可能是ubuntu-latestubuntu-18.04windows-latestmacos-10.15

name: my-projecton:
push:
branches:
- master
- 'feature/**'jobs:
my-application:
runs-on: ubuntu-18.04

You must go over the pricing for the different operating systems as they can significantly vary amongst these. You can verify for more information on the prices here.

您必须仔细检查不同操作系统的价格,因为它们之间的差异可能很大。 您可以在此处验证有关价格的更多信息。

下一站:步骤! (Next stop: Steps!)

Image for post
Fabian Fauth Fabian Fauth

A step is an individual command you run on your action. This action could be running tests or building your application. You can add one or more steps to your job. Your first step will be to add the steps array property to your job. You should add a name and an id to your job. The name will be what displays on the UI when it is running the job. The id will help you refer to any output that comes from this step on other steps.

步骤是您对操作执行的单个命令。 该操作可能正在运行测试或构建您的应用程序。 您可以在工作中添加一个或多个步骤。 您的第一步将是添加steps数组属性到您的工作。 您应该在工作中添加nameid 。 名称将是运行作业时在UI上显示的名称。 该ID将帮助您引用其他步骤中来自此步骤的任何输出。

If you add the run property and add a pipe | you will be able to run all your commands on the next lines. In this case, the

如果添加run属性并添加管道| 您将能够在下一行运行所有命令。 在这种情况下,

name: my-projecton:
push:
branches:
- master
- 'feature/**'jobs:
my-application:
runs-on: ubuntu-18.04
steps:
- name: build
id: build
run: |
echo "Building application...!"
sh scripts/build-app.sh

最后一个:检出代码并指定运行时 (The last one: Checking out your code and specifying your runtime)

So far if you’ve followed the steps above, you might not have a working solution. The reason for this is because you need to check out your code. For you to do this, you just need to add another step, called actions/checkout@v2.

到目前为止,如果您已按照上述步骤进行操作,则可能没有可行的解决方案。 这样做的原因是因为您需要签出代码。 为此,您只需要添加另一步骤,称为actions/checkout@v2

- uses: actions/checkout@v2

In addition to this, you are going to need the runtime of the program you are building. Let’s assume your program needs node. In this case, you will need another step after checking out the code where you bring in the version of node you want. Doing this is simple… Just provide a name to the step and use actions/setup-node@v1 and you can specify the node version in the node-version property.

除此之外,您将需要正在构建的程序的运行时。 假设您的程序需要节点。 在这种情况下,在签出引入所需节点版本的代码之后,您将需要执行下一步。 这样做很简单……只需为该步骤提供一个名称,然后使用actions/setup-node@v1 ,即可在node-version属性中指定节点版本。

- name: Node '12.15'
uses: actions/setup-node@v1
with:
node-version: '12.15'

If you were to be working on an application that uses Java, you could have brought in Java using the actions/setup-java@v1 and specifying the version on the java-version attribute.

如果要处理使用Java的应用程序,则可以使用actions/setup-java@v1并在java-version属性上指定版本来引入Java。

- name: Java Version 11
uses: actions/setup-java@v1
with:
java-version: 11

Finally, your workflow file should look like this.

最后,您的工作流程文件应如下所示。

name: my-projecton:
push:
branches:
- master
- 'feature/**'jobs:
my-application:
runs-on: ubuntu-18.04
steps:
- name: Node '12.15'
uses: actions/setup-node@v1
with:
node-version: '12.15'
id: build
run: |
echo "Building application...!"
sh scripts/build-app.sh

By now, you should have your application being built every time you check in code to the master or feature branches for your project. If you get any invalid syntax issues, feel free to use a YAML linter online such as this one. This will help identify any small issues you’ve had related to formatting or spacing.

现在,您应该在每次将代码签入项目的master或feature分支时都构建您的应用程序。 如果遇到任何无效的语法问题,请随时在线使用YAML linter,例如this 。 这将帮助您确定与格式或间距有关的任何小问题。

I hope that this article helped provide you an overview of what GitHub Actions is and how to create a very simple workflow to build your application. Enjoy!

我希望本文能帮助您概述GitHub Actions是什么以及如何创建一个非常简单的工作流来构建您的应用程序。 请享用!

翻译自: https://medium.com/dev-genius/github-actions-basics-aa553e04fd5c

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值