hangman_web이라는 repo를 실행.
1. 테스트 수행.
2. Docker Image 빌드.
3. Docker Image를 Docker Hub으로 푸시.
- 위 과정을 Github repo에 Github Actions로 구현.
- hanaman repo: https://github.com/learndataeng/hangman_web
- repo를 다운로드 받은 후, 본인의 깃헙 레포로 복사하여 저장(혹은 fork).
- requirements 설치 후,
python3 -m flask run --host=0.0.0.0 --port=4000
으로 실행 가능.
hangman repo에 dockerfile 추가하기.
- 이미지 이름 : ho99(DockerHub 계정 이름)/hangman
- apple chip의 경우 빌드 시 --platform=linux/amd64 필요.
- 해당 이미지를 docker run으로 테스트.
docker run -p 4000:4000 ho99/hangman
FROM python:3.8-slim-buster LABEL Maintainer="wogh6860@gmail.com" WORKDIR /app COPY app.py ./ COPY requirements.txt ./ RUN pip3 install -r requirements.txt EXPOSE 4000 CMD ["python3", "-m", "flask", "run", "--host=0.0.0.0", "--port=4000"]
docker build --platform=linux/amd64 -t ho99/hangman .
실행.docker image ls
docker inspect ho99/hangman
docker run -p 4000:4000 ho99/hangman
docker ps
docker stop {container ID}
docker run -p 4000:4000 -d ho99/hangman
. -d를 통해서 백그라운드에서 돌아가도록 설정 가능.docker push ho99/hangman
으로 dockerhub에 푸쉬.docker run -p 4000:4000 ho99/hangman
소프트웨어 빌드란,
- 개발한 소프트웨어를 출시할 수 있는 형태로 만드는 것.
- 테스트가 충분히 있어야 함.
Continuous Integration.
- 코드 Repo.는 하나만 유지. (main)
- 코드 변경을 최대한 자주 반영.
- 테스트를 최대한 추가. (Test Coverage)
- 빌드 자동화.
- 배포 자동화. Continuous Delivery.
빌드 실패.
- 새 코드의 커밋으로 인해 테스트를 실패하는 경우.
- 일반적으로 빌드 실패 시 다시 성공할 때까지 코드 변경을 금지함.
flake8 툴.
- 파이썬 코드에서 에러나 코딩 스타일 등에서 이슈를 체크해 주는 툴.
flake8. --count --exit-zero --max-complexity=10 --max-line-length=127 --stastics
flake8 sample.py
python3 -m unittest test.py
"""Unit test cases for hangman game."""
import unittest
import app as hangman
class HangmanTestCase(unittest.TestCase):
# def setUp(self):
#
# checkCorrectAnswer(correctLetters, secretWord)
def test_checkCorrectAnswer(self):
answer = hangman.checkCorrectAnswer("baon", "baboon")
self.assertTrue(answer)
def test_checkWrongAnswer(self):
answer = hangman.checkWrongAnswer("zebrio", "zebra")
self.assertTrue(answer)
def test_1(self):
answer = hangman.checkCorrectAnswer("bazn", "baboon")
self.assertFalse(answer)
def test_2(self):
answer = hangman.checkCorrectAnswer("", " ")
self.assertFalse(answer)
def test_3(self):
answer = hangman.checkCorrectAnswer("ZEBRA", "zebra")
self.assertFalse(answer)
if __name__ == "__main__":
unittest.main()
# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python
name: Python application
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
permissions:
contents: read
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python 3.10
uses: actions/setup-python@v3
with:
python-version: "3.10"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with unittest
run: |
python -m unittest discover -p 'test*.py'
Docker 관련 테스트 추가.
- Actions -> 워크플로 추가 -> Docker Image configure -> yml 생성.
- settings의 security에서 docker 로그인 정보 변수 입력.
- docker-image.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 login env: DOCKER_USER: ${{secrets.DOCKER_USER}} DOCKER_PASSWORD: ${{secrets.DOCKER_PASSWORD}} 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