github 自动构建
In recent months, GitHub has launched many tools for automation.
最近几个月,GitHub启动了许多自动化工具。
For example, Dependabot (through acquisition), an automated dependency management tool, GitHub Action, GitHub’s native CI/CD platform, and code scanning, an automated tool for vulnerability discovery.
例如,Dependabot(通过收购)(一种自动化的依赖性管理工具),GitHub Action(一种GitHub的本机CI / CD平台)和代码扫描(一种用于漏洞发现的自动化工具)。
This is great for independent open-source contributors because most of us can’t afford a DevOps team to maintain the side projects.
这对于独立的开源贡献者非常有用,因为我们大多数人负担不起DevOps团队来维护副项目。
However, every project comes in different shapes, so the generic tools can only partially cover the workflow.
但是,每个项目都有不同的形状,因此通用工具只能部分覆盖工作流程。
Luckily, we developers won’t let the stock solution limit us.
幸运的是,我们开发人员不会让库存解决方案限制我们。
If there is no automation that fits the requirements, then build one.
如果没有满足要求的自动化,则构建一个。
I will demonstrate how to automate your workflow by building a GitHub app with one I built to automate mine.
我将通过构建一个GitHub应用程序来演示如何自动化您的工作流程,该应用程序是由我自己创建的,以实现我的自动化。
Here’s the background story of why I built the app:
这是为什么我构建应用程序的背景故事:
I’m a fan of protected branch schema which is a GitHub feature that lets repository owners define hard pull request merging policies.
我是受保护的分支架构的忠实支持者,它是GitHub的一项功能,可让存储库所有者定义硬合并请求合并策略。
I defined my repositories to only allow pull requests merging when all continuous integration tests have passed and at least one reviewer approves it.
我将我的存储库定义为仅在所有连续集成测试都通过且至少有一个审阅者批准后,才允许合并请求请求。
However, it becomes a pain when developers have to ask other developers to approve trivial/production-neutral changes on experimental projects or internal-facing documentations, so I wanted an app that lets me define what kinds of pull requests should be automatically approved and approves it.
但是,当开发人员不得不要求其他开发人员批准实验项目或内部文档的琐碎/与生产无关的更改时,这变得很痛苦,因此我想要一个应用程序,该应用程序可以让我定义应自动批准和批准哪些类型的拉取请求它。
For example, I can define that any changes that look like playground/${github_username}/** are trivial. After defining this rule, all my changes in playground/tianhaoz95 will be auto-approved given that my GitHub username is tianhaoz95 .
例如,我可以定义任何看起来如playground/${github_username}/**更改都是微不足道的。 定义了此规则后,鉴于我的GitHub用户名是tianhaoz95 ,我对playground/tianhaoz95所有更改都将自动获得批准。
We will be using the automation mention above as an example to go through how you can build your automation.
我们将以上面提到的自动化为例,介绍如何构建自动化。
To start building a GitHub app, we first need to understand the high-level mechanism of a GitHub app.
要开始构建GitHub应用程序,我们首先需要了解GitHub应用程序的高级机制。
GitHub apps listen to a set of repository activities through Webhooks (to read more about Webhooks, see the documentation) and react to the activities with GitHub REST or GraphQL APIs depending on preferences like shown in the following diagram:
GitHub应用程序通过Webhooks监听一组存储库活动(要了解有关Webhooks的更多信息,请参见文档),并根据GitHub REST或GraphQL API对活动做出React,具体取决于下图所示的首选项:
the ApproveMan repository ApproveMan存储库If we fit the app that we want to build into the diagram above, the repository activities will be any pull request creation actions including “opened” and “reopened” and the reaction will be scanning through the modified files and post an approving review if applicable.
如果我们适合要在上图中构建的应用程序,则存储库活动将是任何拉动请求创建操作,包括“已打开”和“重新打开”,并且React将扫描经过修改的文件并发布批准审核(如果适用) 。
Working with vanilla Webhooks can be complicated, but fortunately, a group of awesome people built a framework, Probot, to abstract the mapping from Webhook events to handlers for us.
使用普通的Webhooks可能会很复杂,但是幸运的是,一群很棒的人构建了Probot框架,为我们抽象了从Webhook事件到处理程序的映射。
Here is an overly simplified version of how Probot works:
这是Probot工作方式的过度简化版本:
the ApproveMan repository ApproveMan存储库Probot lets us define a set of events to listen to and a set of handlers that should get executed when those events happen.
Probot让我们定义了一组要侦听的事件以及一组在这些事件发生时应执行的处理程序。
GitHub app works like a normal GitHub account except that it only has the permissions users grant upon installation.
GitHub应用程序的工作方式类似于普通的GitHub帐户,只是它仅具有用户在安装时授予的权限。
All the operation/automation our app performs will be under the name of this GitHub app account.
我们的应用执行的所有操作/自动化都将以该GitHub应用帐户的名称进行。
Probot offers a neat feature: guided GitHub registration which detects if you have already registered a GitHub app and guide you through the registration if not.
Probot提供了一个简洁的功能:引导式GitHub注册,该功能可检测您是否已经注册了GitHub应用程序,如果尚未注册,则会引导您完成注册。
After finishing the app registration, you should see an app in your GitHub developer settings like this:
完成应用程序注册后,您应该在GitHub开发人员设置中看到一个应用程序,如下所示:
the ApproveMan repository ApproveMan存储库As stated above, Probot helps us map Webhook events to handlers, so we need to register the events we want to subscribe to and supply Problot with event handlers:
如上所述,Probot帮助我们将Webhook事件映射到处理程序,因此我们需要注册要订阅的事件并为Problot提供事件处理程序:
import { Application } from "probot"; import { maybeApproveChange } from "./core"; export = (app: Application): void => { app.on( [ "pull_request.opened", "pull_request.reopened", "pull_request.synchronize", ], async (context) => { await maybeApproveChange(context); }, ); };In the code snippet above, pull_request.opened , pull_request.reopened and pull_request.synchronize are the events that we want to subscribe to (they correspond to when a pull request is opened, reopened, and updated as the names suggest).
在上面的代码片段中, pull_request.opened , pull_request.reopened和pull_request.synchronize是我们要订阅的事件(它们对应于打开,重新打开和更新请求的pull_request.opened , pull_request.reopened )。
The function maybeApproveChange are our handler for the event and the input, context is a convenient interface Probot abstracts for us which contain useful information like authentication, scope, and API endpoints extracted from the event payload (all Webhook events contain an event type and a payload. For details, see the documentation).
函数maybeApproveChange是事件和输入的处理程序, context是对我们而言方便的接口Probot抽象,其中包含有用的信息,例如从事件有效内容中提取的身份验证,范围和API端点(所有Webhook事件均包含事件类型和有效内容有关详细信息,请参见文档)。
A bit more on the function, maybeApproveChange : Since its implementation details are not important for the purpose of this post, I will only briefly describe it simply to avoid confusion. It scans the files listed in the pull request that initiated the Webhook event and approves the pull request if all files are trivial (all performed with APIs in context ).
关于功能的更多信息, maybeApproveChange :由于就本文而言,其实现细节并不重要,因此我仅简要地描述一下它以避免混淆。 它会扫描发起Webhook事件的请求请求中列出的文件,如果所有文件都是微不足道的(均在context使用API执行),则批准该请求。
Putting the pieces together, the code snippet above will scan every new pull request and approve it if all the files it modifies are trivial.
将各个部分放在一起,上面的代码段将扫描每个新的拉取请求,如果它修改的所有文件都很琐碎,则将其批准。
Since the post is not on how to use GitHub APIs, I will use the “approving pull request” action as an example to demonstrate how our GitHub app can interact with GitHub repositories:
由于该文章不是关于如何使用GitHub API的,因此我将以“批准请求请求”操作为例来说明我们的GitHub应用如何与GitHub存储库进行交互:
const approveChange = async (context: Context): Promise<void> => { const req = context.repo({ "event": "APPROVE" as ReviewEvent, "pull_number": context.payload.pull_request.number, }); await context.github.pulls.createReview(req); };In the example snippet above, we can use the context interface Probot generates for us to access the underlying GitHub API and utilize it to create an approval review with createReview .
在上面的示例代码段中,我们可以使用Probot为我们生成的context接口访问底层GitHub API,并利用它与createReview一起创建批准审查。
If you are curious about how this is related to the last step, this function is used by maybeApproveChange to approve when applicable.
如果您对这与上一步有什么关系感到好奇,则maybeApproveChange适用时此函数可能由maybeApproveChange批准。
The simplest way to deploy a GitHub app is through Heroku. Linking the repository to a Heroku project will do the trick and anything checked into the main branch will be deployed:
部署GitHub应用程序的最简单方法是通过Heroku。 将存储库链接到Heroku项目将达到目的,并且将部署到主分支中的所有内容:
the ApproveMan repository ApproveMan存储库Alternatively, you can deploy the app on any platform that supports running node servers with npm run build and npm start.
另外,您可以通过npm run build和npm start在支持运行节点服务器的任何平台上部署该应用程序。
After installing the app into one of the repositories, when I open an eligible pull request, the app will receive the Webhook event and automatically approve it like this:
将应用程序安装到一个存储库中之后,当我打开一个合格的请求请求时,该应用程序将收到Webhook事件并自动批准,如下所示:
the ApproveMan repository ApproveMan存储库Now we have a workflow for developers to do fast iteration on experimental projects and also for new contributors to get familiar with the repository without bothering other developers!
现在,我们有了一个工作流,供开发人员在实验项目上进行快速迭代,也供新的贡献者熟悉存储库而无需打扰其他开发人员!
Thanks for reading and happy hacking with your next automation!
感谢您的阅读和对您下一个自动化技术的满意学习!
If you are interested in the app I used for the demonstration (since it’s kind of generic), it is available in the GitHub Marketplace:
如果您对我用于演示的应用程序感兴趣(因为它是通用的),则可以在GitHub Marketplace中找到它:
The repository for this app is here:
该应用程序的存储库位于:
Thanks for being a part of our community! Subscribe to our YouTube channel or join the Skilled.dev coding interview course.
感谢您加入我们的社区! 订阅我们的YouTube频道或参加Skilled.dev编码面试课程。
翻译自: https://levelup.gitconnected.com/howto-build-a-github-app-to-streamline-your-workflow-edc88d0bb99e
github 自动构建
相关资源:jdk-8u281-windows-x64.exe