गिटहब एक्शन लाइफ

मुझे ऐसा लग रहा था कि "लाइफ ऑफ गिटहब एक्शन" नामक पोस्ट को गेस करना मजेदार होगा। Google के परिचयात्मक प्रशिक्षण में, आपको "जीवन का अनुरोध" के माध्यम से नेतृत्व किया जाता है, और मेरे पास मेरे पसंदीदा तत्वों में से एक था। इसलिए, मैं गिटहब एक्शन के लिए एक समान दृष्टिकोण का उपयोग करता हूं।


टैंक में रहने वालों के लिए, एक्ट्स पिछले साल यूनिवर्स में लॉन्च किया गया एक 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


इस पर एक नज़र डालें, और मैं टिप्पणियों में सब कुछ समझाने की कोशिश करूंगा।


 ## 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 क्रियाओं में प्रक्रियाओं को कैसे लागू किया जाता है। मैं यह देखने के लिए इंतजार नहीं कर सकता कि आपके साथ क्या होता है।

Source: https://habr.com/ru/post/hi438248/


All Articles