diff --git a/.gitea/workflows/deploy-questions.yml b/.gitea/workflows/deploy-questions.yml new file mode 100644 index 0000000..36bbb7c --- /dev/null +++ b/.gitea/workflows/deploy-questions.yml @@ -0,0 +1,81 @@ +name: Deploy STEP3 Questions API + +on: + push: + branches: + - main + workflow_dispatch: {} + +jobs: + deploy: + runs-on: self-hosted + + env: + IMAGE_NAME: step3-questions:latest + IMAGE_TAR: /tmp/step3-questions.tar + NODES: "192.168.86.72 192.168.86.73 192.168.86.74" + + steps: + - name: Checkout repo + uses: actions/checkout@v4 + + - name: Build Docker image + run: | + cd api/QuestionsApi + docker build -t "${IMAGE_NAME}" . + + - name: Save Docker image to TAR + run: | + docker save "${IMAGE_NAME}" -o "${IMAGE_TAR}" + + - name: Copy TAR to nodes + run: | + for node in ${NODES}; do + echo "Copying image tar to $node ..." + scp -o StrictHostKeyChecking=no "${IMAGE_TAR}" hz@"$node":/tmp/step3-questions.tar + done + + - name: Import image on nodes + run: | + for node in ${NODES}; do + echo "Importing image on $node ..." + ssh -o StrictHostKeyChecking=no hz@"$node" "sudo ctr -n k8s.io images import /tmp/step3-questions.tar" + done + + - name: Clean TAR from nodes + run: | + for node in ${NODES}; do + echo "Removing image tar on $node ..." + ssh -o StrictHostKeyChecking=no hz@"$node" "rm -f /tmp/step3-questions.tar" + done + + - name: Clean TAR on runner + run: | + rm -f "${IMAGE_TAR}" + + - name: Write kubeconfig from secret + env: + KUBECONFIG_CONTENT: ${{ secrets.KUBECONFIG }} + run: | + mkdir -p /tmp/kube + printf '%s\n' "$KUBECONFIG_CONTENT" > /tmp/kube/config + + - name: Create namespace if missing + env: + KUBECONFIG: /tmp/kube/config + run: | + kubectl create namespace step3-questions --dry-run=client -o yaml | kubectl apply -f - + + - name: Apply Questions deployment & service + env: + KUBECONFIG: /tmp/kube/config + run: | + kubectl apply -f api/QuestionsApi/k8s/deployment.yaml -n step3-questions + kubectl apply -f api/QuestionsApi/k8s/service.yaml -n step3-questions + + - name: Restart Questions deployment + env: + KUBECONFIG: /tmp/kube/config + run: | + kubectl rollout restart deployment/step3-questions -n step3-questions + kubectl rollout status deployment/step3-questions -n step3-questions diff --git a/.github/workflows/questions-api.yml b/.github/workflows/questions-api.yml deleted file mode 100644 index 27bc015..0000000 --- a/.github/workflows/questions-api.yml +++ /dev/null @@ -1,65 +0,0 @@ -name: Questions API - -on: - push: - branches: [main] - paths: - - "api/QuestionsApi/**" - - ".github/workflows/questions-api.yml" - pull_request: - paths: - - "api/QuestionsApi/**" - - ".github/workflows/questions-api.yml" - -env: - DOTNET_VERSION: "8.0.x" - IMAGE_NAME: ghcr.io/${{ github.repository }}/questions-api - -jobs: - build: - runs-on: ubuntu-latest - permissions: - contents: read - packages: write - - steps: - - name: Checkout - uses: actions/checkout@v4 - - - name: Setup .NET - uses: actions/setup-dotnet@v4 - with: - dotnet-version: ${{ env.DOTNET_VERSION }} - - - name: Restore - run: dotnet restore api/QuestionsApi/QuestionsApi.csproj - - - name: Build - run: dotnet build api/QuestionsApi/QuestionsApi.csproj --configuration Release --no-restore - - - name: Publish - run: dotnet publish api/QuestionsApi/QuestionsApi.csproj --configuration Release --no-build --output publish/questions-api - - - name: Upload publish artifact - uses: actions/upload-artifact@v4 - with: - name: questions-api-publish - path: publish/questions-api - - - name: Log in to GitHub Container Registry - if: github.event_name == 'push' - uses: docker/login-action@v3 - with: - registry: ghcr.io - username: ${{ github.actor }} - password: ${{ secrets.GITHUB_TOKEN }} - - - name: Build and push Docker image - if: github.event_name == 'push' - uses: docker/build-push-action@v6 - with: - context: api/QuestionsApi - push: true - tags: | - ${{ env.IMAGE_NAME }}:${{ github.sha }} - ${{ env.IMAGE_NAME }}:latest diff --git a/api/QuestionsApi/Dockerfile b/api/QuestionsApi/Dockerfile index cbfec21..7e4a86e 100644 --- a/api/QuestionsApi/Dockerfile +++ b/api/QuestionsApi/Dockerfile @@ -11,9 +11,9 @@ FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS final WORKDIR /app COPY --from=build /app/publish . -ENV ASPNETCORE_URLS=http://+:8080 \ +ENV ASPNETCORE_URLS=http://+:5008 \ ASPNETCORE_ENVIRONMENT=Production -EXPOSE 8080 +EXPOSE 5008 ENTRYPOINT ["dotnet", "QuestionsApi.dll"] diff --git a/api/QuestionsApi/k8s/deployment.yaml b/api/QuestionsApi/k8s/deployment.yaml new file mode 100644 index 0000000..849b6d1 --- /dev/null +++ b/api/QuestionsApi/k8s/deployment.yaml @@ -0,0 +1,28 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: step3-questions + labels: + app: step3-questions +spec: + replicas: 2 + selector: + matchLabels: + app: step3-questions + template: + metadata: + labels: + app: step3-questions + spec: + containers: + - name: step3-questions + image: step3-questions:latest + imagePullPolicy: IfNotPresent + ports: + - containerPort: 5008 + readinessProbe: + httpGet: + path: /healthz + port: 5008 + initialDelaySeconds: 5 + periodSeconds: 10 diff --git a/api/QuestionsApi/k8s/service.yaml b/api/QuestionsApi/k8s/service.yaml new file mode 100644 index 0000000..6e0683b --- /dev/null +++ b/api/QuestionsApi/k8s/service.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Service +metadata: + name: step3-questions + labels: + app: step3-questions +spec: + selector: + app: step3-questions + type: NodePort + ports: + - name: http + port: 80 + targetPort: 5008 + nodePort: 30088