GithubHelp home page GithubHelp logo

cicd_example's Introduction

GitHub CI/CD Example

This repository demonstrates a simple example of CI/CD (Continuous Integration/Continuous Deployment) using GitHub Actions. We have a basic FastAPI application that serves as an API server. The focus is on illustrating how to set up a CI/CD pipeline to automatically integrate your codebase and run unit tests for your functions.

๐Ÿƒ Workflow

โญ Prerequisites

Before you get started, make sure you have the following:

  • Git installed on your machine.
  • GitHub and Docker Hub accounts.
  • Basic knowledge of Python programming.
  • Configured AWS EC2 Instance (see below for setup details).
    sudo yum update
    sudo yum install docker
    sudo usermod -a -G docker ec2-user
    sudo systemctl enable docker
    sudo systemctl start docker
    sudo systemctl status docker
    
    • Install docker-compose
    sudo curl -L https://github.com/docker/compose/releases/download/1.22.0/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
    
    sudo chmod +x /usr/local/bin/docker-compose
    
    sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
    
    • Set inbound rules
      • Type: Custom TCP
      • Protocol: TCP
      • Port Range: 8000
      • Source: Anywhere (0.0.0.0/0, ::/0)

๐Ÿš€ Getting Started

  1. Fork or Copy the Repository

    • Click the Fork button.

    • Set repository's name & click Create fork button.

  2. Set Secrets & enable Actions in GitHub

    • Create secrets for CI/CD Actions

    • Add secrets: DOCKERHUB_USERNAME, DOCKERHUB_PASSWORD, EC2_HOST_NAME, EC2_USER_NAME & SSH_PRIVATE_KEY

    • Enable Actions for CI/CD

  3. Clone to Your Local Machine

    • Clone the repository to your local development environment using Git.
    git clone https://github.com/{Owner_name}/cicd_example.git
    
  4. Repository Description

  5. Making Changes in app/main.py

    • Add a New API: You can add new API endpoints to the FastAPI application.
    def gender():
        return 'Man'
    
    @app.get("/v1/gender",
                description='Get gender',
                responses={
                            400: {"description": 'get_gender fail'},
                            500: {"description": 'Internal server error'}
                }
            )
    async def get_gender():
        gender_value = gender()
        return JSONResponse(status_code=200, content={"data": gender_value})
    • Edit an Existing API: Be aware that editing an existing API might cause the CI/CD pipeline to fail if the changes do not pass the unit tests.
    def age():
        return 28 # original 18
  6. Push Changes to GitHub

    • Commit and push your changes to GitHub. This will trigger the CI/CD pipeline defined in GitHub Actions.
    git add .
    git commit -m "{your commit message}"
    git push
    
  7. Check the CI/CD Pipeline

    • Go to the 'Actions' tab in your GitHub repository to view the status and logs of the CI/CD pipeline.

  8. Check API Server in AWS EC2

    • Go to address: http://{ Public IPv4 address }:8000/docs

  9. Launch the API Server in Local(Optional)

    • Docker & Docker-Compose installed on your machine.
    • Go to your DockerHub & sign in.
    • Copy the image name with tag and paste it to docker-compose.yaml

    • Launch api server
    docker-compose up -d
    
    docker-compose down
    

cicd_example's People

Contributors

gallonshih avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.