diff --git a/.github/workflows b/.github/workflows deleted file mode 100644 index e69de29..0000000 diff --git a/.github/workflows/cicd.yml b/.github/workflows/cicd.yml new file mode 100644 index 0000000..c04cc76 --- /dev/null +++ b/.github/workflows/cicd.yml @@ -0,0 +1,62 @@ +name: Flask Docker CICD Tutorial + +on: + push: + branches: + [ main ] + pull request: + branches: + [ main ] + + +jobs: + build-and-test: + runs-on: ubuntu-latest + + steps: + - name: Checkout Code + uses: actions/checkout@v3 + + - name: Setup Python + uses: actions/setup-python@v4 + with: + python-versions: '3.9' + + - name: Install Dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt + + - 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: Setup Docker BuildX + uses: docker/setup-buildx-actions@v2 + + - name: Login to Docker + uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + - name: Push image to Docker + uses: docker/build-push-action@v4 + with: + context: . + file: ./Dockerfile + push: true + tags: ${{ secrets.DOCKER_USERNAME }}/flaskapp:latest + + - name: Image digest + run: echo ${{ secrets.build-and-publish.outputs.digest }} + + diff --git a/DockerFile b/DockerFile index e69de29..03bfa4b 100644 --- a/DockerFile +++ b/DockerFile @@ -0,0 +1,6 @@ +FROM python:3.9-slim +WORKDIR /app +COPY . /app +RUN pip install -r requirements.txt +EXPOSE 5000 +CMD ["python", "app.py"] \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..5f8fa41 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +## Github Actions Tutorial + +This project includes, diff --git a/app.py b/app.py index e69de29..06a7dc7 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(debug=True, host='0.0.0.0', port=5000) \ No newline at end of file diff --git a/test_app.py b/test_app.py index e69de29..6409d6f 100644 --- a/test_app.py +++ b/test_app.py @@ -0,0 +1,7 @@ +from app import app + +def test_home(): + response = app.test_client.get('/') + + assert response.status_code == 200 + assert response.data == "hello world" \ No newline at end of file