Bagi saya tampaknya akan menyenangkan untuk mendapatkan posting yang disebut "Life of GitHub Action." Di pelatihan pengantar Google, Anda dipimpin melalui "Life of the Request", dan saya memiliki salah satu elemen favorit saya. Karena itu, saya menggunakan pendekatan serupa untuk GitHub Action.
Bagi mereka yang ada di dalam tangki, Actions adalah fitur GitHub yang diluncurkan di Universe tahun lalu. Ingin penguji beta? Pergi di sini
Gagasan umum adalah GitHub dengan dukungan scripting, tapi saya tidak akan menderita sampah dan menumpahkan penjelasan. Lebih baik mengarahkan Anda melalui peristiwa yang terjadi saat tindakan dimulai.
Masalah
Berikut ini adalah alur kerja yang khas :
- Saya membuat permintaan tarik ke repositori.
- Permintaan tarikan diserap.
- Cabang dipertahankan sampai akhir waktu, memakan saya bagian otak yang menyukai kebersihan dan ketertiban.

Cabang-cabang yang tersisa adalah rasa sakit saya, fokus pada itu. Meskipun masalahnya umum, jadi mari kita membuat tindakan untuk menghapus cabang setelah menyerap permintaan tarikan.
Mnogabukaf? Semua kode untuk tindakan ada di sini .
File alur kerja
Jika Anda ingin - buat tindakan melalui antarmuka pengguna, tetapi jika Anda mau - tulis file alur kerja dengan pena. Pada artikel ini, saya hanya menggunakan file.
Ini adalah tampilannya, dan saya akan menjelaskan apa artinya semua ini di komentar pada file. Itu terletak di .github/main.workflow
di repositori Anda.
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"] }
Acara
Jadi, karena artikel itu disebut "The Life of Action", kita mulai dengan apa yang terjadi untuk obskurantisme. Semua tindakan dipicu melalui acara GitHub. Daftar acara yang didukung ada di sini .
Di atas, kami memilih acara pull_request
. Itu dimulai ketika permintaan tarikan ditetapkan, tidak ditugaskan, ditandai, tidak dicentang, dibuka, diedit, ditutup, dibuka kembali, disinkronkan, diminta untuk meminta permintaan tarikan atau menghapus permintaan tarikan.
Oke, kami memulai acara ini, dan ...
Ada yang salah dengan permintaan tarik ...
Dan inilah GitHub: "Pancake sialan, ada yang salah dengan permintaan tarik! Saya kehilangan semua alat untuk masalah! "
Melihat file alur kerja (lihat di atas), GitHub mengatakan: "Saya sekarang akan memulai alur kerja untuk menyerap permintaan tarikan, dan saya akan menghapus cabang."
Apa yang menyebabkan ini? Oh, "bersihkan rantingnya." Biarkan saya merampingkan langkah-langkah yang diperlukan untuk membersihkan cabang (dalam hal ini, tidak ada) dan jalankan secara berurutan / secara paralel untuk tiba di "cabang bersih".
Aksi
Di sini GitHub menyatakan: "Hai, saya harus memulai" pembersihan cabang "di sini. Biarkan saya mencari tahu. "
Ini membawa kita kembali ke bagian uses
file kita. jessfraz/branch-cleanup-action@master
ke repositori: jessfraz/branch-cleanup-action@master
.
Repositori ini berisi Dockerfile. Ini mendefinisikan lingkungan di mana tindakan kita akan dilakukan.
Dockerfile
Lihatlah, dan saya akan mencoba menjelaskan semuanya dalam komentar.
## 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"]
Skrip
Di bawah ini adalah isi dari skrip uji coba yang saya jalankan.
#!/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 "$@"
Jadi, untuk saat ini, GitHub telah mengeksekusi skrip kami di runtime kami.
GitHub akan melaporkan status tindakan ke antarmuka pengguna, dan Anda dapat melihatnya di tab "Tindakan".
Semoga ini membawa beberapa kejelasan tentang bagaimana proses diimplementasikan dalam Tindakan GitHub. Saya tidak sabar untuk melihat apa yang terjadi dengan Anda.