깃허브 액션 - cron 및 슬랙 메시지 전송

00_8_3·2023년 5월 17일
0

github

목록 보기
1/1

cron

on: 
  schedule:
    - cron: '*/5 * * * *'

그런데 제 시간에 실행되지 않는 경우가 많다.

https://yceffort.kr/2020/07/cron-job-with-github-actions
https://yceffort.kr/2021/01/from-github-workflow-to-firebase-functions

  • 특정 브랜치에서 cron push한다고 해서 cron 실행 불가.

main 브랜치에 cron 파일이 생성되어야 cron이 실행이 된다.

가장 최신 tag 가져오기

  • steps.<id\>.outputs.tag 사용해서 checkout해서 사용하면 된다.
- name: Get latest tag
        id: latest_tag
        run: |
          git fetch --depth=1 origin "+refs/tags/*:refs/tags/*"
          latest_commit=$(git rev-list --tags --max-count=1)
          latest_tag=$(git describe --tags "$latest_commit")
          echo "::set-output name=tag::$latest_tag"
      
      - name: Display latest tag
        run: |
          echo "Latest tag is ${{ steps.latest_tag.outputs.tag }}"
      - uses: actions/checkout@v3
        with:
          ref: ${{steps.latest_tag.outputs.tag}}

성공 실패

success() 또는 failure() 사용하면 된다.

특정 step에서의 실패에 따른 슬랙 메시지를 다르게 하고 싶으면

if문을 사용하면 된다.

if: failure() && steps.test.conclusion == 'failure'

여기서 failure는 무조건 작은 따움표로 감싸야 한다. 아니면 에러발생.

특정 steps가 갖는 컨텍스트 : https://docs.github.com/ko/actions/learn-github-actions/contexts

cron.yaml

name: Cron

on: 
  schedule:
    - cron: '*/5 * * * *'

jobs:
  build:

    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Get latest tag
        id: latest_tag
        run: |
          git fetch --depth=1 origin "+refs/tags/*:refs/tags/*"
          latest_commit=$(git rev-list --tags --max-count=1)
          latest_tag=$(git describe --tags "$latest_commit")
          echo "::set-output name=tag::$latest_tag"
      
      - name: Display latest tag
        run: |
          echo "Latest tag is ${{ steps.latest_tag.outputs.tag }}"

      - uses: actions/checkout@v3
        with:
          ref: ${{steps.latest_tag.outputs.tag}}

      - name: Build
        id: build
        run: go build -v ./...

      - name: Test
        id: test
        run: go test -v ./...

      - name: Set up Go
        uses: actions/setup-go@v4
        with:
          go-version: ^1.18

      - name: Slack Notify - Success
        if: success()
        uses: rtCamp/action-slack-notify@v2
        env:
          SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
          SLACK_CHANNEL: github-action-notify-test
          SLACK_COLOR: ${{job.status}}
          SLACK_ICON: https://github.com/github.png
          SLACK_USERNAME: Github Actions
          SLACK_TITLE: 'Job ${{github.job}}: ${{ github.repository }}'
          SLACK_MESSAGE: 'Success'
          SLACK_FOOTER: 'Linked Repo - <${{ github.event.repository.html_url }}|${{ github.repository }}>'

      - name: Slack Notify - Build Failure
        if: failure() && steps.build.conclusion == 'failure'
        uses: rtCamp/action-slack-notify@v2
        env:
          SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
          SLACK_CHANNEL: github-action-notify-test
          SLACK_COLOR: ${{job.status}}
          SLACK_ICON: https://github.com/github.png
          SLACK_USERNAME: Github Actions
          SLACK_TITLE: 'Job ${{github.job}}: ${{ github.repository }}'
          SLACK_MESSAGE: 'Build Failed'
          SLACK_FOOTER: 'Linked Repo - <${{ github.event.repository.html_url }}|${{ github.repository }}>'

      - name: Slack Notify - Test Failure
        if: failure() && steps.test.conclusion == 'failure'
        uses: rtCamp/action-slack-notify@v2
        env:
          SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
          SLACK_CHANNEL: github-action-notify-test
          SLACK_COLOR: ${{job.status}}
          SLACK_ICON: https://github.com/github.png
          SLACK_USERNAME: Github Actions
          SLACK_TITLE: 'Job ${{github.job}}: ${{ github.repository }}'
          SLACK_MESSAGE: 'Test Failed'
          SLACK_FOOTER: 'Linked Repo - <${{ github.event.repository.html_url }}|${{ github.repository }}>'

0개의 댓글