GitHub行动生活

在我看来,整理一篇名为“ GitHub Action的生活”的文章会很有趣。 在Google的入门培训中,您经历了“请求的生活”,而我拥有我最喜欢的元素之一。 因此,我对GitHub Action使用了类似的方法。


对于那些在坦克上的人来说,Actions是去年Universe推出的GitHub功能。 想要测试版吗? 去这里


一般的想法是一个具有脚本支持的GitHub,但是我不会受垃圾和解释的困扰。 最好引导您完成操作开始时发生的事件。


问题


这是典型的工作流程


  • 我创建对存储库的拉取请求。
  • 拉取请求被吸收。
  • 树枝一直保存到最后,让我吃掉了喜欢清洁和秩序的那部分大脑。


剩下的分支是我的痛苦,集中精力。 尽管问题很普遍,所以让我们创建一个在吸收拉取请求后删除分支的操作​​。


Mnogabukaf? 动作的所有代码都在这里


工作流程文件


如果需要-通过用户界面创建操作,但如果需要-用笔编写工作流文件。 在本文中,我仅使用一个文件。


这就是它的外观,我将在文件的注释中解释所有这些含义。 它位于您存储库中的.github/main.workflow中。


 workflow "on pull request merge, delete the branch" { ## On pull_request defines that whenever a pull request event is fired this ## workflow will be run. on = "pull_request" ## What is the ending action (or set of actions) that we are running. ## Since we can set what actions "need" in our definition of an action, ## we only care about the last actions run here. resolves = ["branch cleanup"] } ## This is our action, you can have more than one but we just have this one for ## our example. ## I named it branch cleanup, and since it is our last action run it matches ## the name in the resolves section above. action "branch cleanup" { ## Uses defines what we are running, you can point to a repository like below ## OR you can define a docker image. uses = "jessfraz/branch-cleanup-action@master" ## We need a github token so that when we call the github api from our ## scripts in the above repository we can authenticate and have permission ## to delete a branch. secrets = ["GITHUB_TOKEN"] } 

大事记


因此,由于该文章被称为“行动的一生”,所以我们从模糊主义开始。 所有动作都是通过GitHub事件触发的。 支持的事件列表在这里


在上方,我们选择了pull_request事件。 它在分配,未分配,标记,未选中,打开,编辑,关闭,重新打开,同步,请求拉取请求或删除拉取请求的拉取请求时开始。


好的,我们开始了这个活动,然后...


拉取请求有问题...

这是GitHub:“该死的煎饼,请求请求有问题! 我无法解决所有问题!”


查看工作流文件(见上文),GitHub说:“我现在将启动工作流以吸收拉取请求,并删除分支。”


这会导致什么? 哦,“打扫树枝”。 让我简化清理分支的必要步骤(在这种情况下,没有任何步骤),然后按顺序/并行运行它们,以到达“清理分支”。


动作片


GitHub在这里声明:“伙计们,我必须在这里开始“分支清理”。 让我弄清楚。”


这使我们回到文件的“ uses部分。 jessfraz/branch-cleanup-action@master存储库: jessfraz/branch-cleanup-action@master


该存储库包含Dockerfile。 它定义了执行操作的环境。


Docker文件


看一看,我将尝试解释评论中的所有内容。


 ## FROM defines what Docker image we are starting at. A docker image is a bunch ## of files combined in a tarball. ## This image is all the files we need for an Alpine OS environment. FROM alpine:latest ## This label defines our action name, we could have named it butts but ## I decided to be an adult. LABEL "com.github.actions.name"="Branch Cleanup" ## This label defines the description for our action. LABEL "com.github.actions.description"="Delete the branch after a pull request has been merged" ## We can pick from a variety of icons for our action. ## The list of icons is here: https://developer.github.com/actions/creating-github-actions/creating-a-docker-container/#supported-feather-icons LABEL "com.github.actions.icon"="activity" ## This is the color for the action icon that shows up in the UI when it's run. LABEL "com.github.actions.color"="red" ## These are the packages we are installing. Since I just wrote a shitty bash ## script for our Action we don't really need all that much. We need bash, ## CA certificates and curl so we can send a request to the GitHub API ## and jq so I can easily muck with JSON from bash. RUN apk add --no-cache \ bash \ ca-certificates \ curl \ jq ## Now I am going to copy my shitty bash script into the image. COPY cleanup-pr-branch /usr/bin/cleanup-pr-branch ## The cmd for the container defines what arguments should be executed when ## it is run. ## We are just going to call back to my shitty script. CMD ["cleanup-pr-branch"] 

剧本


以下是我正在运行的试用脚本的内容。


 #!/bin/bash set -e set -o pipefail # This is populated by our secret from the Workflow file. if [[ -z "$GITHUB_TOKEN" ]]; then echo "Set the GITHUB_TOKEN env variable." exit 1 fi # This one is populated by GitHub for free :) if [[ -z "$GITHUB_REPOSITORY" ]]; then echo "Set the GITHUB_REPOSITORY env variable." exit 1 fi URI=https://api.github.com API_VERSION=v3 API_HEADER="Accept: application/vnd.github.${API_VERSION}+json" AUTH_HEADER="Authorization: token ${GITHUB_TOKEN}" main(){ # In every runtime environment for an Action you have the GITHUB_EVENT_PATH # populated. This file holds the JSON data for the event that was triggered. # From that we can get the status of the pull request and if it was merged. # In this case we only care if it was closed and it was merged. action=$(jq --raw-output .action "$GITHUB_EVENT_PATH") merged=$(jq --raw-output .pull_request.merged "$GITHUB_EVENT_PATH") echo "DEBUG -> action: $action merged: $merged" if [[ "$action" == "closed" ]] && [[ "$merged" == "true" ]]; then # We only care about the closed event and if it was merged. # If so, delete the branch. ref=$(jq --raw-output .pull_request.head.ref "$GITHUB_EVENT_PATH") owner=$(jq --raw-output .pull_request.head.repo.owner.login "$GITHUB_EVENT_PATH") repo=$(jq --raw-output .pull_request.head.repo.name "$GITHUB_EVENT_PATH") default_branch=$( curl -XGET -sSL \ -H "${AUTH_HEADER}" \ -H "${API_HEADER}" \ "${URI}/repos/${owner}/${repo}" | jq .default_branch ) if [[ "$ref" == "$default_branch" ]]; then # Never delete the default branch. echo "Will not delete default branch (${default_branch}) for ${owner}/${repo}, exiting." exit 0 fi echo "Deleting branch ref $ref for owner ${owner}/${repo}..." curl -XDELETE -sSL \ -H "${AUTH_HEADER}" \ -H "${API_HEADER}" \ "${URI}/repos/${owner}/${repo}/git/refs/heads/${ref}" echo "Branch delete success!" fi } main "$@" 

因此,到目前为止,GitHub已在运行时执行了脚本。
GitHub将向用户界面报告操作状态,您可以在“操作”选项卡中看到它。


希望这可以使人们清楚地了解如何在GitHub Actions中实现流程。 我迫不及待想知道你的经历。

Source: https://habr.com/ru/post/zh-CN438248/


All Articles