github actions란?

  • ci/cd 를 github에서 구현하기 위한 서비스.
  • 코드 테스트, 빌드, 배포 자동화 기능을 제공한다.
  • github actions로 만들 어진것 workflow라 부르며 아래 컴포넌트로 구성(events,jobs,actions, runner)

 

workflow

  • 트리거 이벤트 발생하면 시작되는 동작들
  • 트리커 이벤트 예 : 코드커밋, pr(pull request)생성, 또다른 workflow의 실행
  • workflow를 위한 명령어들은 yaml 파일로 저장, yaml은 환경설정과 scripts 실행들이 있다.
  • 윈도우나 리눅스 서버 위에서 runner에 의해 실행됨
  • docker image를 만들어 테스트 하고 업로드하기도 한다.(git과 docker와 연동 강화)

 

github actions 사용

  1. repo 별로 들어가면 actions 메뉴가 있다.
  2. workflow 생성
  3. yml 파일을 직접 생성 혹은 템플릿 선택 후 수정


실습개요

 

'docker(도커) & 쿠버네티스' 카테고리의 글 목록

 

allofdater.tistory.com

여기서 잠깐!
flake8 은 에러를 찾아주고, 통일된 형식으로 코드를 작성하는걸 도와준다.



random 모듈 안쓰임
def lower(a) 옆에 : 생략
# 앞에 공백 최소 두개는 있어야함

yaml 파일의 구조(환경설정에 많이 쓰임)
1 : 그리고 ' ' 공백으로 구분
2  리스트시 -와 ' ' 공백
3 키(contact) 서브키(phone) value(home, work) 를 공백 두개씩 차이남
4 | 사용시 엔터 해도 this ~string 까지 하나의 value로 인식

+ json 과 yaml 의 차이

 

실습1: github actions 실습

  1. CI에서 python application 선택
  2. yml(or yaml) 파일 편집 : main branch에서 push 나 pull 을 요청시 액션스 실행 조건
  3. 자동으로 내용이 채워지는데 일부 수정 필요.




name: Python application

on:
  push:
     # main 브랜치에 push 이벤트 발생 시
    branches: [ "main" ]
  pull_request:
    # main 브랜치로 pull request가 생성되었을 때
    branches: [ "main" ]

permissions:
  contents: read

jobs:
  build:
    
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3

    # Python 3.10 설치
    - name: Set up Python 3.10
      uses: actions/setup-python@v3
      with:
        python-version: "3.10"

    - name: Install dependencies
      run: |
        # pip를 최신 버전으로 업그레이드
        python -m pip install --upgrade pip
        
        # flake8 패키지 설치
        pip install flake8
        
        # requirements.txt 파일이 존재하면 해당 파일에 명시된 의존성 패키지들을 설치
        if [ -f requirements.txt ]; then pip install -r requirements.txt; fi

    - name: Lint with flake8
      run: |
        # Python 문법 오류나 정의되지 않은 이름이 있으면 빌드 중단
        flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
        
        # exit-zero는 모든 오류를 경고로 처리합니다. GitHub 에디터는 127자 폭
        flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics

    - name: Test with unittest
      run: |
        # m은 모듈을 지정하는 옵션, unittest 모듈을 실행하라는 의미
        # -p는 파일 패턴을 지정하는 옵션
        python -m unittest discover -p 'test*.py'

 

4. 이제 커밋할때, yaml이 잘 동작하는지 테스트

1) 리드미 파일 수정후 commit


2) actions 작동을 위한 main에 바로 commit


3) 홈에 가면 노란색 진행중 버튼 클릭


4) 디테일 눌러보면


5) 완료 된것을 단계별로 확인가능

 

 

실습2: 이번엔 변화가 있으면 docker hub로 업로드

  1. 시작전 준비: 도커파일제작(https://allofdater.tistory.com/22), 제작한 hangman_web, 깃 레포에 업로드(push)

  2. docker hub 로그인 정보를 세팅해야함.
    + user 이메일 아닙니다, 계정명입니다.

  3. 깃허브에서 docker image 템플릿 다운

    위 화면이 안나오면다면, workflow가 이미 있어서 그런거니 new workflow 눌러 추가 
  4. yml 파일 편집

    name: Docker Image CI

    on:
      push:
        branches: [ "main" ]
      pull_request:
        branches: [ "main" ]

    jobs:

      build:

        runs-on: ubuntu-latest

        steps:
        - uses: actions/checkout@v3
        - name: docker image
          env:
          #저장했던 아이디와 비밀번호 연결
           DOCKER_USER: ${{secrets.DOCKER_USER}}
           DOCKER_PASSWORD: ${{secrets.DOCKER_PASSWORD}}
          
          #로그인 실행 -u(유저) -p(비밀번호)
          run: |
           docker login -u $DOCKER_USER -p $DOCKER_PASSWORD
        - name: Build the Docker image
        # 도커 이미지 만듦
          run: docker build --tag ${{secrets.DOCKER_USER}}/hangman:latest .
        - name: docker push
        # 만든 이미지 도커허브에 업로드
          run: docker push ${{secrets.DOCKER_USER}}/hangman:latest

    + 들여쓰기 잘해야합니다. 잘못하면 빨간색 밑줄 뜨는데 스페이스로 잘 조절하면 없어짐.
    + 다 수정후 commit changes 클릭

  5.  동작 확인
  6. 로컬에서 깃 내용 다운후 실행 까지 해보자

    # 현재 로컬에 실행중인 legoking/hangman 찾음
    weare@DESKTOP-BE1I4GE:~$ docker image ls
    REPOSITORY                 TAG       IMAGE ID       CREATED         SIZE
    legoking/hangman           latest    f63deabbaf88   2 days ago      155MB
    apache/airflow             2.5.1     cb77a108637f   11 days ago     1.23GB
    ubuntu                     latest    174c8c134b2a   2 weeks ago     77.9MB
    postgres                   13        135171763bd4   2 weeks ago     413MB
    redis                      latest    e40e2763392d   3 weeks ago     138MB
    docker/welcome-to-docker   latest    c1f619b6477e   7 weeks ago     18.6MB
    mysql/mysql-server         8.0       1d9c2219ff69   11 months ago   496MB

    # 삭제
    weare@DESKTOP-BE1I4GE:~$ docker image rm -f f63deabbaf88
    Untagged: legoking/hangman:latest
    Untagged: legoking/hangman@sha256:5423af489767acf74d147fd86b81d6683b8c0c1b057f86b9007fa5115a916d29
    Deleted: sha256:f63deabbaf881311d0ba660aed5ab2f313fabdfc10b23603750518663ab7538c
    weare@DESKTOP-BE1I4GE:~$ docker image ls
    REPOSITORY                 TAG       IMAGE ID       CREATED         SIZE
    apache/airflow             2.5.1     cb77a108637f   11 days ago     1.23GB
    ubuntu                     latest    174c8c134b2a   2 weeks ago     77.9MB
    postgres                   13        135171763bd4   2 weeks ago     413MB
    redis                      latest    e40e2763392d   3 weeks ago     138MB
    docker/welcome-to-docker   latest    c1f619b6477e   7 weeks ago     18.6MB
    mysql/mysql-server         8.0       1d9c2219ff69   11 months ago   496MB

    # legoking/hangman를 받는데, 로컬에 없으니 깃허브에 가서 자동으로 받아옴
    -p 4000:4000 이건 내부 콘테이너 4000을 외부 4000 포트로 포트포워딩 (이래야 4000 주소로 접속가능)
    weare@DESKTOP-BE1I4GE:~$ docker run -p 4000:4000 legoking/hangman
    Unable to find image 'legoking/hangman:latest' locally
    latest: Pulling from legoking/hangman
    8b91b88d5577: Already exists
    824416e23423: Already exists
    8f777578c172: Already exists
    cbfea27109a8: Already exists
    276dfcf5deff: Already exists
    b4e3c0c59c7b: Pull complete
    5a0899081932: Pull complete
    2aa60784fc2e: Pull complete
    a8f2ad823d12: Pull complete
    Digest: sha256:51711cd316ca73213223e44e257ccda66453036d618963b4b46c23a7ff485bf4
    Status: Downloaded newer image for legoking/hangman:latest
     * Debug mode: off
    WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
     * Running on all addresses (0.0.0.0)
     * Running on http://127.0.0.1:4000
     * Running on http://172.17.0.2:4000
    Press CTRL+C to quit

    # 127.0.0.1:4000로 접속 (정상동작)

'깃허브' 카테고리의 다른 글

깃허브 기초  (1) 2023.12.26
깃허브 CI,CD 기초  (0) 2023.12.24

+ Recent posts