diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..c2e9c87 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,10 @@ +.git +.gitignore +*.md +LICENSE +kubeconfig-*.yaml +k8s/ +.gitea/ +__pycache__ +*.pyc +.env diff --git a/.gitea/workflows/ci.yaml b/.gitea/workflows/ci.yaml new file mode 100644 index 0000000..f458162 --- /dev/null +++ b/.gitea/workflows/ci.yaml @@ -0,0 +1,55 @@ +name: CI/CD Pipeline + +on: + push: + branches: [main] + +env: + REGISTRY: git.fpmoz.sum.ba + IMAGE_NAME: blazp04/distribuirani + +jobs: + build-and-push: + runs-on: ubuntu-latest + steps: + - name: Checkout koda + uses: actions/checkout@v4 + + - name: Postavi Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Prijava na Gitea Container Registry + uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY }} + username: ${{ secrets.REGISTRY_USER }} + password: ${{ secrets.REGISTRY_PASSWORD }} + + - name: Generiraj tagove za image + id: meta + run: | + SHORT_SHA=$(echo "${{ gitea.sha }}" | cut -c1-7) + echo "short_sha=${SHORT_SHA}" >> "$GITHUB_OUTPUT" + echo "tags=${REGISTRY}/${IMAGE_NAME}:${SHORT_SHA},${REGISTRY}/${IMAGE_NAME}:latest" >> "$GITHUB_OUTPUT" + + - name: Build i push Docker image + uses: docker/build-push-action@v5 + with: + context: . + push: true + tags: ${{ steps.meta.outputs.tags }} + cache-from: type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest + cache-to: type=inline + + - name: Ažuriraj image tag u deployment manifestu + run: | + SHORT_SHA=${{ steps.meta.outputs.short_sha }} + sed -i "s|image: ${REGISTRY}/${IMAGE_NAME}:.*|image: ${REGISTRY}/${IMAGE_NAME}:${SHORT_SHA}|" k8s/distribuirani/deployment.yaml + cat k8s/distribuirani/deployment.yaml + + - name: Commit i push ažuriranog manifesta + run: | + git config user.name "Gitea Actions" + git config user.email "actions@git.fpmoz.sum.ba" + git add k8s/distribuirani/deployment.yaml + git diff --cached --quiet && echo "Nema promjena" || (git commit -m "ci: update image tag to ${{ steps.meta.outputs.short_sha }} [skip ci]" && git push) diff --git a/.gitignore b/.gitignore index f54494f..78cbd96 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,5 @@ -kubeconfig-Blazp04.yaml +kubeconfig-*.yaml +.env +__pycache__/ +*.pyc +*.pyo diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..811df7c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,24 @@ +# ---------- build stage ---------- +FROM python:3.12-slim AS builder + +WORKDIR /build +COPY app/requirements.txt . +RUN pip install --no-cache-dir --prefix=/install -r requirements.txt + +# ---------- runtime stage ---------- +FROM python:3.12-slim + +LABEL maintainer="blazp04" +LABEL org.opencontainers.image.source="https://git.fpmoz.sum.ba/blazp04/distribuirani" + +RUN adduser --disabled-password --no-create-home appuser + +WORKDIR /app +COPY --from=builder /install /usr/local +COPY app/ . + +USER appuser + +EXPOSE 8000 + +CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"] diff --git a/app/main.py b/app/main.py new file mode 100644 index 0000000..fc3dd5f --- /dev/null +++ b/app/main.py @@ -0,0 +1,123 @@ +import os +import datetime +from fastapi import FastAPI +from fastapi.responses import HTMLResponse + +app = FastAPI(title="Distribuirani Sustavi - FPMOZ") + +SERVICE_NAME = os.getenv("SERVICE_NAME", "distribuirani-service") +LOG_LEVEL = os.getenv("LOG_LEVEL", "info") +WELCOME_MSG = os.getenv("WELCOME_MSG", "Pozdrav iz FPMOZ k3s clustera!") + + +@app.get("/", response_class=HTMLResponse) +def root(): + return f""" + +
+ + +{WELCOME_MSG}
+