8000 GitHub - shyambista1/test: test · GitHub
[go: up one dir, main page]

Skip to content
shyambista1Public

About

test

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

9 Commits

Folders and files

Repository files navigation

test

test

CI/CD Setup

This project uses GitHub Actions to automatically build and deploy the Flask application to an AWS EC2 instance. Deployment is triggered on every push to the main branch.

Prerequisites

Before you can use this CI/CD setup, ensure you have the following:

  1. AWS Account: An active AWS account with an EC2 instance configured.
    • Ensure your EC2 instance has Docker installed and the user specified in EC2_USERNAME can run Docker commands (e.g., is part of the docker group).
    • The EC2 instance's security group must allow inbound traffic on port 80 (or the host port you map in docker-compose.yml or the GitHub Actions workflow).
  2. Docker Hub Account: A Docker Hub account to store the built Docker images.
  3. Docker (Local): Docker installed on your local machine if you wish to build and test the application locally.

GitHub Secrets Configuration

The CI/CD workflow relies on several secrets that must be configured in your GitHub repository settings. Go to Settings > Secrets and variables > Actions and add the following repository secrets:

  • DOCKERHUB_USERNAME: Your Docker Hub username.
  • DOCKERHUB_TOKEN: Your Docker Hub access token or password. This token needs permissions to push images to your Docker Hub repository.
  • EC2_HOST: The public hostname or IP address of your EC2 instance.
  • EC2_USERNAME: The username for SSHing into your EC2 instance (e.g., ubuntu, ec2-user).
  • EC2_PRIVATE_KEY: The private SSH key (contents of the .pem file) for authenticating to your EC2 instance.

Workflow Placeholders

You need to customize the GitHub Actions workflow file (.github/workflows/deploy.yml) with your specific details. Look for the following placeholders and replace them:

  1. Docker Image Name:

    • In the build-and-push job, under Build and push Docker image step, change tags: your-dockerhub-username/your-repo-name:latest to your Docker Hub username and desired image name (e.g., johndoe/my-flask-app:latest).
    • In the deploy-to-ec2 job, within the script section, change docker pull your-dockerhub-username/your-repo-name:latest to match the image name you set above.
    • Also in the deploy-to-ec2 job script, change docker run ... your-dockerhub-username/your-repo-name:latest to match the image name.
  2. EC2 Container Name:

    • In the deploy-to-ec2 job, within the script section, replace all instances of your-app-container with the name you want to use for the Docker container running on your EC2 instance (e.g., my-flask-app-container). This affects docker stop, docker rm, and docker run --name.

Manual Build and Run (Local)

You can build and run the application locally using Docker before pushing changes or for development purposes.

Using Docker:

  1. Build the image:

    docker build -t my-flask-app .

    (Replace my-flask-app with your preferred image name)

  2. Run the container:

    docker run -p 5000:5000 my-flask-app

    This will run the app, and it will be accessible at http://localhost:5000. The Flask app inside the container runs on port 5000, and we map it to port 5000 on the host.

Using Docker Compose:

Docker Compose uses the docker-compose.yml file which is configured to map host port 80 to container port 5000.

  1. Build and run the services:

    docker-compose up --build

    The app will be accessible at http://localhost:80.

  2. Run in detached mode (in the background):

    docker-compose up -d
  3. Stop the services:

    docker-compose down

Deployment Process

When changes are pushed to the main branch of your GitHub repository:

  1. The GitHub Actions workflow defined in .github/workflows/deploy.yml is automatically triggered.
  2. Build and Push: The build-and-push job executes.
    • It checks out the latest code.
    • It logs into Docker Hub using the configured secrets.
    • It builds a Docker image from the Dockerfile.
    • The newly built image is tagged (e.g., your-dockerhub-username/your-repo-name:latest) and pushed to Docker Hub.
  3. Deploy to EC2: The deploy-to-ec2 job executes (only after the build-and-push job succeeds).
    • It securely connects to your AWS EC2 instance via SSH using the configured secrets.
    • On the EC2 instance, it performs the following:
      • Logs into Docker Hub (using secrets passed to the script).
      • Pulls the latest Docker image that was just pushed from Docker Hub.
      • Stops the currently running Docker container (if one exists with the specified name).
      • Removes the old container.
      • Starts a new Docker container from the newly pulled image, mapping port 80 on the EC2 instance to port 5000 in the container.
  4. Your application is now updated and accessible via the EC2 instance's public IP address or hostname (on port 80).

About

test

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages

0