Skip to content

patch: added piccolo fixture load #5

patch: added piccolo fixture load

patch: added piccolo fixture load #5

Workflow file for this run

name: Deploy Backend to Homelab k3s
on:
push:
branches:
- main
env:
REGISTRY: ghcr.io
IMAGE_NAME: parsa-asgari/backend-homelab
jobs:
test:
runs-on: ubuntu-latest
# Service containers to run with `container-job`
services:
# Label used to access the service container
db:
# Docker Hub image
image: postgres:14.4-alpine
# Provide the password for postgres
env:
POSTGRES_PASSWORD: postgres
# Set health checks to wait until postgres has started
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
# Maps tcp port 5432 on service container to the host
- 5432:5432
steps:
# Step 1: Checkout code
- name: Checkout code
uses: actions/checkout@v3
# Step 2: Set up Python
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: '3.10'
# Step 3: Install dependencies
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
# Step 4: Run tests
- name: Run migrations & tests
run: piccolo migrations forwards all && pytest
env:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
POSTGRES_HOST: localhost
POSTGRES_PORT: 5432
POSTGRES_DB: postgres
build_and_push_image:
runs-on: ubuntu-latest
needs: [test]
permissions:
contents: write
packages: write
id-token: write
attestations: write
outputs:
version: ${{ steps.version.outputs.version }}
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Get semantic version
id: version
uses: paulhatch/semantic-version@v3
with:
token: ${{ secrets.GITHUB_TOKEN }}
major_pattern: '^feat!:'
minor_pattern: '^feat:'
patch_pattern: '^fix:'
default: patch
bump: auto
- name: Create Git tag
run: |
if git rev-parse ${{ steps.version.outputs.version }} >/dev/null 2>&1; then
echo "Tag ${{ steps.version.outputs.version }} already exists locally."
else
git tag ${{ steps.version.outputs.version }}
fi
if git ls-remote --tags origin | grep -q "refs/tags/${{ steps.version.outputs.version }}$"; then
echo "Tag ${{ steps.version.outputs.version }} already exists in remote."
else
git push origin ${{ steps.version.outputs.version }}
fi
- uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
- name: Build and push Docker image
id: push
uses: docker/build-push-action@v6
with:
context: .
push: true
tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.version.outputs.version }}
labels: ${{ steps.meta.outputs.labels }}
load: false
create_github_release:
runs-on: ubuntu-latest
needs: [build_and_push_image]
permissions:
contents: write
steps:
- name: Create GitHub Release
uses: actions/create-release@v1
with:
tag_name: ${{ needs.build_and_push_image.outputs.version }}
release_name: Release ${{ needs.build_and_push_image.outputs.version }}
body: |
New release with Docker image version ${{ needs.build_and_push_image.outputs.version }} deployed to Kubernetes.
draft: false
prerelease: false
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
deploy:
runs-on: ubuntu-latest
needs: [build_and_push_image, create_github_release]
permissions:
contents: write
steps:
- name: Set up Kubernetes kubeconfig
run: |
echo "${{ secrets.KUBECONFIG_FILE }}" > kubeconfig
chmod 600 kubeconfig
env:
KUBECONFIG_FILE: ${{ secrets.KUBECONFIG_FILE }}
- name: Generate docker compose with version
uses: rondefreitas/[email protected]
with:
template: .kubernetes/backend-homelab-k8s-manifest.yml.j2
output_file: backend-homelab-k8s-manifest.yml
strict: true
variables: |
image=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ needs.build_and_push_image.outputs.version }}
- name: Deploy to Kubernetes
uses: azure/setup-kubectl@v3
- name: Apply Kubernetes manifests
run: |
sed -i "s|image:.*|image: ${{ secrets.DOCKER_USERNAME }}/my-app:${{ steps.version.outputs.version }}|" backend-homelab-k8s-manifest.yml
kubectl apply -f backend-homelab-k8s-manifest.yml --kubeconfig=kubeconfig
env:
KUBECONFIG: ${{ secrets.KUBECONFIG_FILE }}