Skip to content

ci/cd updated

ci/cd updated #57

Workflow file for this run

name: CI Pipeline
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
# Build Stage
build:
name: Build and Push Docker Image to Docker Hub
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Docker Login
env:
DOCKER_HUB_USERNAME: ${{ secrets.DOCKER_HUB_USERNAME }}
DOCKER_HUB_ACCESS_TOKEN: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}
run: |
echo $DOCKER_HUB_ACCESS_TOKEN | docker login --username $DOCKER_HUB_USERNAME --password-stdin
- name: Build and Push Docker image
run: |
docker buildx build \
--platform linux/amd64 \
--file development/Dockerfile.prod \
--tag ${{ secrets.DOCKER_HUB_USERNAME }}/myapp:latest \
--push \
.
# Test Stage
test:
name: Test and Lint
runs-on: ubuntu-latest
needs: build # Wait for build stage to complete
services:
postgres:
image: postgres:latest
env:
POSTGRES_DB: users
POSTGRES_USER: runner
POSTGRES_PASSWORD: runner
ports:
- 5432:5432
options: >-
--health-cmd="pg_isready -U runner"
--health-interval=10s
--health-timeout=5s
--health-retries=5
env:
DATABASE_URL: postgresql://runner:runner@localhost:5432/users
DJANGO_ALLOWED_HOSTS: 'localhost 127.0.0.1 [::1]'
SECRET_KEY: ${{ secrets.SECRET_KEY }}
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.12' # Adjust as needed
- name: Install dependencies
run: |
pip install -r app/requirements.txt
- name: Run tests with Pytest
run: |
cd app
pytest -p no:warnings --cov=.
- name: Run Black
run: |
cd app
black --check . --exclude 'movies/migrations'
- name: Run Ruff (including isort)
run: |
cd app
ruff check . --fix --exclude 'tests/*,movies/migrations/*,tasks.py,drf_project/settings.py'
# Release Stage
release:
name: Release Docker Image
runs-on: ubuntu-latest
needs: test # Wait for test job to complete
steps:
- name: Docker Login
env:
DOCKER_HUB_USERNAME: ${{ secrets.DOCKER_HUB_USERNAME }}
DOCKER_HUB_ACCESS_TOKEN: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}
run: |
echo $DOCKER_HUB_ACCESS_TOKEN | docker login --username $DOCKER_HUB_USERNAME --password-stdin
- name: Pull Docker image
run: |
docker pull ${{ secrets.DOCKER_HUB_USERNAME }}/myapp:latest
- name: Deploy to Heroku
run: |
heroku container:push web --app ${{ secrets.HEROKU_APP_NAME }}
heroku container:release web --app ${{ secrets.HEROKU_APP_NAME }}