Skip to content

Commit

Permalink
feat(ci): add GitHub Actions workflows for CI/CD and update Docker im…
Browse files Browse the repository at this point in the history
…age configuration
  • Loading branch information
shba007 committed Dec 22, 2024
1 parent 8d69c93 commit 00bb559
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 40 deletions.
122 changes: 122 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
name: Flask CD
run-name: ${{ github.ref_name }}

on:
push:
tags:
- 'v*.*.*'

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

env:
REPO: ${{ github.repository }}
REGISTRY: ${{ vars.REGISTRY }}
USERNAME: ${{ vars.USERNAME }}
PORT: ${{ vars.PORT }}
PYTHON_ENV: production

jobs:
build:
runs-on: ubuntu-latest

permissions:
contents: read
packages: write
attestations: write
id-token: write

outputs:
image_name: ${{ steps.metadata.outputs.image_name }}
image_title: ${{ steps.metadata.outputs.image_title }}
buildtime: ${{ steps.metadata.outputs.buildtime }}
version: ${{ steps.metadata.outputs.version }}

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ env.USERNAME }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.REPO }}
tags: |
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=sha,prefix=sha-,format=short
- name: Set tags as environment variable
id: metadata
run: |
echo "Setting metadata to env variables"
echo "image_name=${{ fromJSON(steps.meta.outputs.json).tags[0] }}" >> $GITHUB_OUTPUT
echo "image_title=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.title'] }}" >> $GITHUB_OUTPUT
echo "buildtime=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.created'] }}" >> $GITHUB_OUTPUT
echo "version=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.version'] }}" >> $GITHUB_OUTPUT
- name: Build and push Docker image
id: push
uses: docker/build-push-action@v6
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: |
VERSION=${{ steps.metadata.outputs.version }}
BUILD_TIME=${{ steps.metadata.outputs.buildtime }}
cache-from: type=gha
cache-to: type=gha,mode=max

- name: Generate artifact attestation
uses: actions/attest-build-provenance@v1
with:
subject-name: ${{ env.REGISTRY }}/${{ env.REPO }}
subject-digest: ${{ steps.push.outputs.digest }}
push-to-registry: false

deploy:
needs: build
runs-on: ubuntu-latest

steps:
- name: Deploy to VPS
uses: appleboy/ssh-action@v1.1.0
with:
host: ${{ secrets.VPS_HOST }}
username: ${{ secrets.VPS_USERNAME }}
password: ${{ secrets.VPS_PASSWORD }}
script: |
# Pull new image
docker pull ${{ needs.build_web.outputs.image_name }}
# Stop the existing container
docker stop ${{ needs.build_web.outputs.image_title }} || true
docker rm ${{ needs.build_web.outputs.image_title }} || true
# Create and start new container
docker run --detach \
--name ${{ needs.build_web.outputs.image_title }} \
--restart unless-stopped \
--mount type=bind,source=$(pwd)/${{ needs.build_web.outputs.image_title }}/static,target=/app/static,readonly \
--env-file ~/${{ needs.build_web.outputs.image_title }}/.env.prod \
-p ${{ env.PORT }}:3000 \
${{ needs.build_web.outputs.image_name }}
# Clean up unused images
docker image prune -f
51 changes: 51 additions & 0 deletions .github/workflows/integrate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: Flask CI

on:
push:
branches:
- develop

jobs:
build:
runs-on: ubuntu-latest

permissions:
contents: write
pull-requests: write

steps:
- name: Checkout Repo
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Setup uv
uses: astral-sh/setup-uv@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version-file: '.python-version'

- name: Install Dependencies
run: uv sync

# - name: Build Artifacts
# run: uv build

# - name: Run Tests
# run: uv test

- name: Lint Code
run: uv run task lint || echo "Linting failed, but continuing"

- name: Format Code
run: uv run task format

- name: Create Release Pull Request
uses: changesets/action@v1
with:
title: 'chore(release): update version'
commit: 'chore(release): update version'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: 'unai'
services:
api:
build: .
image: "ghcr.io/shba007/unai-api:latest"
env_file:
- .env.prod
ports:
Expand Down
Loading

0 comments on commit 00bb559

Please sign in to comment.