https://github.com/marketplace/actions/action-slack
action-slack - GitHub Marketplace
You can notify slack of GitHub Actions
github.com
action/slack을 이용하여 깃허브 액션이 시작되면 slack 메시지를 발송하도록 만들었지만
가장 마지막의 commit message만을 표시하는 문제점이 있었다.
모든 commit message를 보여주기 위해 다음과 같은 코드를 작성했다.
1. checkout을 통해 코드 가져오기
- name: Checkout code
uses: actions/checkout@v3
with:
fetch-depth: 0
fetch-depth: 0 로 설정되어 있어야 모든 커밋 기록을 가져온다.
2. 새롭게 커밋된 메시지 로그 가져오기
- name: Log newly committed messages
env:
BEFORE_REF: ${{ github.event.before }}
AFTER_REF: ${{ github.event.after }}
# \n로 인해서 오류가 발생함
# \n를 ' <- '로 대체하여 모든 comment로 대체
run: |
COMMIT_MSG_STR=$(git log --pretty=format:%s "$BEFORE_REF...$AFTER_REF" |
python3 -c "import sys; print(sys.stdin.read().replace('\n', ' <- '))")
echo "commit_msg_str=$COMMIT_MSG_STR" >> $GITHUB_ENV
Ubuntu-latest에 기본적으로 깔려 있는 요소(link)
github-action에는 python3가 기본적으로 깔려 있기 때문에 해당 문법을 사용할 수 있다.
1. --pretty=format:%s
--pretty=format:%s
깃 로그에서 subject 라인만 가져옴
ex) git log 원본
commit a1b2c3d4
Author: user <user@example.com>
Date: Wed Jul 27 14:53:32 2023 +0900
This is the commit message
commit e5f6g7h8
Author: user <user@example.com>
Date: Wed Jul 27 14:53:32 2023 +0900
This is another commit message
ex) git log --pretty=format:%s
This is the commit message
This is another commit message
2. |
// Unix 계열 시스템에서 standard output을 standard input으로 넣어주는 파이프라인
stdout | stdin
3. python3 -c "import sys; print(sys.stdin.read().replace('\n', ' <- '))")
// 뒤에 따라오는 ""안에 있는 파이썬 커맨드를 실행한다
python3 -c
// sys변수 import
import sys;
// 입력되는 값을 읽고 '\n'을 ' <- '로 변경
print(sys.stdin.read().replace('\n', ' <- '))
그냥 \n이 들어갈 경우 Error: Unable to process file command 'env' successfully 에러가 발생한다.
해당 코드를 통해서 커밋 메시지를 추출해서 변수로 저장할 수 있다.
Reference:
git log --pretty=format:%s "$BEFORE_REF...$AFTER_REF" 출처
https://github.com/orgs/community/discussions/25797#discussioncomment-3249295
'DevOps' 카테고리의 다른 글
AWS SAA C03 덤프 오답 정리 (100 ~ 200) (0) | 2024.01.26 |
---|---|
AWS SAA C03 덤프 오답 정리 (0 ~ 100) (0) | 2024.01.16 |
Docker와 Git Action을 이용한 AWS배포 (3) (0) | 2023.02.21 |
Docker와 Git Action을 이용한 AWS배포 (2) (0) | 2023.02.21 |
Docker와 Git Action을 이용한 AWS배포 (1) (0) | 2023.02.21 |