.github이라는 폴더를 프로젝트 루트 경로에 생성을 해주고, 그 안에 workflows라는 폴더를 만들어 줍니다. 이렇게 해주면 github에서 해당 경로 안에 들어 있는 workflow 파일을 찾아서 github action을 실행할 수 있게 됩니다..yml으로 끝나야 합니다. (github action은 YML 파일로 동작한다고 합니다.)
name: pr_verification # 워크 플로우 이름
on: # 워크 플로우 실행 트리거
pull_request: # PR이 실행될 때
types: [opened, synchronize] # PR이 열렸을 때, 업데이트 됐을 때
jobs: # 작업 정의
check:
runs-on: ubuntu-lastest # 우분투 환경
permissions: #권한 설정
pull-requests: write # PR 쓰기 권한 설정
steps: # 실제로 실행되는 작업 단계
- uses: actions/github-script@v7
with:
script: | # 스크립트 시작
const pull_requst = context.payload.pull_request; #pull_request 객체 가져오기
if (pull_requst === undefined) { # pull_requst가 없는 경우
console.log("The corresponding PR cannot be verified.")
return;
}
const pr_body = pull_requst.body;
if(!pr_body) { //PR 내용이 비어 있다면
//PR에 코멘트 추가
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pull_requst.number,
body: "해당 pull request는 가이드라인을 준수하지 않아 close 처리가 되었습니다. 가이드라인을 준수해서 다시 pull request를 요청해주세요.😉"
})
//해당 PR을 닫습니다.
await github.rest.pulls.update({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pull_requst.number,
state: close
})
}
이렇게 작성해서 테스트용 PR을 요청해봤습니다.

오 돌기 시작한다! 그러나... 너무 오래 걸려서 Details로 들어가보니...

계속 빙글빙글... Check에서 돌기만 하는 나의 github action... 뭔가 이상함을 감지하고 일단 끕니다. ㅠㅠ
ubuntu-lastest → ubuntu-latestawait 키워드를 사용하기 위해서는 스크립트가 async 함수 내에서 실행되어야 하는데, 현재 주어진 코드에서는 async 함수로 감싸져 있지 않았던 문제가 있었습니다. (이런 초보적인 실수를;)
name: pr_verification # 워크 플로우 이름
on: # 워크 플로우 실행 트리거
pull_request: # PR이 실행될 때
types: [opened, synchronize] # PR이 열렸을 때, 업데이트 됐을 때
jobs: # 작업 정의
check:
runs-on: ubuntu-latest # 우분투 환경
permissions: # 권한 설정
pull-requests: write # PR 쓰기 권한 설정
steps: # 실제로 실행되는 작업 단계
- name: Check PR Body and Close if Empty
uses: actions/github-script@v7
with:
script: |
async function run() { // async 함수로 감싸줍니다.
const pull_request = context.payload.pull_request; // pull_request 객체 가져오기
if (pull_request === undefined) { // pull_request가 없는 경우
console.log("The corresponding PR cannot be verified.")
return;
}
const pr_body = pull_request.body;
if (!pr_body) { // PR 내용이 비어 있다면
// PR에 코멘트 추가
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pull_request.number,
body: "해당 pull request는 가이드라인을 준수하지 않아 close 처리가 되었습니다. 가이드라인을 준수해서 다시 pull request를 요청해주세요.😉"
});
// 해당 PR을 닫습니다.
await github.rest.pulls.update({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pull_request.number,
state: "closed"
});
}
}
run(); // 함수 실행
수정하고 나서 보니 계속 check에서만 돌았던 이유가 runs-on에서 작성했던 환경에서 난 오타 때문에 어떤 환경인지 찾지를 못했던 것 같습니다. 😵💫
그러나 저 오타가 멀쩡했었어도 아마 async 함수로 감싸지 않은 문제점과 run() 이라는 함수 호출 부분이 빠져 있었다면 또 error를 뿜었을 거 같긴 합니다...
이렇게 YML 파일을 다시 수정하고 다시 시도!
일부러 PR 내용을 전부 비우고 PR 요청을 했습니다.
PR 내용이 비었다는 가정 하에 close가 되어야 하기 때문입니다.

엇 다 잘 된 건가...?

새로고침해 보니 코멘트도 잘 달리고 훌륭하게 PR이 close 되었습니다. 👏

PR 내용을 채우고 재요청 해보니 닫히지 않고 open 상태로 되어 있는 걸 확인할 수 있었습니다. 👏 👏