๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ–ฅ๏ธ ๋ฐฑ์—”๋“œ/CICD

[CICD] Github Actions์˜ event

by OR15A 2024. 3. 21.

 

Github Actions์˜ event

 

1. push
  • ๋ ˆํŒŒ์ง€ํ† ๋ฆฌ / .github / workflows / push.yaml ํŒŒ์ผ ์ž‘์„ฑํ•˜๊ธฐ
  • push.yaml
    • name : ์•ก์…˜ ์ฐฝ์—์„œ ํ‘œํ˜„ ๋  ์‹œ๊ฐํ™” ์ด๋ฆ„
    • on : ์›Œํฌํ”Œ๋กœ์šฐ ํŠธ๋ฆฌ๊ฑฐ ํ•  ์ด๋ฒคํŠธ. ํŠธ๋ฆฌ๊ฑฐ ํƒ€์ž…
    • jobs : ์‹คํ–‰์‹œํ‚ฌ job์˜ ์ง‘ํ•ฉ. ํ•„์ˆ˜
    • custom-job-name : job์ด๋ฆ„์€ ์ž์œ 
    • runs on : job์„ ์‹คํ–‰์‹œํ‚ฌ runner ์ง€์ •ํ•˜๊ธฐ
    • steps : step์˜ ์ง‘ํ•ฉ
    • - name : job์ด ์‹คํ–‰์‹œํ‚ฌ ์Šคํƒญ ์ •์˜ํ•˜๊ธฐ. ์Šคํƒญ๋งˆ๋‹ค ์ด๋ฆ„์„ ๋ช…์‹œ์ ์œผ๋กœ ์ž‘์„ฑ/์ƒ๋žต ๊ฐ€๋Šฅ
    • run : step์—์„œ ์‹คํ–‰ํ•  ๋‚ด์šฉ. |๋ฅผ ์ด์šฉํ•˜๋ฉด ๋ฉ€ํ‹ฐ๋ผ์ธ ์ปค๋งจ๋“œ ์ž‘์„ฑ ๊ฐ€๋Šฅ
name: push-workflow
on: push

jobs:
  push-job:
    runs-on: ubuntu-latest
    steps:
    - name: step1
      run: echo hello hello hello world 1
    - name: step2
      run: |
        echo hello world 2
        echo github action first-day

Actions

 

 

2. pull request
  • ๋ณ€๊ฒฝ์‚ฌํ•ญ์„ ์ฝ”๋“œ ์ €์žฅ์†Œ์— ํ•ฉ๋ณ‘์‹œํ‚ฌ ๋•Œ ํ•˜์šฉํ•˜๋Š” pull request
  • on : pull_request
  • types : ์•กํ‹ฐ๋น„ํ‹ฐ ํƒ€์ž…์œผ๋กœ ์ข€ ๋” ์„ธ๋ฐ€ํ•˜๊ฒŒ ์„ค์ • ๊ฐ€๋Šฅ
name: pull-request-workflow
on: 
  pull_request:
    types: [opened]


jobs:
  pull-request-job:
    runs-on: ubuntu-latest
    steps:
    - name: step1
      run: echo hello pull-request world
    - name: step2
      run: |
        echo hello world
        echo github action pull-request

์„ธ๋ถ€์ ์ธ activity ํƒ€์ž…์„ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด default activity type์ธ opened, synchronize, reopened ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•  ๋•Œ๋งˆ๋‹ค GitHub Action์ด ํŠธ๋ฆฌ๊ฑฐ ๋จ

ํ’€ ๋ฆฌํ€˜์ŠคํŠธ๊ฐ€ open๋  ๋•Œ๋งŒ ๊นƒํ—ˆ๋ธŒ ์•ก์…˜ ํŠธ๋ฆฌ๊ฑฐ๋ฅผ ์›ํ•˜๋ฉด ๋ช…์‹œ์ ์œผ๋กœ ์•กํ‹ฐ๋น„ํ‹ฐ ํƒ€์ž…์„ ์ง€์ •ํ•ด์ฃผ๊ธฐ

์ด๋ฒคํŠธ๋ฅผ ์„ธ๋ฐ€ํ•˜๊ฒŒ ์ง€์ •ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” on - ์ด๋ฒคํŠธ์— types ํ‚ค์›Œ๋“œ์™€ ํƒ€์ž…์„ ๋ช…์‹œํ•ด์ค˜์•ผํ•จ

 

 

 

 

3. issue
  • ๋ฒ„๊ทธ, ๊ธฐ๋Šฅ ์š”์ฒญ, ์งˆ๋ฌธ, ๋…ผ์˜ ๋“ฑ ์ถ”์  ๋ฐ ๊ด€๋ฆฌ
  • ํ”„๋กœ์ ํŠธ์˜ ๋ฒ„๊ทธ๋‚˜ ๊ธฐ๋Šฅ ๊ฐœ๋ฐœ ๋“ฑ์˜ ์ง„ํ–‰ ์ƒํ™ฉ ํŒŒ์•… ๊ฐ€๋Šฅํ•จ
  • ๊นƒํ—ˆ๋ธŒ ์ƒ๋‹จ์˜ Issues ํƒญ์—์„œ ์ƒ์„ฑ ๊ฐ€๋Šฅํ•จ
    • ์ด์Šˆํƒญ์ด ๋ณด์ด์ง€ ์•Š๋Š”๋‹ค๋ฉด ์…‹ํŒ…์—์„œ Features - Issues  ํ™œ์„ฑํ™” ํ•ด์ฃผ๊ธฐ
  • ๋ชจ๋“  ์•กํ‹ฐ๋น„ํ‹ฐ ํƒ€์ž…์— ์˜ํ•ด ํŠธ๋ฆฌ๊ฑฐ๊ฐ€  ๋จ
  • ์ด์Šˆ๊ฐ€ ์˜คํ”ˆ ๋  ๋•Œ๋งŒ ํŠธ๋ฆฌ๊ฑฐ ๋˜๋Š” yaml ์˜ˆ์‹œ
name: issue-workflow
on:
  issues:
    types: [opened]

jobs:
  issue-job:
    runs-on: ubuntu-latest
    steps:
    - name: step1
      run: echo hello issue-world
    - name: step2
      run: |
        echo hello world
        echo github action of issue-workflow

issue ๋งŒ๋“ค๊ธฐ
opened ์•ก์…˜์— ์˜ํ•œ ์ด๋ฒคํŠธ ํŠธ๋ฆฌ๊ฑฐ

  • ์ด์Šˆ ์ด๋ฒคํŠธ๋Š” ์˜ค์ง ๋””ํดํŠธ ๋ธŒ๋žœ์น˜์—์„œ๋งŒ ํŠธ๋ฆฌ๊ฑฐ๋จ
    • ๊ทธ ํŒŒ์ผ์ด ๋””ํดํŠธ ๋ธŒ๋žœ์น˜์— ์กด์žฌํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ

 

 

 

 

4. issue comment
  • issue ์ด๋ฒคํŠธ์— ์ฝ”๋ฉ˜ํŠธ๋ฅผ ๋‹ค๋Š” ๊ฒƒ
  • pull request ์ด๋ฒคํŠธ์— ์ฝ”๋ฉ˜ํŠธ๋ฅผ ๋‹ค๋Š” ๊ฒƒ
  • ์ด์Šˆ ์ด๋ฒคํŠธ์ฒ˜๋Ÿผ ๋””ํดํŠธ ๋ธŒ๋žœ์น˜์—์„œ๋งŒ ๋™์ž‘
  • ๋ชจ๋“  ์•กํ‹ฐ๋น„ํ‹ฐ ํƒ€์ž…์— ๋Œ€ํ•ด์„œ ํŠธ๋ฆฌ๊ฑฐ ๋จ
  • if : ๊นƒ ํ—ˆ๋ธŒ ์ปจํ…์ŠคํŠธ ์‚ฌ์šฉ (๋‚˜์ค‘์— ๋ณด์ถฉ์„ค๋ช…), job์ด ์‹คํ–‰๋˜๊ธฐ ์ „์— ์กฐ๊ฑด์„ ๊ฑธ ์ˆ˜ ์žˆ์Œ
  • ์˜ˆ์‹œ yaml
    • job_1 : pull request์— ์ฝ”๋ฉ˜ํŠธ๋ฅผ ๋‹ฌ ๋•Œ ์‹คํ–‰๋จ / job_2 : issue์— ์ฝ”๋ฉ˜ํŠธ๋ฅผ ๋‹ฌ ๋•Œ ์‹คํ–‰๋จ
name: issue-comment-workflow
on: issue_comment

jobs:
  pr-comment:
    if: ${{ github.event.issue.pull_request }}
    runs-on: ubuntu-latest
    steps:
    - name: pr comment
      run: echo ${{ github.event.issue.pull_request }}


  issue-comment:
    if: ${{ !github.event.issue.pull_request }}
    runs-on: ubuntu-latest
    steps:
    - name: issue comment
      run: echo ${{ github.event.issue.pull_request }}

์กฐ๊ฑด๋ถ€ ์‹คํ–‰
์กฐ๊ฑด๋ถ€ ์‹คํ–‰

 

 

 

 

 

5. schedule
  • ํŠน์ • ์‹œ๊ฐ„์— Github Workflow๋ฅผ ์‹คํ–‰ํ•˜๋Š” 
  • cron ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•จ. ํŠธ๋ฆฌ๊ทธ ๋  ์‹œ๊ฐ„์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Œ

cron

  • ์Šค์ผ€์ค„ ์ด๋ฒคํŠธ๋Š” ๋””ํดํŠธ ๋ธŒ๋žœ์น˜์— GitHub Action Workflow๊ฐ€ ์žˆ์–ด์•ผ๋งŒ ๋™์ž‘ํ•จ
  • ๊ทธ๋Ÿฌ๋‚˜ ์ง€์ •ํ•œ ์‹œ๊ฐ„์— ์ •ํ™•ํžˆ ๋™์ž‘ํ•˜์ง€ ์•Š์„ ๋•Œ๊ฐ€ ์žˆ์Œ
    • Github Actions ์‚ฌ์šฉ์— ๋Œ€ํ•œ ๋†’์€ ๋ถ€ํ•˜๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ ๋”œ๋ ˆ์ด ๋˜๊ธฐ๋„ ํ•˜๊ฑฐ๋‚˜, ์•„์˜ˆ ์‹คํ–‰๋˜์ง€ ์•Š๊ธฐ๋„ ํ•จ
    • 5๋ถ„ ๊ฐ„๊ฒฉ, 30๋ถ„ ๊ฐ„๊ฒฉ์˜ ์ž‘์—…์€ ๋“œ๋ž๋  ๊ฐ€๋Šฅ์„ฑ ์žˆ์Œ
    • ์ •ํ•ด์ง„ ์‹œ๊ฐ„์— ๊ผญ ์‹คํ–‰๋˜์–ด์•ผ ํ•˜๋Š” ์ž‘์—…์ธ ๊ฒฝ์šฐ ๊นƒํ—™์•ก์…˜ ์Šค์ผ€์ค„๋ง ์‚ฌ์šฉ X

 

 

 

6. workflow_dispatch
  • ์‚ฌ์šฉ์ž๊ฐ€ ์›ํ•  ๋•Œ ์ˆ˜๋™์œผ๋กœ ํŠธ๋ฆฌ๊ฑฐํ•  ์ˆ˜ ์žˆ
  • ๋””ํดํŠธ ๋ธŒ๋žœ์น˜์—์„œ๋งŒ ๋™์ž‘ํ•จ
  • ์ธํ’‹๊ฐ’์„ ๋„ฃ์œผ๋ฉด ๊นƒํ—™์•ก์…˜์—์„œ ๋ฐ›์•„์˜ฌ ์ˆ˜ ์žˆ์Œ
  • ์ธํ’‹๊ฐ’์„ ์ •์˜ํ•˜๋ ค๋ฉด ์ด๋ฒคํŠธ์—์„œ inputs ํ‚ค์›Œ๋“œ ์‚ฌ์šฉํ•˜๊ธฐ
  • ์ธํ’‹ ๋ฐ์ดํ„ฐ ํƒ€์ž…
    • String, number, boolean, choice

์ธํ’‹ ๋ฐ์ดํ„ฐ ํƒ€์ž…

name: workflow_dispatch
on: 
  workflow_dispatch:
    inputs:
      name:
        description: 'set name'
        required: true
        default: 'github-actions'
        type: string
      environment:
        description: 'set env'
        required: true
        default: 'dev'
        type: choice
        options:
          - dev
          - qa
          - prod  

jobs: 
  workflow-dispatch-job:
    runs-on: ubuntu-latest
    steps:
    - name: step1
      run: echo hello world
    - name: step2
      run: |
        echo hello world
        echo github action
    - name: echo input
      run: |
        echo ${{ inputs.name }}
        echo ${{ inputs.environment }}

Run workflow
๊ฐ’ ํ™•

 

 

๋‹ค์–‘ํ•œ ์ด๋ฒคํŠธ๋กœ ํ•˜๋‚˜์˜ ์›Œํฌํ”Œ๋กœ์šฐ ํŠธ๋ฆฌ๊ฑฐ ํ•˜๊ธฐ
name: multiple-event-workflow
on:
  push: {} # push ์ด๋ฒคํŠธ์— ๋Œ€ํ•œ ์„ธ๋ถ€ ์„ค์ •์„ ๋ช…์‹œ์ ์œผ๋กœ ๋น„์›Œ๋‘๊ธฐ
  issues: 
    types: [opened] # issues ์ด๋ฒคํŠธ์— ๋Œ€ํ•ด "opened" ํƒ€์ž…๋งŒ ๋ฐ˜์‘ํ•˜๋„๋ก ์„ค์ •
  workflow_dispatch: # ์ˆ˜๋™ ํŠธ๋ฆฌ๊ฑฐ๋ฅผ ์œ„ํ•ด workflow_dispatch ์ด๋ฒคํŠธ ์ถ”๊ฐ€

jobs:
  multiple-event-job:
    runs-on: ubuntu-latest
    steps:
    - name: step1
      run: echo hello world
    - name: step2
      run: |
        echo hello world 
        echo github action
  • multipleEvent.yaml ํŒŒ์ผ push๋กœ ์ธํ•ด ๋ฐ”๋กœ ์•ก์…˜ ๋ฐœ์ƒํ•จ

์•ก์…˜ ๋ฐœ์ƒ

 

 

 

 

needs
  • ํ•˜๋‚˜์˜ ์›Œํฌํ”Œ๋กœ์šฐ ์•ˆ์—์„œ ์—ฌ๋Ÿฌ ๊ฐœ์˜ job์„ ์ •์˜ํ•ด์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ
  • ์—ฌ๋Ÿฌ ๊ฐœ์˜ job์„ ์ •์˜ ์‹œ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ณ‘๋ ฌ ์‹คํ–‰
  • job ์‚ฌ์ด์˜ ์ข…์†์„ฑ์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ needs ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉ
  • job1 -> job2 / job3 -> job4 ์ข…์†์„ฑ์„ ๊ฐ€์ง„ needs.yaml ์˜ˆ์‹œ ์ž‘์„ฑ
  • exit 1 : ๊ฐ•์ œ ์‹คํŒจ ์‹œํ‚ค๊ธฐ
name: needs
on: push

jobs:
  job1:
    runs-on: ubuntu-latest
    steps:
    - name: echo
      run: echo "job1 done"

  job2:
    runs-on: ubuntu-latest
    needs: [job1]
    steps:
    - name: echo
      run: echo "job2 done"

  job3:
    runs-on: ubuntu-latest
    steps:
    - name: echo
      run: |
        echo "job3 failed"
        exit 1  
        
  job4:
    runs-on: ubuntu-latest
    needs: [job3]
    steps:
    - name: echo
      run: echo "job4 done"

needs ์˜ˆ์ œ ๊ฒฐ๊ณผ

  • ๊นƒํ—ˆ๋ธŒ ์•ก์…˜์—์„œ๋Š” ์ข…์†์„ฑ ์žˆ๋Š” job์ด ์‹คํŒจํ•˜๋”๋ผ๋„ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•จ (if condition)

 

 

 

re-run
  • ๊ณผ๊ฑฐ์— ์‹คํ–‰๋œ ์›Œํฌํ”Œ๋กœ์šฐ๋ฅผ ์žฌ์‹คํ–‰ํ•จ
  • ์„ฑ๊ณต, ์‹คํŒจ ์—ฌ๋ถ€์™€ ์ƒ๊ด€ ์—†์ด ์žฌ์‹คํ–‰ ๊ฐ€๋Šฅํ•จ
  • ํŠธ๋ฆฌ๊ฑฐ๋œ ๊ทธ ์‹œ์ ์„ ๋‹ค์‹œ ์‹คํ–‰
  • ๊ณผ๊ฑฐ์— ์‹คํ–‰๋œ ์›Œํฌํ”Œ๋กœ์šฐ๋ฅผ re-runํ•ด๋„ ์ˆ˜์ •ํ•œ ๋‚ด์šฉ(์›Œํฌํ”Œ๋กœ์šฐ ์‹คํ–‰ ํ›„ ์ˆ˜์ •๋œ ๋‚ด์šฉ)์€ ๋ฐ˜์˜๋˜์ง€ ์•Š์Œ