From 558159b9836b099645b409c8f3d8da0ede4a9f5a Mon Sep 17 00:00:00 2001 From: Tharushi <2021is059@stu.ucsc.cmb.ac.lk> Date: Mon, 28 Jul 2025 15:33:51 +0530 Subject: [PATCH 01/31] Updated home route with custom welcome message --- app/routes.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/app/routes.py b/app/routes.py index 8aa3782..a3eeb5f 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,11 +1,14 @@ -from flask import json, jsonify +from flask import jsonify from app import app from app import db from app.models import Menu @app.route('/') def home(): - return jsonify({ "status": "ok" }) + return jsonify({ + "message": "Welcome to Tharushi's CI/CD demo app 🎉", + "status": "ok" + }) @app.route('/menu') def menu(): @@ -16,4 +19,4 @@ def menu(): else: body = { "error": "Sorry, the service is not available today." } status = 404 - return jsonify(body), status \ No newline at end of file + return jsonify(body), status From fea8414b8538a7aeee44f918a2d7fcb42b2feaef Mon Sep 17 00:00:00 2001 From: Tharushi <2021is059@stu.ucsc.cmb.ac.lk> Date: Mon, 28 Jul 2025 15:35:02 +0530 Subject: [PATCH 02/31] Add GitHub Actions CI workflow --- .github/workflows/ci.yml | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 .github/workflows/ci.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..11883bb --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,29 @@ +name: Python Flask CI + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +jobs: + 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 -r requirements.txt + + - name: Run tests + run: | + pytest From 145ab359fa2c577003080e709db67bad20ad6cca Mon Sep 17 00:00:00 2001 From: Nethma-k <111905294+Nethma-k@users.noreply.github.com> Date: Mon, 28 Jul 2025 15:37:08 +0530 Subject: [PATCH 03/31] Delete .github/workflows/codeql-analysis.yml --- .github/workflows/codeql-analysis.yml | 67 --------------------------- 1 file changed, 67 deletions(-) delete mode 100644 .github/workflows/codeql-analysis.yml diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml deleted file mode 100644 index 4151f4c..0000000 --- a/.github/workflows/codeql-analysis.yml +++ /dev/null @@ -1,67 +0,0 @@ -# For most projects, this workflow file will not need changing; you simply need -# to commit it to your repository. -# -# You may wish to alter this file to override the set of languages analyzed, -# or to provide custom queries or build logic. -# -# ******** NOTE ******** -# We have attempted to detect the languages in your repository. Please check -# the `language` matrix defined below to confirm you have the correct set of -# supported CodeQL languages. -# -name: "CodeQL" - -on: - push: - branches: [ master ] - pull_request: - # The branches below must be a subset of the branches above - branches: [ master ] - schedule: - - cron: '31 0 * * 1' - -jobs: - analyze: - name: Analyze - runs-on: ubuntu-latest - - strategy: - fail-fast: false - matrix: - language: [ 'python' ] - # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ] - # Learn more: - # https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed - - steps: - - name: Checkout repository - uses: actions/checkout@v2 - - # Initializes the CodeQL tools for scanning. - - name: Initialize CodeQL - uses: github/codeql-action/init@v1 - with: - languages: ${{ matrix.language }} - # If you wish to specify custom queries, you can do so here or in a config file. - # By default, queries listed here will override any specified in a config file. - # Prefix the list here with "+" to use these queries and those in the config file. - # queries: ./path/to/local/query, your-org/your-repo/queries@main - - # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). - # If this step fails, then you should remove it and run the build manually (see below) - - name: Autobuild - uses: github/codeql-action/autobuild@v1 - - # â„šī¸ Command-line programs to run using the OS shell. - # 📚 https://git.io/JvXDl - - # âœī¸ If the Autobuild fails above, remove it and uncomment the following three lines - # and modify them (or add more) to build your code if your project - # uses a compiled language - - #- run: | - # make bootstrap - # make release - - - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v1 From 8e1de5850f96f83937801bfa876c8b2c7d928087 Mon Sep 17 00:00:00 2001 From: Tharushi <2021is059@stu.ucsc.cmb.ac.lk> Date: Mon, 28 Jul 2025 15:41:07 +0530 Subject: [PATCH 04/31] Add GitHub Actions CI workflow --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 11883bb..8471b51 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -17,7 +17,7 @@ jobs: - name: Set up Python uses: actions/setup-python@v4 with: - python-version: 3.9 + python-version: 3.9.6 - name: Install dependencies run: | From 7f249902da6095e7a25f9443eee2a66df78958ff Mon Sep 17 00:00:00 2001 From: Nethma-k <111905294+Nethma-k@users.noreply.github.com> Date: Mon, 28 Jul 2025 15:41:17 +0530 Subject: [PATCH 05/31] Update ci.yml --- .github/workflows/ci.yml | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 11883bb..90f0fbb 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -7,23 +7,22 @@ on: branches: [ master ] jobs: - build-and-test: + test: runs-on: ubuntu-latest - steps: - name: Checkout code - uses: actions/checkout@v3 + uses: actions/checkout@v4 - - name: Set up Python + - name: Set up Python 3.9 uses: actions/setup-python@v4 with: - python-version: 3.9 + python-version: '3.9' - name: Install dependencies run: | python -m pip install --upgrade pip - pip install -r requirements.txt + pip install pytest + # pip install -r requirements.txt - - name: Run tests - run: | - pytest + - name: Run pytest + run: pytest From 806be3a5c55baa6a4d522ce87b002f2a02759d7e Mon Sep 17 00:00:00 2001 From: Tharushi <2021is059@stu.ucsc.cmb.ac.lk> Date: Mon, 28 Jul 2025 15:42:04 +0530 Subject: [PATCH 06/31] Fix CI workflow syntax --- .github/workflows/ci.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8471b51..a7791a9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -25,5 +25,4 @@ jobs: pip install -r requirements.txt - name: Run tests - run: | - pytest + run: pytest From 23e5376d67b7604f9440fb4035c9c2990b148c08 Mon Sep 17 00:00:00 2001 From: Tharushi <2021is059@stu.ucsc.cmb.ac.lk> Date: Mon, 28 Jul 2025 15:49:39 +0530 Subject: [PATCH 07/31] Update Python version to 3.9.17 in GitHub Actions workflow --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a7791a9..ef2904d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -17,7 +17,7 @@ jobs: - name: Set up Python uses: actions/setup-python@v4 with: - python-version: 3.9.6 + python-version: 3.9.17 - name: Install dependencies run: | From 7e0c5d18b3cf1615e6da2f6f1344ec97cf4784be Mon Sep 17 00:00:00 2001 From: Nethma-k <111905294+Nethma-k@users.noreply.github.com> Date: Mon, 28 Jul 2025 15:51:38 +0530 Subject: [PATCH 08/31] Create google-cloudrun-docker.yml --- .../workflows/google-cloudrun-docker.yml | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 .github/workflows/.github/workflows/google-cloudrun-docker.yml diff --git a/.github/workflows/.github/workflows/google-cloudrun-docker.yml b/.github/workflows/.github/workflows/google-cloudrun-docker.yml new file mode 100644 index 0000000..4a3e557 --- /dev/null +++ b/.github/workflows/.github/workflows/google-cloudrun-docker.yml @@ -0,0 +1,48 @@ +name: 'Build and Deploy to Cloud Run' + +on: + push: + branches: + - main + +env: + PROJECT_ID: 'thermal-hour-467308-u4' + GAR_NAME: 'gh-demo' + REGION: 'us-central1' + SERVICE: 'gitactionnew' + +jobs: + deploy: + runs-on: ubuntu-latest + + steps: + - name: 'Checkout' + uses: actions/checkout@v4 + + - name: 'Authenticate to Google Cloud with SA Key' + uses: google-github-actions/auth@v2 + with: + credentials_json: '${{ secrets.ABC }}' + + - name: 'Set up gcloud CLI' + uses: google-github-actions/setup-gcloud@v2 + + - name: 'Docker Auth' + run: gcloud auth configure-docker "${{ env.REGION }}-docker.pkg.dev" + + - name: 'Build and Push Docker Image' + run: | + IMAGE="${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.GAR_NAME }}/${{ env.SERVICE }}:${{ github.sha }}" + docker build -t "$IMAGE" . + docker push "$IMAGE" + + - name: 'Deploy to Cloud Run' + id: deploy + uses: google-github-actions/deploy-cloudrun@v2 + with: + service: ${{ env.SERVICE }} + region: ${{ env.REGION }} + image: "${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.GAR_NAME }}/${{ env.SERVICE }}:${{ github.sha }}" + + - name: Show Deployed URL + run: echo ${{ steps.deploy.outputs.url }} From 9521a61d97654623ce6fa0bb8f50d49a4ca0696b Mon Sep 17 00:00:00 2001 From: Nethma-k <111905294+Nethma-k@users.noreply.github.com> Date: Mon, 28 Jul 2025 15:53:33 +0530 Subject: [PATCH 09/31] Delete .github/workflows/.github/workflows directory --- .../workflows/google-cloudrun-docker.yml | 48 ------------------- 1 file changed, 48 deletions(-) delete mode 100644 .github/workflows/.github/workflows/google-cloudrun-docker.yml diff --git a/.github/workflows/.github/workflows/google-cloudrun-docker.yml b/.github/workflows/.github/workflows/google-cloudrun-docker.yml deleted file mode 100644 index 4a3e557..0000000 --- a/.github/workflows/.github/workflows/google-cloudrun-docker.yml +++ /dev/null @@ -1,48 +0,0 @@ -name: 'Build and Deploy to Cloud Run' - -on: - push: - branches: - - main - -env: - PROJECT_ID: 'thermal-hour-467308-u4' - GAR_NAME: 'gh-demo' - REGION: 'us-central1' - SERVICE: 'gitactionnew' - -jobs: - deploy: - runs-on: ubuntu-latest - - steps: - - name: 'Checkout' - uses: actions/checkout@v4 - - - name: 'Authenticate to Google Cloud with SA Key' - uses: google-github-actions/auth@v2 - with: - credentials_json: '${{ secrets.ABC }}' - - - name: 'Set up gcloud CLI' - uses: google-github-actions/setup-gcloud@v2 - - - name: 'Docker Auth' - run: gcloud auth configure-docker "${{ env.REGION }}-docker.pkg.dev" - - - name: 'Build and Push Docker Image' - run: | - IMAGE="${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.GAR_NAME }}/${{ env.SERVICE }}:${{ github.sha }}" - docker build -t "$IMAGE" . - docker push "$IMAGE" - - - name: 'Deploy to Cloud Run' - id: deploy - uses: google-github-actions/deploy-cloudrun@v2 - with: - service: ${{ env.SERVICE }} - region: ${{ env.REGION }} - image: "${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.GAR_NAME }}/${{ env.SERVICE }}:${{ github.sha }}" - - - name: Show Deployed URL - run: echo ${{ steps.deploy.outputs.url }} From fc3f1f1ab76df4972d0a8e59e6a7f1164c04bdd3 Mon Sep 17 00:00:00 2001 From: Nethma-k <111905294+Nethma-k@users.noreply.github.com> Date: Mon, 28 Jul 2025 15:53:53 +0530 Subject: [PATCH 10/31] Create cloudrunner --- .github/workflows/cloudrunner | 48 +++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 .github/workflows/cloudrunner diff --git a/.github/workflows/cloudrunner b/.github/workflows/cloudrunner new file mode 100644 index 0000000..4a3e557 --- /dev/null +++ b/.github/workflows/cloudrunner @@ -0,0 +1,48 @@ +name: 'Build and Deploy to Cloud Run' + +on: + push: + branches: + - main + +env: + PROJECT_ID: 'thermal-hour-467308-u4' + GAR_NAME: 'gh-demo' + REGION: 'us-central1' + SERVICE: 'gitactionnew' + +jobs: + deploy: + runs-on: ubuntu-latest + + steps: + - name: 'Checkout' + uses: actions/checkout@v4 + + - name: 'Authenticate to Google Cloud with SA Key' + uses: google-github-actions/auth@v2 + with: + credentials_json: '${{ secrets.ABC }}' + + - name: 'Set up gcloud CLI' + uses: google-github-actions/setup-gcloud@v2 + + - name: 'Docker Auth' + run: gcloud auth configure-docker "${{ env.REGION }}-docker.pkg.dev" + + - name: 'Build and Push Docker Image' + run: | + IMAGE="${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.GAR_NAME }}/${{ env.SERVICE }}:${{ github.sha }}" + docker build -t "$IMAGE" . + docker push "$IMAGE" + + - name: 'Deploy to Cloud Run' + id: deploy + uses: google-github-actions/deploy-cloudrun@v2 + with: + service: ${{ env.SERVICE }} + region: ${{ env.REGION }} + image: "${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.GAR_NAME }}/${{ env.SERVICE }}:${{ github.sha }}" + + - name: Show Deployed URL + run: echo ${{ steps.deploy.outputs.url }} From f60715b702d8e5abebe72d0bc42354239ef55c80 Mon Sep 17 00:00:00 2001 From: Nethma-k <111905294+Nethma-k@users.noreply.github.com> Date: Mon, 28 Jul 2025 15:54:52 +0530 Subject: [PATCH 11/31] Delete .github/workflows/cloudrunner --- .github/workflows/cloudrunner | 48 ----------------------------------- 1 file changed, 48 deletions(-) delete mode 100644 .github/workflows/cloudrunner diff --git a/.github/workflows/cloudrunner b/.github/workflows/cloudrunner deleted file mode 100644 index 4a3e557..0000000 --- a/.github/workflows/cloudrunner +++ /dev/null @@ -1,48 +0,0 @@ -name: 'Build and Deploy to Cloud Run' - -on: - push: - branches: - - main - -env: - PROJECT_ID: 'thermal-hour-467308-u4' - GAR_NAME: 'gh-demo' - REGION: 'us-central1' - SERVICE: 'gitactionnew' - -jobs: - deploy: - runs-on: ubuntu-latest - - steps: - - name: 'Checkout' - uses: actions/checkout@v4 - - - name: 'Authenticate to Google Cloud with SA Key' - uses: google-github-actions/auth@v2 - with: - credentials_json: '${{ secrets.ABC }}' - - - name: 'Set up gcloud CLI' - uses: google-github-actions/setup-gcloud@v2 - - - name: 'Docker Auth' - run: gcloud auth configure-docker "${{ env.REGION }}-docker.pkg.dev" - - - name: 'Build and Push Docker Image' - run: | - IMAGE="${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.GAR_NAME }}/${{ env.SERVICE }}:${{ github.sha }}" - docker build -t "$IMAGE" . - docker push "$IMAGE" - - - name: 'Deploy to Cloud Run' - id: deploy - uses: google-github-actions/deploy-cloudrun@v2 - with: - service: ${{ env.SERVICE }} - region: ${{ env.REGION }} - image: "${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.GAR_NAME }}/${{ env.SERVICE }}:${{ github.sha }}" - - - name: Show Deployed URL - run: echo ${{ steps.deploy.outputs.url }} From 1e5c3e9595ef82b2dbdb3a4eb0d47cf7d65474c6 Mon Sep 17 00:00:00 2001 From: Nethma-k <111905294+Nethma-k@users.noreply.github.com> Date: Mon, 28 Jul 2025 15:55:16 +0530 Subject: [PATCH 12/31] Create cloudrunner.yml --- .github/workflows/cloudrunner.yml | 48 +++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 .github/workflows/cloudrunner.yml diff --git a/.github/workflows/cloudrunner.yml b/.github/workflows/cloudrunner.yml new file mode 100644 index 0000000..4a3e557 --- /dev/null +++ b/.github/workflows/cloudrunner.yml @@ -0,0 +1,48 @@ +name: 'Build and Deploy to Cloud Run' + +on: + push: + branches: + - main + +env: + PROJECT_ID: 'thermal-hour-467308-u4' + GAR_NAME: 'gh-demo' + REGION: 'us-central1' + SERVICE: 'gitactionnew' + +jobs: + deploy: + runs-on: ubuntu-latest + + steps: + - name: 'Checkout' + uses: actions/checkout@v4 + + - name: 'Authenticate to Google Cloud with SA Key' + uses: google-github-actions/auth@v2 + with: + credentials_json: '${{ secrets.ABC }}' + + - name: 'Set up gcloud CLI' + uses: google-github-actions/setup-gcloud@v2 + + - name: 'Docker Auth' + run: gcloud auth configure-docker "${{ env.REGION }}-docker.pkg.dev" + + - name: 'Build and Push Docker Image' + run: | + IMAGE="${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.GAR_NAME }}/${{ env.SERVICE }}:${{ github.sha }}" + docker build -t "$IMAGE" . + docker push "$IMAGE" + + - name: 'Deploy to Cloud Run' + id: deploy + uses: google-github-actions/deploy-cloudrun@v2 + with: + service: ${{ env.SERVICE }} + region: ${{ env.REGION }} + image: "${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.GAR_NAME }}/${{ env.SERVICE }}:${{ github.sha }}" + + - name: Show Deployed URL + run: echo ${{ steps.deploy.outputs.url }} From 8c143e7befc92ffcaea4d090aa3b3332cedf2c4f Mon Sep 17 00:00:00 2001 From: Nethma-k <111905294+Nethma-k@users.noreply.github.com> Date: Mon, 28 Jul 2025 15:59:20 +0530 Subject: [PATCH 13/31] Update ci.yml --- .github/workflows/ci.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ef2904d..a301e44 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -23,6 +23,7 @@ jobs: run: | python -m pip install --upgrade pip pip install -r requirements.txt + pip install pytest - name: Run tests - run: pytest + run: python -m pytest # Change this line From f44ce7e3f3763891b3983fd0755335b8c8aae8df Mon Sep 17 00:00:00 2001 From: Nethma-k <111905294+Nethma-k@users.noreply.github.com> Date: Mon, 28 Jul 2025 16:01:57 +0530 Subject: [PATCH 14/31] Update requirements.txt --- requirements.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/requirements.txt b/requirements.txt index d78cc59..9f69ee3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ -Flask==1.1.2 -Flask-Migrate==2.5.3 -Flask-SQLAlchemy==2.4.4 + +Flask +Jinja2 From 9535625f3d7f8eae6884a9be100aff1fe06a380e Mon Sep 17 00:00:00 2001 From: Nethma-k <111905294+Nethma-k@users.noreply.github.com> Date: Mon, 28 Jul 2025 16:03:46 +0530 Subject: [PATCH 15/31] Update requirements.txt --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 9f69ee3..03a80e5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ - Flask Jinja2 +Flask-SQLAlchemy From 57858c3e55a5267b2f26086de1ea0bd78979e9b7 Mon Sep 17 00:00:00 2001 From: Nethma-k <111905294+Nethma-k@users.noreply.github.com> Date: Mon, 28 Jul 2025 16:05:26 +0530 Subject: [PATCH 16/31] Update requirements.txt --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index 03a80e5..7d3bae8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ Flask Jinja2 Flask-SQLAlchemy +Flask-Migrate From 31c065404029678693696527bbb65e5f51f0e44a Mon Sep 17 00:00:00 2001 From: Nethma-k <111905294+Nethma-k@users.noreply.github.com> Date: Mon, 28 Jul 2025 16:09:44 +0530 Subject: [PATCH 17/31] Update cloudrunner.yml --- .github/workflows/cloudrunner.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/cloudrunner.yml b/.github/workflows/cloudrunner.yml index 4a3e557..fbe92df 100644 --- a/.github/workflows/cloudrunner.yml +++ b/.github/workflows/cloudrunner.yml @@ -2,8 +2,9 @@ name: 'Build and Deploy to Cloud Run' on: push: - branches: - - main + branches: [ master ] + pull_request: + branches: [ master ] env: PROJECT_ID: 'thermal-hour-467308-u4' From aa27610b8a5efe90db723dc56d54c7e90b804998 Mon Sep 17 00:00:00 2001 From: Nethma-k <111905294+Nethma-k@users.noreply.github.com> Date: Mon, 28 Jul 2025 16:13:19 +0530 Subject: [PATCH 18/31] Update Dockerfile --- Dockerfile | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index d85316f..f222733 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,9 +9,14 @@ WORKDIR /sample-app COPY . /sample-app/ -RUN pip3 install -r requirements.txt && \ +# ... (previous lines of your Dockerfile) + +RUN pip3 install --no-cache-dir --upgrade pip && \ # Add this line + pip3 install -r requirements.txt && \ pip3 install -r requirements-server.txt +# ... (rest of your Dockerfile) + ENV LC_ALL="C.UTF-8" ENV LANG="C.UTF-8" From 8e118830c0ee4735f4657809ca94c6813b27660d Mon Sep 17 00:00:00 2001 From: Nethma-k <111905294+Nethma-k@users.noreply.github.com> Date: Mon, 28 Jul 2025 16:20:54 +0530 Subject: [PATCH 19/31] Update Dockerfile --- Dockerfile | 73 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 48 insertions(+), 25 deletions(-) diff --git a/Dockerfile b/Dockerfile index f222733..797f0e0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,25 +1,48 @@ -FROM ubuntu:18.04 - -RUN apt-get update && \ - apt-get -y upgrade && \ - DEBIAN_FRONTEND=noninteractive apt-get install -yq libpq-dev gcc python3.8 python3-pip && \ - apt-get clean - -WORKDIR /sample-app - -COPY . /sample-app/ - -# ... (previous lines of your Dockerfile) - -RUN pip3 install --no-cache-dir --upgrade pip && \ # Add this line - pip3 install -r requirements.txt && \ - pip3 install -r requirements-server.txt - -# ... (rest of your Dockerfile) - -ENV LC_ALL="C.UTF-8" -ENV LANG="C.UTF-8" - -EXPOSE 8000/tcp - -CMD ["/bin/sh", "-c", "flask db upgrade && gunicorn app:app -b 0.0.0.0:8000"] +# Use the official lightweight Python image. +# python:3.11-slim is a great choice for smaller image sizes. +FROM python:3.11-slim + +# Set environment variables for Python in Docker +# PYTHONDONTWRITEBYTECODE prevents Python from writing .pyc files to disk, reducing image size. +# PYTHONUNBUFFERED ensures that Python output is sent straight to the terminal without buffering. +ENV PYTHONDONTWRITEBYTECODE=1 +ENV PYTHONUNBUFFERED=1 + +# Set the working directory inside the container to /app. +# This is where your application code will reside. +WORKDIR /app + +# Expose port 8080. Cloud Run typically expects services to listen on this port. +EXPOSE 8080 + +# Install dependencies +# Copy requirements files first to leverage Docker's caching. +# If these files don't change, Docker can use a cached layer for dependency installation, speeding up builds. +COPY requirements.txt . +# Assuming you still have/need requirements-server.txt for Gunicorn or other server-specific tools. +COPY requirements-server.txt . + +# Install Python dependencies. +# `pip install --no-cache-dir --upgrade pip` ensures pip is up-to-date and avoids storing temporary cache files. +# `pip install -r ...` reads and installs packages listed in your requirements files. +RUN pip install --no-cache-dir --upgrade pip && \ + pip install -r requirements.txt && \ + pip install -r requirements-server.txt + +# Copy the rest of your application code into the container. +# This should be done after installing dependencies to maximize cache hits. +COPY . . + +# Copy the entrypoint script into the container and make it executable. +# This script will be run first when the container starts. +COPY entrypoint.sh /usr/local/bin/entrypoint.sh +RUN chmod +x /usr/local/bin/entrypoint.sh + +# Use the entrypoint script. +# This ensures that `flask db upgrade` runs first, then the command defined in CMD. +ENTRYPOINT ["/usr/local/bin/entrypoint.sh"] + +# Define the default command to run the Flask application using Gunicorn. +# 'app:app' assumes your Flask application instance is named 'app' within the 'app' module/package. +# '-b 0.0.0.0:8080' binds Gunicorn to all network interfaces on port 8080, matching the EXPOSE instruction. +CMD ["gunicorn", "app:app", "-b", "0.0.0.0:8080"] From 6535232afa295e4b411c4c74710473694b535379 Mon Sep 17 00:00:00 2001 From: Nethma-k <111905294+Nethma-k@users.noreply.github.com> Date: Mon, 28 Jul 2025 16:21:32 +0530 Subject: [PATCH 20/31] Create entrypoint.sh --- entrypoint.sh | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 entrypoint.sh diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..cdd049f --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,21 @@ +#!/bin/sh + +# This script will be executed when the Docker container starts. + +# Run Flask database migrations +# Ensure FLASK_APP is set correctly if your app entry point is not app.py +# If your Flask app instance is named 'app' inside an 'app' package (e.g., app/__init__.py), +# you might need FLASK_APP=app or rely on Flask's auto-discovery. +# If your main app file is app.py and the instance is 'app', it should work. +echo "Running database migrations..." +# Check if the 'app' module can be found for Flask CLI (optional but good for debugging) +if ! python -c "import app" &> /dev/null; then + echo "Error: Could not import 'app' module. Ensure it's in the root or FLASK_APP is set." + exit 1 +fi +flask db upgrade + +# Execute the main command passed to the container (e.g., gunicorn) +# This allows you to pass arguments like 'gunicorn app:app -b 0.0.0.0:8080' +# as the CMD in the Dockerfile, and it will be executed here. +exec "$@" From b4242684bdc6276dc39ad5f00319f7ecfffb11ad Mon Sep 17 00:00:00 2001 From: Nethma-k <111905294+Nethma-k@users.noreply.github.com> Date: Mon, 28 Jul 2025 16:26:10 +0530 Subject: [PATCH 21/31] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0af701b..badb205 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ ## Description -This sample Python REST API application was written for a tutorial on implementing Continuous Integration and Delivery pipelines. +This sample Python REST API application was written for a tutorial on implementing Continuous Integration and Delivery pipelines It demonstrates how to: From 29a6810d2250c9a0ca32b5c306ce74eaa6da08c6 Mon Sep 17 00:00:00 2001 From: Nethma-k <111905294+Nethma-k@users.noreply.github.com> Date: Mon, 28 Jul 2025 16:42:25 +0530 Subject: [PATCH 22/31] Update entrypoint.sh --- entrypoint.sh | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index cdd049f..24147ae 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,21 +1,25 @@ #!/bin/sh -# This script will be executed when the Docker container starts. +echo "Starting entrypoint script..." + +# Set FLASK_APP to 'app' (the package name). +# This is crucial for Flask CLI commands and Gunicorn to find your application instance. +export FLASK_APP=app -# Run Flask database migrations -# Ensure FLASK_APP is set correctly if your app entry point is not app.py -# If your Flask app instance is named 'app' inside an 'app' package (e.g., app/__init__.py), -# you might need FLASK_APP=app or rely on Flask's auto-discovery. -# If your main app file is app.py and the instance is 'app', it should work. echo "Running database migrations..." -# Check if the 'app' module can be found for Flask CLI (optional but good for debugging) -if ! python -c "import app" &> /dev/null; then - echo "Error: Could not import 'app' module. Ensure it's in the root or FLASK_APP is set." +# Execute migrations. Redirecting stderr to stdout (2>&1) ensures errors are logged to Cloud Logging. +# The 'set -e' (often implied by shebang or default shell behavior) will cause the script to exit +# immediately if 'flask db upgrade' fails, which is desired for failed deployments. +if flask db upgrade 2>&1; then + echo "Database migrations completed successfully." +else + echo "ERROR: Database migrations failed!" + # Exit with a non-zero status to indicate failure to Cloud Run. exit 1 fi -flask db upgrade -# Execute the main command passed to the container (e.g., gunicorn) -# This allows you to pass arguments like 'gunicorn app:app -b 0.0.0.0:8080' -# as the CMD in the Dockerfile, and it will be executed here. -exec "$@" +echo "Starting Gunicorn server..." +# Cloud Run injects the PORT environment variable (defaulting to 8080). +# Ensure Gunicorn binds to 0.0.0.0 and uses this PORT variable. +# The ${PORT:-8080} syntax provides a fallback to 8080 if PORT isn't set (e.g., for local testing). +exec gunicorn app:app -b 0.0.0.0:${PORT:-8080} From ce56fb8dd7916a30e7c66f87c2e250f6eecba50b Mon Sep 17 00:00:00 2001 From: Chamudi Siriwardhane <116910122+heittre@users.noreply.github.com> Date: Mon, 28 Jul 2025 16:42:45 +0530 Subject: [PATCH 23/31] Create cd.yml --- cd.yml | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 cd.yml diff --git a/cd.yml b/cd.yml new file mode 100644 index 0000000..621bba7 --- /dev/null +++ b/cd.yml @@ -0,0 +1,57 @@ +name: Deploy Flask App to VM + +on: + push: + branches: + - main # or your default branch + +jobs: + deploy: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Set up Python 3.x + uses: actions/setup-python@v4 + with: + python-version: '3.x' + + - name: Install dependencies (for lint/test, optional) + run: | + python -m venv venv + source venv/bin/activate + pip install --upgrade pip + pip install -r requirements.txt + + - name: Copy files to VM + uses: appleboy/scp-action@v0.1.5 + with: + host: ${{ secrets.VM_HOST }} + username: ${{ secrets.VM_USER }} + key: ${{ secrets.VM_SSH_KEY }} + port: 22 + source: "." + target: "/home/${{ secrets.VM_USER }}/ci-cd-tutorial-sample-app" + + - name: Run deploy commands on VM + uses: appleboy/ssh-action@v0.1.6 + with: + host: ${{ secrets.VM_HOST }} + username: ${{ secrets.VM_USER }} + key: ${{ secrets.VM_SSH_KEY }} + port: 22 + script: | + cd ~/ci-cd-tutorial-sample-app + # Activate virtual environment or create if missing + if [ ! -d "venv" ]; then + python3 -m venv venv + fi + source venv/bin/activate + pip install --upgrade pip + pip install -r requirements.txt + + # Restart Gunicorn (adjust service name or command as needed) + pkill gunicorn || true + nohup gunicorn --bind 0.0.0.0:8000 app:app > gunicorn.log 2>&1 & From ccab56ba01c37384e7736aa10edc27edebc764d2 Mon Sep 17 00:00:00 2001 From: Nethma-k <111905294+Nethma-k@users.noreply.github.com> Date: Mon, 28 Jul 2025 16:43:06 +0530 Subject: [PATCH 24/31] Update Dockerfile --- Dockerfile | 32 +++++++++++--------------------- 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/Dockerfile b/Dockerfile index 797f0e0..a868e48 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,15 +1,13 @@ # Use the official lightweight Python image. -# python:3.11-slim is a great choice for smaller image sizes. FROM python:3.11-slim # Set environment variables for Python in Docker -# PYTHONDONTWRITEBYTECODE prevents Python from writing .pyc files to disk, reducing image size. -# PYTHONUNBUFFERED ensures that Python output is sent straight to the terminal without buffering. -ENV PYTHONDONTWRITEBYTECODE=1 -ENV PYTHONUNBUFFERED=1 +ENV PYTHONDONTWRITEBYTECODE=1 # Prevents Python from writing .pyc files +ENV PYTHONUNBUFFERED=1 # Ensures Python output is sent immediately to the terminal +# Add /app to PYTHONPATH so Python can find your 'app' package +ENV PYTHONPATH=/app:$PYTHONPATH -# Set the working directory inside the container to /app. -# This is where your application code will reside. +# Set the working directory inside the container WORKDIR /app # Expose port 8080. Cloud Run typically expects services to listen on this port. @@ -17,32 +15,24 @@ EXPOSE 8080 # Install dependencies # Copy requirements files first to leverage Docker's caching. -# If these files don't change, Docker can use a cached layer for dependency installation, speeding up builds. COPY requirements.txt . -# Assuming you still have/need requirements-server.txt for Gunicorn or other server-specific tools. -COPY requirements-server.txt . +COPY requirements-server.txt . # Assuming you still have/need this file # Install Python dependencies. -# `pip install --no-cache-dir --upgrade pip` ensures pip is up-to-date and avoids storing temporary cache files. -# `pip install -r ...` reads and installs packages listed in your requirements files. RUN pip install --no-cache-dir --upgrade pip && \ pip install -r requirements.txt && \ pip install -r requirements-server.txt -# Copy the rest of your application code into the container. -# This should be done after installing dependencies to maximize cache hits. +# Copy the rest of your application code into the container COPY . . -# Copy the entrypoint script into the container and make it executable. -# This script will be run first when the container starts. +# Copy the entrypoint script and make it executable COPY entrypoint.sh /usr/local/bin/entrypoint.sh RUN chmod +x /usr/local/bin/entrypoint.sh # Use the entrypoint script. -# This ensures that `flask db upgrade` runs first, then the command defined in CMD. ENTRYPOINT ["/usr/local/bin/entrypoint.sh"] -# Define the default command to run the Flask application using Gunicorn. -# 'app:app' assumes your Flask application instance is named 'app' within the 'app' module/package. -# '-b 0.0.0.0:8080' binds Gunicorn to all network interfaces on port 8080, matching the EXPOSE instruction. -CMD ["gunicorn", "app:app", "-b", "0.0.0.0:8080"] +# The CMD provides default arguments to the ENTRYPOINT script. +# Since Gunicorn is started by entrypoint.sh, this can be empty or used for further arguments. +CMD [] From 6a22c0f5f62f872a1fa2d48fc628b81f92cab7f0 Mon Sep 17 00:00:00 2001 From: Chamudi Siriwardhane <116910122+heittre@users.noreply.github.com> Date: Mon, 28 Jul 2025 16:43:37 +0530 Subject: [PATCH 25/31] Create cd.yml --- .github/workflows/cd.yml | 57 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 .github/workflows/cd.yml diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml new file mode 100644 index 0000000..621bba7 --- /dev/null +++ b/.github/workflows/cd.yml @@ -0,0 +1,57 @@ +name: Deploy Flask App to VM + +on: + push: + branches: + - main # or your default branch + +jobs: + deploy: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Set up Python 3.x + uses: actions/setup-python@v4 + with: + python-version: '3.x' + + - name: Install dependencies (for lint/test, optional) + run: | + python -m venv venv + source venv/bin/activate + pip install --upgrade pip + pip install -r requirements.txt + + - name: Copy files to VM + uses: appleboy/scp-action@v0.1.5 + with: + host: ${{ secrets.VM_HOST }} + username: ${{ secrets.VM_USER }} + key: ${{ secrets.VM_SSH_KEY }} + port: 22 + source: "." + target: "/home/${{ secrets.VM_USER }}/ci-cd-tutorial-sample-app" + + - name: Run deploy commands on VM + uses: appleboy/ssh-action@v0.1.6 + with: + host: ${{ secrets.VM_HOST }} + username: ${{ secrets.VM_USER }} + key: ${{ secrets.VM_SSH_KEY }} + port: 22 + script: | + cd ~/ci-cd-tutorial-sample-app + # Activate virtual environment or create if missing + if [ ! -d "venv" ]; then + python3 -m venv venv + fi + source venv/bin/activate + pip install --upgrade pip + pip install -r requirements.txt + + # Restart Gunicorn (adjust service name or command as needed) + pkill gunicorn || true + nohup gunicorn --bind 0.0.0.0:8000 app:app > gunicorn.log 2>&1 & From 0a6af15ad02499a1ed8a95333a84c3d457b73fa7 Mon Sep 17 00:00:00 2001 From: Nethma-k <111905294+Nethma-k@users.noreply.github.com> Date: Mon, 28 Jul 2025 16:45:14 +0530 Subject: [PATCH 26/31] Update Dockerfile --- Dockerfile | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index a868e48..c58b2c3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,8 +2,10 @@ FROM python:3.11-slim # Set environment variables for Python in Docker -ENV PYTHONDONTWRITEBYTECODE=1 # Prevents Python from writing .pyc files -ENV PYTHONUNBUFFERED=1 # Ensures Python output is sent immediately to the terminal +# Prevents Python from writing .pyc files +ENV PYTHONDONTWRITEBYTECODE=1 +# Ensures Python output is sent immediately to the terminal +ENV PYTHONUNBUFFERED=1 # Add /app to PYTHONPATH so Python can find your 'app' package ENV PYTHONPATH=/app:$PYTHONPATH From 3cea696a332e1d91722ecb956a1d28142b69fe42 Mon Sep 17 00:00:00 2001 From: Chamudi Siriwardhane <116910122+heittre@users.noreply.github.com> Date: Mon, 28 Jul 2025 16:46:43 +0530 Subject: [PATCH 27/31] Create test.yml --- .github/workflows/test.yml | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 .github/workflows/test.yml diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..6831739 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,12 @@ +name: Test Workflow + +on: + push: + branches: [main] + +jobs: + test: + runs-on: ubuntu-latest + steps: + - name: Echo message + run: echo "✅ GitHub Actions are working!" From 2aef03e8a3c111245be1f47819111ce0661be7e2 Mon Sep 17 00:00:00 2001 From: Nethma-k <111905294+Nethma-k@users.noreply.github.com> Date: Mon, 28 Jul 2025 16:48:30 +0530 Subject: [PATCH 28/31] Update Dockerfile --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index c58b2c3..464ec29 100644 --- a/Dockerfile +++ b/Dockerfile @@ -18,7 +18,7 @@ EXPOSE 8080 # Install dependencies # Copy requirements files first to leverage Docker's caching. COPY requirements.txt . -COPY requirements-server.txt . # Assuming you still have/need this file +COPY requirements-server.txt . # Install Python dependencies. RUN pip install --no-cache-dir --upgrade pip && \ From 943df97ec3058a59eda51e1e52a5188a5a42e07a Mon Sep 17 00:00:00 2001 From: Chamudi Date: Mon, 28 Jul 2025 17:21:32 +0530 Subject: [PATCH 29/31] add changes --- .github/workflows/cd.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index 621bba7..982e3ed 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -3,7 +3,7 @@ name: Deploy Flask App to VM on: push: branches: - - main # or your default branch + - main # orr your default branch jobs: deploy: From 01047ba10a2b2b5c28bfd6c37cab3119217ecdc3 Mon Sep 17 00:00:00 2001 From: Chamudi Date: Mon, 28 Jul 2025 17:33:13 +0530 Subject: [PATCH 30/31] add changes --- tests/test_routes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_routes.py b/tests/test_routes.py index c8609b9..13fa81f 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -4,7 +4,7 @@ import json -# Add parent directory to path for import +# Add parent directory to path for import #comment sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), os.pardir)) from app import app, db From e16a0731c11b47cee1dd9a9623447c0d6bda97ba Mon Sep 17 00:00:00 2001 From: Chamudi Siriwardhane <116910122+heittre@users.noreply.github.com> Date: Mon, 28 Jul 2025 19:28:45 +0530 Subject: [PATCH 31/31] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index badb205..9b809b6 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ # CD/CI Tutorial Sample Application -## Description +## Descriptio This sample Python REST API application was written for a tutorial on implementing Continuous Integration and Delivery pipelines