From 990a7c9b00523f1022caffee8bbb8798425a0945 Mon Sep 17 00:00:00 2001 From: samuelstrike Date: Wed, 29 Jan 2025 17:44:22 +0800 Subject: [PATCH] updated docker file and workflow --- .github/workflows/cicd.yml | 64 ++++++++++++++++++++++++++++++++++++++ DockerFile | 11 +++++++ app.py | 10 ++++++ test_app.py | 6 ++++ 4 files changed, 91 insertions(+) diff --git a/.github/workflows/cicd.yml b/.github/workflows/cicd.yml index e69de29..94c0637 100644 --- a/.github/workflows/cicd.yml +++ b/.github/workflows/cicd.yml @@ -0,0 +1,64 @@ +name: CI/CD for Dockerized Flask App + +on: + push: + branches: [ main ] + pull_request: + branches: [ main ] + +jobs: + dockerbuild: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Build The Docker Image + run: docker build . --file DockerFile --tag workflow-test:$(date +%s) + build-and-test: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: '3.9' + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install flask + pip install pytest + + - name: Run tests + run: | + pytest + + build-and-publish: + needs: build-and-test + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + + - name: Login to DockerHub + uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + - name: Build and push Docker image + uses: docker/build-push-action@v4 + with: + context: . + file: ./DockerFile + push: true + tags: ${{ secrets.DOCKER_USERNAME }}/flasktest-app:latest + + - name: Image digest + run: echo ${{ steps.build-and-publish.outputs.digest }} \ No newline at end of file diff --git a/DockerFile b/DockerFile index e69de29..c1857e3 100644 --- a/DockerFile +++ b/DockerFile @@ -0,0 +1,11 @@ +FROM python:3.9-slim + +WORKDIR /app + +COPY . /app + +RUN pip install flask + +EXPOSE 5001 + +CMD [ "python" , "app.py" ] \ No newline at end of file diff --git a/app.py b/app.py index e69de29..3ce4669 100644 --- a/app.py +++ b/app.py @@ -0,0 +1,10 @@ +from flask import Flask + +app = Flask(__name__) + +@app.route('/') +def home(): + return 'Hello, World!' + +if __name__ == '__main__': + app.run(host="0.0.0.0", port=5001) \ No newline at end of file diff --git a/test_app.py b/test_app.py index e69de29..e8e657a 100644 --- a/test_app.py +++ b/test_app.py @@ -0,0 +1,6 @@ +from app import app + +def test_home(): + response = app.test_client().get('/') + assert response.status_code == 200 + assert response.data == b'Hello, World!' \ No newline at end of file