데보션앱 소개페이지 바로가기
로그인 선택

신고하기

CLOSE
신고사유 (대표 사유 1개)
상세내용 (선택)
0/200
  • 신고한 게시글은 더 이상 보이지 않습니다.
  • 이용약관과 운영정책에 따라 신고사유에 해당하는지 검토 후 조치됩니다.
  • 허위 신고인 경우, 신고자의 서비스 이용이 제한될 수 있으니 유의하시어 신중하게 신고해 주세요.
(이 회원이 작성한 모든 댓글과 커뮤니티 게시물이 보이지 않고, 알림도 오지 않습니다.)

미리보기

커뮤니티

      1,234

      badge 23.06.15

      글 등록

      카테고리를 선택해주세요.

      DEVOTEE를 활성화 시키면
      지금 작성한 커뮤니티 글에 대해 1개의 댓글을 달아줍니다.

      버튼을 누르면 글 수정 시 ChatGPT가 작성한 댓글이 수정됩니다.

      임시저장함에 저장되었습니다. 저장일시 : 2022.5.17 14:29:08

      임시저장함

      제목을 선택하시면 이어서 작성이 가능하며,
      최대 20건까지 저장합니다.
      컨텐츠 유형, 제목, 저장일시, 삭제로 이뤄진 임시저장 목록
      컨텐츠 유형 제목 저장일 삭제

      데보션 블로그 게재 요청

      CLOSE
      • *
      • *

      본인인증

      효율적인 데보션 서비스 이용 및
      고객님의 소중한 개인정보보호를 위해
      본인인증을 진행해주세요. 본인인증 미 진행 시 로그인이 제한됩니다.
      본인인증 실패

      본인인증 로그인에 실패하였습니다.
      회원이 아니시거나 본인인증 등록이
      완료되지 않은 사용자입니다.

      회원정보 연결

      Github Action 을 cli 로 manual 하게 호출하기

      seungkyua 21.10.29
      8,832 7 2

      최근 github action 을 workflow CI/CD 로 활용하는 사례가 점점 늘어나고 있다. 이전 글인 "10분만에 만드는 Docker image 저장 자동화"도 github action 을 활용하여 docker registry 에 컨테이너 이미지를 빌드하고 push 하는 방법을 설명하였다.

      이번에는 github action 을 client cli 를 활용하여 원하는 시점에 manual 로 호출하는 방법을 알아보자.

       

      1. 새로운 github repository 만들기

      먼저 github repository 를 만들어 clone 한다.

      $ git clone https://github.com/seungkyua/github-action-sample.git
      $ cd github-action-sample
      

       

      2. workflow action 파일 만들기

      github action 을 위한 .github/workflows 디렉토리를 생성하고 action 파일인 hello.yaml 파일을 만든다. 디렉토리 명이 workflow 가 아닌 workflows 이니 실수하지 않게 조심하자.

      $ mkdir -p .github/workflow
      $ touch .github/workflows/hello.yaml
      

       

      hello.yaml 은 다음과 같다.

      name: Hello
      
      on: 
        workflow_dispatch:
          inputs:
            greeting:
              required: false
              default: 'Hello'
              type: string
            name:
              required: false
              default: 'Seungkyu'
              type: string
      
      jobs:
        build:
          runs-on: ubuntu-latest
          steps:
            # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
            - uses: actions/checkout@v2
      
            - name: Run hello.sh
              run: |
                set -xe
                chmod +x .github/workflows/hello.sh
                .github/workflows/hello.sh ${{ github.event.inputs.greeting }} ${{ github.event.inputs.name }}
      

       

      workflow 에서 parameter 를 받기 위해서는 workflow_dispatch: 를 명시적으로 선언하고 parameter 는 inputs 아래에 기술한다. inputs 아래 나오는 이름이 parameter 명이 되며 필수 여부, 기본값, 타입 등을 지정할 수 있다.

      여기서는 'greeting' 과 'name' 이라는 2개의 parameter 를 받는 다고 선언하였다.

      다음은 workflow step 인데 첫번째 actions/checkout@v2 는 현재의 github repository 소스를 다운 받는 다는 의미이다. 두번째 name: Run hello.sh 는 run: 으로 shell 을 실행하겠다는 의미이며, 앞 단계에서 소스를 다운받았으니 hello.sh 을 실행할 수 있게 되었으며, 실제로 hello.sh 을 실행하면서 parameter 를 argument 로 호출한다.

      parameter 는 ${{ github.event.inputs.파라미터명 }} 과 같이 활용할 수 있다.

       

      다음은 간단산 hello.sh 쉡스크립트 이다.

      #!/bin/bash
      
      if [ $# -eq 1 ]; then
        greeting=$1
      elif [ $# -eq 2 ]; then
        greeting=$1
        name=$2
      fi
      
      echo "[hello.sh] ${greeting} ${name}\n"
      

       

      이제 해당 파일들을 github 에 push 한다.

       

      3. workflow 호출하기

      github workflow client 를 다운 받는다. mac 에서는 brew 로 간단히 설치할 수 있다.

      $ brew install gh
      

       

      github-action-sample 소스를 clone 한 디렉토리에서 gh auth login 으로 github 에 로그인 한다.

      $  gh auth login
      ? What account do you want to log into?  [Use arrows to move, type to filter]
      > GitHub.com
        GitHub Enterprise Server
      

       

      GitHub.com 을 선택하고 엔터를 치면 아래의 내용이 나온다.

      ? What account do you want to log into? GitHub.com
      ? You're already logged into github.com. Do you want to re-authenticate? (y/N) y
      

       

      이미 로그인을 했기 때문에 다시 로그인 하겠다는 의미이며 처음이면 로그인 하라는 메세지가 나온다. y 로 선택하고 로그인을 하자.

      ? What account do you want to log into? GitHub.com
      ? You're already logged into github.com. Do you want to re-authenticate? Yes
      ? What is your preferred protocol for Git operations?  [Use arrows to move, type to filter]
      > HTTPS
        SSH
      

       

      HTTPS 를 선택한다.

      ? What account do you want to log into? GitHub.com
      ? You're already logged into github.com. Do you want to re-authenticate? Yes
      ? What is your preferred protocol for Git operations? HTTPS
      ? Authenticate Git with your GitHub credentials? (Y/n) y
      

       

      y 로 Github credentials 로 로그인을 한다.

      ? What account do you want to log into? GitHub.com
      ? You're already logged into github.com. Do you want to re-authenticate? Yes
      ? What is your preferred protocol for Git operations? HTTPS
      ? Authenticate Git with your GitHub credentials? Yes
      ? How would you like to authenticate GitHub CLI?  [Use arrows to move, type to filter]
        Login with a web browser
      > Paste an authentication token
      

       

      token 으로 로그인을 한다.

      ? What account do you want to log into? GitHub.com
      ? You're already logged into github.com. Do you want to re-authenticate? Yes
      ? What is your preferred protocol for Git operations? HTTPS
      ? Authenticate Git with your GitHub credentials? Yes
      ? How would you like to authenticate GitHub CLI? Paste an authentication token
      Tip: you can generate a Personal Access Token here https://github.com/settings/tokens
      The minimum required scopes are 'repo', 'read:org', 'workflow'.
      ? Paste your authentication token: ****************************************
      

       

      정상적으로 login 이 되면 아래와 같이 표시된다.

      - gh config set -h github.com git_protocol https
      ✓ Configured git protocol
      ✓ Logged in as seungkyua
      

       

      로그인 후에 gh workflow list 명령어로 workflow 를 조회하면 아래와 같이 Hello 란 workflow 가 있음을 알 수 있다.

      ➜ github-action-sample git:(main) gh workflow list
      Hello  active  14639329
      

       

      이를 실행해 본다.

      $ gh workflow run hello.yaml -f greeting=Welcome -f name=Seungkyu
      

       

      실행 중인 workflow 를 조회할 수 있다.

      $ gh run list --workflow=hello.yaml
      STATUS  NAME            WORKFLOW  BRANCH  EVENT              ID          ELAPSED  AGE
      *       Initial commit  Hello     main    workflow_dispatch  1390917347  7s       0m
      

       

      완료된 후의 workflow 를 조회하면 STATUS 가 완료로 체크되어 있다.

      $ gh run list --workflow=hello.yaml
      STATUS  NAME            WORKFLOW  BRANCH  EVENT              ID          ELAPSED  AGE
      ✓       Initial commit  Hello     main    workflow_dispatch  1390917347  15s      2m
      

       

      웹 브라우저로 repository https://github.com/seungkyua/github-action-sample 에 접속하여 Action 탭의 build 를 클릭해보면 아래의 화면 처럼 '[hello.sh] Welcome Seungkyu\n' 가 출력되어 있음을 알 수 있다.

       

       

       

      4. 활용법

      manual 하게 github action 을 호출하는 부분은 다른 workflow tool 과 같이 사용할 때가 많다. 예를 들면 argo workflow 에서 하나의 단계로 github action 을 필요한 시점에 호출하여 결과를 활용하는 등으로 사용할 수 있다.

      github action 은 github 의 리소스를 활용하므로 CI 에서 많은 리소스가 필요할 때는 github action 을 같이 활용하는 것도 좋은 대안이 될 것이다.

       

      댓글 0

      DEVOTEE를 활성화 시키면
      지금 작성한 댓글에 AI가 댓글을 달아줍니다.

      seungkyua 님의 최신 블로그

      더보기
      동영상 기고하기