
GitHub Actions로 Java Checkstyle 검사 파일 Slack 공유 방법을 찾다가
작성한 워크플로우 스크립트이다.
Slack upload file 기능을 사용해서 공유하는 방법인데,
현재는 GitHub Artifacts로 파일 공유하는 방식이 낫다고 판단해서 사실 아래 스크립트 안 쓴다. 다른 파일 공유할 때 다시 찾게 될 것 같아서 기록한다.
files.getUploadURLExternal
files.completeUploadExternal
Slack API 문서 링크
https://api.slack.com/methods/files.getUploadURLExternal
https://api.slack.com/methods/files.completeUploadExternal
슬랙에 알림까지는 잘 가고, 링크 퍼미션만 조절하면 될 듯하다. (링크 클릭 시 접근이 불가능 했었다.)
name: Java Lint Check
on:
pull_request:
types: [opened, synchronize, reopened] # 처음 생성시점(opened), 커밋 추가/변경 시점(synchronize), 닫힌 PR 재오픈 시점(reopened)
branches:
- main
- dev
jobs:
lint:
runs-on: ubuntu-20.04
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up JDK
uses: actions/setup-java@v3
with:
java-version: '21'
distribution: 'corretto'
- name: Install dependencies
run: ./gradlew build -x test
- name: Run Checkstyle
id: checkstyle
continue-on-error: true
run: ./gradlew checkstyleMain
- name: Send Slack notification on Checkstyle Failure
if: failure()
env:
SLACK_API_TOKEN: ${{ secrets.SLACK_API_TOKEN }}
SLACK_CHANNEL_ID: ${{ secrets.SLACK_CHANNEL_ID }}
run: |
curl -X POST -H 'Content-type: application/json' \
-H "Authorization: Bearer $SLACK_API_TOKEN" \
--data '{
"channel": "'"$SLACK_CHANNEL_ID"'",
"text": "❗ Checkstyle 실행에 실패했습니다. 워크플로우 설정 또는 환경 구성을 확인하세요.",
"attachments": [
{
"color": "#ff0000",
"fields": [
{ "title": "Repository", "value": "${{ github.repository }}", "short": true },
{ "title": "Branch", "value": "${{ github.ref }}", "short": true },
{ "title": "Commit Message", "value": "${{ github.event.head_commit.message }}", "short": false },
{ "title": "Actions URL", "value": "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}", "short": false }
]
}
]
}' https://slack.com/api/chat.postMessage
- name: Get Upload URL from Slack
if: success()
id: get_upload_url
env:
SLACK_API_TOKEN: ${{ secrets.SLACK_API_TOKEN }}
run: |
FILE_SIZE=$(stat -c%s "build/reports/checkstyle/goodbite-checkstyle-result.xml")
response=$(curl -s -X POST \
-H "Authorization: Bearer $SLACK_API_TOKEN" \
-H "Content-Type: application/x-www-form-urlencoded" \
--data-urlencode "filename=goodbite-checkstyle-result.xml" \
--data-urlencode "length=$FILE_SIZE" \
https://slack.com/api/files.getUploadURLExternal)
echo "Slack API response: $response"
upload_url=$(echo $response | jq -r '.upload_url')
file_id=$(echo $response | jq -r '.file_id')
# GitHub Actions 환경 파일을 사용하여 환경 변수 설정
echo "UPLOAD_URL=$upload_url" >> $GITHUB_ENV
echo "FILE_ID=$file_id" >> $GITHUB_ENV
- name: Upload Checkstyle result to Slack URL
if: success()
env:
SLACK_API_TOKEN: ${{ secrets.SLACK_API_TOKEN }}
UPLOAD_URL: ${{ env.UPLOAD_URL }}
run: |
curl -X POST "$UPLOAD_URL" \
-F file=@build/reports/checkstyle/goodbite-checkstyle-result.xml \
-H "Authorization: Bearer $SLACK_API_TOKEN"
- name: Complete Upload in Slack
if: success()
env:
SLACK_API_TOKEN: ${{ secrets.SLACK_API_TOKEN }}
FILE_ID: ${{ env.FILE_ID }}
run: |
curl -s -X POST \
-H "Authorization: Bearer $SLACK_API_TOKEN" \
-H "Content-Type: application/x-www-form-urlencoded" \
--data-urlencode "files=[{\"id\":\"$FILE_ID\", \"title\":\"goodbite-checkstyle-result.xml\"}]" \
https://slack.com/api/files.completeUploadExternal
- name: Send Slack notification with success message
if: success()
env:
SLACK_API_TOKEN: ${{ secrets.SLACK_API_TOKEN }}
SLACK_CHANNEL_ID: ${{ secrets.SLACK_CHANNEL_ID }}
FILE_ID: ${{ env.FILE_ID }}
run: |
curl -X POST -H 'Content-type: application/json' \
-H "Authorization: Bearer $SLACK_API_TOKEN" \
--data '{
"channel": "'"$SLACK_CHANNEL_ID"'",
"text": "✅ Checkstyle 실행에 성공하였습니다. 결과 파일을 확인하세요.",
"attachments": [
{
"color": "#36a64f",
"fields": [
{ "title": "Repository", "value": "${{ github.repository }}", "short": true },
{ "title": "Branch", "value": "${{ github.ref }}", "short": true },
{ "title": "Commit Message", "value": "${{ github.event.head_commit.message }}", "short": false },
{ "title": "Actions URL", "value": "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}", "short": false }
]
}
],
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "업로드된 파일을 보려면 <https://slack.com/files/${SLACK_CHANNEL_ID}/files/${FILE_ID}|여기>를 클릭하세요."
}
}
]
}' https://slack.com/api/chat.postMessage