IaC/CI CD Tool

9. Github Action Steps Context 활용법

Somaz 2024. 11. 10. 00:51
728x90
반응형

Overview

GitHub Actions는 CI/CD를 자동화할 수 있는 강력한 도구로, 각 단계(step) 간의 의존성과 흐름 제어가 매우 중요하다.


이때 핵심적으로 사용되는 것이 바로 steps Context이다. steps Context를 통해 이전 단계의 출력값(output), 실행 결과(outcome), 최종 결론(conclusion) 등에 접근할 수 있으며, 이를 활용하여 조건 분기, 실패 허용, 동적 변수 설정 등 다양한 제어 로직을 구성할 수 있다.

 

 

본 글에서는 steps Context의 기본 구조와 주요 속성(outputs, outcome, conclusion), 그리고 continue-on-error 옵션의 실제 활용법을 예시와 함께 설명한다. 마지막에는 전체 워크플로 코드 예제를 통해 실전 적용 방법을 이해할 수 있도록 구성하였다.

 

 

 

출처 : https://lo-victoria.com/github-actions-101-develop-a-cicd-workflow

 

 

 

 

📅 관련 글

2023.04.27 - [IaC/CI CD Tool] - 1. Github Action이란?

2023.05.22 - [IaC/CI CD Tool] - 2. Github Action (With Syntax)

2023.05.22 - [IaC/CI CD Tool] - 3. Github Action (With Automate Pull Request)

2024.03.12 - [IaC/CI CD Tool] - 4. Github Action (With Matrix Strategy)

2024.03.12 - [IaC/CI CD Tool] - 5. Github Action (With Using jobs in a workflow & Choosing the runner for a job)

2024.03.15 - [IaC/CI CD Tool] - 6. Github Action (With Using Concurrency)

2024.05.28 - [IaC/CI CD Tool] - 7. Github Action Build and Push(with GCP Artifact Registry)

2024.05.28 - [IaC/CI CD Tool] - 7. Github Action Build and Push(with GCP Artifact Registry)

2024.06.20 - [IaC/CI CD Tool] - 8. Github Action Template 생성후 MarketPlace 등록하기

2024.11.10 - [IaC/CI CD Tool] - 9. Github Action Steps Context 활용법

2025.01.22 - [IaC/CI CD Tool] - 10. Github Action Hosted Runner 생성

 

 

 


 

Steps Context란?

GitHub Action에서 steps 컨텍스트를 사용하면 작업 내 step에 대한 정보를 참조할 수 있다. 이 컨텍스트는 동일한 작업 내에서 이전에 실행된 step에 대한 출력, 상태 및 기타 세부 정보에 액세스하는 데 필수적이다. 이를 통해 step은 후속 step에 데이터를 전달하고 종속성을 처리하며 step 결과에 따라 작업의 흐름을 제어할 수 있다.

 

steps 컨텍스트에는 지정되어 있고 이미 실행된 id 가 있는 현재 작업의 단계에 대한 정보가 포함된다.

 

 

steps 컨텍스트의 주요 구성요소

steps 컨텍스트에는 이전 단계의 데이터 및 출력 작업을 가능하게 하는 몇 가지 중요한 요소가 있다.

  • steps (object)
    • 이 컨텍스트는 작업의 각 step에 따라 변경된다. 작업의 모든 step에서 이 컨텍스트에 액세스할 수 있다. 이 개체에는 아래에 나열된 모든 속성이 포함된다.

 

  • steps.<step_id>.outputs (object)
    • 모든 step에 대한 output 값이다.

 

  • steps.<step_id>.conclusion (string)
    • continue-on-error 가 적용된 후 완료된 단계의 결과이다. 가능한 값은 success, failure, cancelled, skipped 이다. continue-on-error 단계가 실패하면 outcomefailure 이지만 최종 conclusionsuccess 이다.

 

  • steps.<step_id>.outcome (string)
    • continue-on-error 가 적용되기 전 완료된 단계의 결과이다. 가능한 값은 success, failure, cancelled, skipped 이다. continue-on-error 단계가 실패하면 outcomefailure 이지만 최종 conclusionsuccess 이다.

 

  • steps.<step_id>.outputs.<output_name> (string)
    • 특정 출력의 값이다.

 

continue-on-error 란?

continue-on-error 는 한 단계가 실패했을 때 작업이 실패하는 것을 방지한다. 이 단계가 실패했을 때 작업을 통과시키려면 true로 설정해야 한다. 이는 단계를 실행하고 싶지만 실패로 인해 후속 단계가 실행되는 것을 원하지 않을 때 유용하다.

 

name: Example Workflow with continue-on-error

on: [push]

jobs:
  test-job:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Code
        uses: actions/checkout@v2

      - name: Step that Might Fail
        id: may_fail
        run: |
          echo "Running a step that might fail"
          exit 1  # Simulate a failure
        continue-on-error: true

      - name: Check Outcome and Conclusion
        run: |
          echo "Outcome of may_fail: ${{ steps.may_fail.outcome }}"
          echo "Conclusion of may_fail: ${{ steps.may_fail.conclusion }}"

      - name: Final Step
        run: echo "This step runs regardless of the failure in 'Step that Might Fail'"

 

  • Step that Might Fail
    • 이 단계는 exit 1로 인해 의도적으로 실패한다.
    • continue-on-error: true 를 사용하면 이 단계가 실패하더라도 워크플로가 다음 단계로 진행할 수 있다.

 

  • Check Outcome and Conclusion
    • may_fail 단계의 outcomeconclusion 을 출력한다.
      • outcome: step 이 실패했기 때문에 failure 가 된다.
      • conclusion: continue-on-error 이 활성화되어 있으므로 이는 success 가 된다.
Outcome of may_fail: failure
Conclusion of may_fail: success

 

  • Final Step
    • 이전 단계의 실패와 관계없이 실행된다.
    • continue-on-error: true 가 없으면 Step that Might Fail 이후에 워크플로가 중지되었을 것이다.

 

 

 

 

steps 컨텍스트 예시

 

각 step 에는 후속 step 에서 해당 출력 및 상태에 액세스할 수 있도록 하는 id 가 할당될 수 있다.

steps.<step_id> 구문을 사용하여 ID로 단계 컨텍스트에 액세스할 수 있다.

- name: Build Project
  id: build
  run: echo "BUILD_VERSION=1.2.3" >> $GITHUB_OUTPUT

- name: Deploy Project
  run: echo "Using build version ${{ steps.build.outputs.BUILD_VERSION }}"
  • 여기서 steps.build.outputs.BUILD_VERSIONid: build 를 사용하여 단계의 BUILD_VERSION 출력에 액세스 한다.

 

 

step.<step_id>.outcome 을 활용해서 skipped 가 발생햇을때 새로운 변수를 할당하게 할 수 있다.

- name: Set GCP region JP for prod environment
  if: ${{ inputs.environment == 'stage' || inputs.environment == 'production' }}
  id: set-prod-region
  run: |
    echo "environment : ${{ inputs.environment }}"
    echo "GCP_REGION=asia-northeast1" >> $GITHUB_ENV

- name: Set GCP region KR
  if: ${{ steps.set-prod-region.outcome == 'skipped' }}
  run: |
    echo "environment : ${{ inputs.environment }}"
    echo "GCP_REGION=asia-northeast3" >> $GITHUB_ENV

 

 

step.<step_id>.outcome 을 활용해서 Build가 success 가 발생햇을 때 Deploy 하게 할 수 있다.

- name: Build
  id: build
  run: echo "Building..."

- name: Deploy
  if: steps.build.outcome == 'success'
  run: echo "Deploying..."

 

 

마지막으로 Github Action Workflow 으로 전체코드를 작성해본다.

jobs:
  build_and_test:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Set Version
        id: version
        run: echo "app_version=1.0.0" >> $GITHUB_OUTPUT

      - name: Run Build
        id: build
        run: echo "Build completed"

      - name: Run Tests
        id: tests
        run: |
          if [ "$RANDOM" -gt "20000" ]; then
            exit 1  # Simulating a failure
          fi
        continue-on-error: true

      - name: Deploy
        if: steps.tests.outcome == 'success'
        run: echo "Deploying version ${{ steps.version.outputs.app_version }}"
  • 버전 관리를 위한 출력 app_version 을 정의하며 후속 단계에서 steps.version.outputs.app_version 으로 액세스할 수 있다.
  • continue-on-error: true 구성으로 실패를 시뮬레이션하여 테스트를 실행한다. 이는 이 단계가 실패하더라도 워크플로가 중지되지 않음을 의미한다.
  • if 조건은 tests 단계가 성공했는지 확인한다. 그렇다면 Set Version 에 정의된 버전 출력을 사용하여 애플리케이션을 배포한다.

 

 

 

 


 

 

 

 

마무리

 

GitHub Actions의 steps Context는 단순한 출력값 전달을 넘어, 조건 분기, 에러 처리, 다단계 의존성 관리에 이르기까지 다양한 CI/CD 시나리오의 핵심 도구로 활용된다.


특히 steps.<id>.outcomecontinue-on-error 조합을 사용하면, 특정 단계 실패 여부에 따른 유연한 워크플로 설계를 할 수 있어 실제 운영 환경에서 매우 유용하다.

 

이러한 컨텍스트 기반 설계를 통해 GitHub Actions는 단순 자동화를 넘어서, 스마트하고 안정적인 배포 파이프라인을 구성하는 기반이 될 수 있다.

 

CI/CD 흐름에서 단계 간 상호작용이 필요한 시점이라면, steps Context의 다양한 활용법을 숙지하고 적극적으로 활용해보시길 추천드린다.

 

 

 

 

 

 

 

 


Reference

https://docs.github.com/ko/actions/writing-workflows/choosing-what-your-workflow-does/accessing-contextual-information-about-workflow-runs#steps-context

https://lo-victoria.com/github-actions-101-develop-a-cicd-workflow

https://docs.github.com/ko/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepscontinue-on-error

728x90
반응형