Project-Stage-Academy / UA1244_beta

1 stars 0 forks source link

Setup CI/CD workflow #58

Open mehalyna opened 3 weeks ago

mehalyna commented 3 weeks ago

Task Description:

We need to set up a Continuous Integration and Continuous Deployment (CI/CD) workflow for the Django project to automate the processes of testing, building, and deploying the application. The CI/CD pipeline will ensure code quality, automate deployment, and facilitate quick iteration cycles by integrating the project with GitHub Actions.


Objectives:

  1. Automate Testing:

    • Set up automated testing for the Django project on every push to the main branch and pull request (PR).
    • Run tests for different components of the project such as users, profiles, projects, communications, and dashboard.
  2. Run Linting and Code Quality Checks:

    • Set up linters (e.g., flake8) to check code quality.
    • Ensure that Python code adheres to PEP8 standards.
  3. Automate Docker Image Build:

    • Build a Docker image for the Django project during the CI process to ensure it works as expected in a containerized environment.
  4. Deploy to Production (CD):

    • Once tests and linting pass, trigger automatic deployment to the production environment using Kubernetes or another hosting provider (e.g., Heroku, AWS, or DigitalOcean).
    • Optionally, create a staging environment for testing before the production release.

Steps to Implement:

  1. Step 1: Set Up GitHub Actions for CI/CD:

    • Create a .github/workflows/ directory in the root of the project to store the GitHub Actions workflow files.
    • Create a YAML file in this directory for the CI/CD pipeline (e.g., ci-cd.yml).

    Example structure:

    projectname/
    ├── .github/
    │   ├── workflows/
    │       ├── ci-cd.yml
  2. Step 2: Define the CI Workflow (GitHub Actions YAML):

    • Configure the pipeline to run tests, lint code, and build a Docker image for the Django project.
    • Ensure the workflow triggers on every push to the main branch and for pull requests.

    Example ci-cd.yml:

    name: Django CI/CD Pipeline
    
    on:
     push:
       branches:
         - main
     pull_request:
       branches:
         - main
    
    jobs:
     test:
       runs-on: ubuntu-latest
    
       services:
         db:
           image: postgres:12
           env:
             POSTGRES_USER: postgres
             POSTGRES_PASSWORD: postgres
             POSTGRES_DB: test_db
           ports:
             - 5432:5432
           options: >-
             --health-cmd="pg_isready -U postgres"
             --health-interval=10s
             --health-timeout=5s
             --health-retries=5
    
       steps:
         - name: Checkout code
           uses: actions/checkout@v2
    
         - name: Set up Python
           uses: actions/setup-python@v2
           with:
             python-version: '3.9'
    
         - name: Install dependencies
           run: |
             python -m pip install --upgrade pip
             pip install -r requirements.txt
    
         - name: Run Migrations
           env:
             DJANGO_SETTINGS_MODULE: projectname.settings
           run: |
             python manage.py migrate
    
         - name: Run Tests
           env:
             DJANGO_SETTINGS_MODULE: projectname.settings
           run: |
             python manage.py test
    
         - name: Run Linting (flake8)
           run: |
             pip install flake8
             flake8 .
    
     build:
       runs-on: ubuntu-latest
       needs: test
    
       steps:
         - name: Checkout code
           uses: actions/checkout@v2
    
         - name: Build Docker Image
           run: |
             docker build -t your-dockerhub-username/projectname:latest .
    
         - name: Push Docker Image
           run: |
             docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD
             docker push your-dockerhub-username/projectname:latest
           env:
             DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
             DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
    
     deploy:
       runs-on: ubuntu-latest
       needs: build
    
       steps:
         - name: Deploy to Production
           run: |
             # Add your deployment steps here (e.g., Kubernetes, Heroku, AWS Elastic Beanstalk, etc.)
             kubectl apply -f k8s/deployment.yaml
             kubectl apply -f k8s/service.yaml
  3. Step 3: Set Up Secrets for Deployment:

    • Store your sensitive information such as DockerHub credentials or Kubernetes credentials as GitHub secrets.
    • You can set secrets in the GitHub repository under Settings > Secrets.

    Example secrets:

    • DOCKER_USERNAME
    • DOCKER_PASSWORD
    • KUBERNETES_CONFIG (for Kubernetes deployment)
  4. Step 4: Linting (Code Quality):

    • Ensure the project adheres to Python best practices by running flake8 in the CI pipeline.
    • Update the project’s requirements.txt to include flake8 as a dependency.
  5. Step 5: Docker Build:

    • The CI process will build a Docker image for the Django app to ensure it can run properly in a containerized environment.
    • The image will be pushed to DockerHub (or another container registry) for deployment.
  6. Step 6: Deploy to Production (Optional Staging Environment):

    • Once all tests and linting have passed, the deployment step will deploy the application to a Kubernetes cluster (or another platform like Heroku).
    • Ensure the deployment step uses kubectl or any other necessary tools for automating the deployment process.

Deliverables:

  1. GitHub Actions Workflow (ci-cd.yml):

    • A fully functional CI/CD pipeline that:
      • Runs automated tests on every push or PR.
      • Lints the code for quality.
      • Builds and pushes a Docker image.
      • Deploys the project to the production (or staging) environment.
  2. Secrets Management:

    • Ensure sensitive information (like DockerHub credentials, Kubernetes configuration) is managed through GitHub Secrets.
  3. Test Automation:

    • Make sure that the Django project’s test suite is comprehensive and runs without errors in the pipeline.
  4. Docker Image:

    • The Docker image for the project is built and pushed to a container registry (DockerHub or any other container registry).
  5. Production Deployment:

    • The app should be automatically deployed to the Kubernetes cluster (or any other deployment target) after successful tests and image builds.

Notes: