0% found this document useful (0 votes)
13 views5 pages

TP Github

This document outlines the process of setting up a Continuous Integration (CI) pipeline using Docker and GitHub Actions for a Flask application. It includes steps for creating and dockerizing the application locally, configuring a GitHub repository, and defining a CI pipeline in a YAML file. Key concepts of GitHub Actions, such as workflows, jobs, and steps, are explained, along with instructions for testing and deploying the application.

Uploaded by

Stephane Behalal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views5 pages

TP Github

This document outlines the process of setting up a Continuous Integration (CI) pipeline using Docker and GitHub Actions for a Flask application. It includes steps for creating and dockerizing the application locally, configuring a GitHub repository, and defining a CI pipeline in a YAML file. Key concepts of GitHub Actions, such as workflows, jobs, and steps, are explained, along with instructions for testing and deploying the application.

Uploaded by

Stephane Behalal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Setting Up a CI Pipeline with Docker and GitHub

Actions
Part 1: Creating and Dockerizing the Flask Application Locally

1. Setting up the development environment:


o Install Python and PIP (if not already installed) on your machine.
o Create a working directory for your local project, for example FlaskApp.
2. Creating the Flask application:
Create a simple Flask application that returns the message "Hello, World!"
when accessing the root of the site.
To do this, you need to create three files in the FlaskApp folder:
o [Link]: contains the application code.

 [Link]: list of the dependencies required for the application.

Flask==3.0.3
pytest==7.4.2

 test_app.py: file containing automated tests to verify that the application works
correctly.

3. Running the application locally:


 In your terminal, navigate to your project folder.
 Install the Flask dependencies by running:
pip install -r [Link]
 Run the Flask application locally using:
python [Link]
 Check that the application works correctly by visiting [Link]
in your browser, and make sure the message "Hello, World!" appears.

4. Creating the local Git repository:

 Initialize a Git repository in your project, add your project files to your local
repository, and commit them using the following message:
"Initial commit - Flask app and tests".

Part 2: Containerizing the application with Docker (locally)

1. Creating the Dockerfile:

Create a Dockerfile in your project to containerize the Flask application. This file
must include:

 Installing Flask dependencies


 Copying the code into the container
 Specifying the port on which the Flask application listens: 5000
 Running the Flask application

2. Local Testing with Docker:

 Build and run the Docker image locally.


 Check again that the application works by accessing [Link]

Part 3: GitHub Repository and CI with GitHub Actions

1. Create an empty repository on GitHub named FlaskApp.


2. Link your local repository to GitHub.
3. Adding a CI pipeline with GitHub Actions:
o Create a .github/workflows folder in your local project, then add a
[Link] file to configure the CI pipeline.

Guide

GitHub Actions is a continuous integration (CI) and continuous deployment (CD)


service built into GitHub. It allows you to automate various development workflows
such as building, testing, and deploying applications directly from a GitHub repository.
Here is an overview of the key concepts of a CI pipeline with GitHub Actions and how
to configure it.

Basic Concepts of GitHub Actions:

 Workflow: A workflow is a series of actions defined in a YAML file. It


describes the steps to follow when an event occurs (for example, a push to the
repository).
 Events: Workflows run in response to events such as push, pull request, etc.
 Jobs: A workflow can contain multiple jobs, each running a series of steps. Jobs
can run in parallel or sequentially.
 Steps: These are the individual actions inside a job. Each step can run a
command or call an action (a predefined task) available on GitHub.
 Actions: These are reusable scripts that can be called in the steps of a job.
GitHub Actions provides a library of predefined actions, and you can also create
your own.

name: CI Pipeline
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.13' # spécifiez la version de Python

- name: Install dependencies


run: |
python -m pip install --upgrade pip
pip install -r [Link] # assurez-vous d'avoir ce fichier

- name: Run tests


run: |
pytest # ou toute autre commande pour exécuter vos tests

 name: Name of the workflow.


 on: Indicates when the workflow should run (here, on pushes or pull requests to
the main branch).
 jobs: Defines the tasks to be executed.
 runs-on: Specifies the environment on which the job runs, here ubuntu-
latest.
 steps: Defines each step of the job, such as:

 actions/checkout: Retrieves the repository code.


 actions/setup-python: Sets up the Python environment.
 Installation of dependencies and execution of tests.

 After adding and saving the [Link] file, commit and push it to your GitHub
repository.
 Verify the execution: Go to the “Actions” tab of your GitHub repository. You
should see the workflow running. If everything is configured correctly, it will
pass the defined tests. GitHub Actions will automatically run on each push,
testing your application and building the Docker image.
 Modify the [Link] file to include a step that pushes the image to Docker Hub
after it has been tested. This allows you to store and reuse Docker images.
 Add your Docker Hub authentication information to GitHub so that GitHub
Actions can push images there:
Go to your GitHub repository → Settings → Secrets and variables → Actions
→ “New repository secret”.

 Name the secret DOCKER_USERNAME with your Docker username.


 Name the secret DOCKER_PASSWORD with your Docker password.

 Why do we need to use GitHub secrets (DOCKER_USERNAME,


DOCKER_PASSWORD) in the [Link] file for authentication to Docker Hub?
 What are the risks associated with storing sensitive information in CI/CD
pipelines? What are best practices to reduce these risks?
 Push your modifications to the [Link] file to GitHub and check the workflow
execution in the Actions tab.
 If your organization uses multiple Docker registries (for example Docker
Hub and a private registry), how could you push the Docker image to multiple
registries in the same GitHub Action? Modify the pipeline to include a step that
pushes the image to two different registries (without testing).
 In the [Link] file, the workflow is triggered on a push to the main branch.
Modify it so that it also triggers when opening a Pull Request.
 Add an additional job to the [Link] pipeline to run unit tests in parallel with
the Docker build.
 Add a condition in [Link] so that the Docker image push step runs only if the
tests pass successfully.
 Add a step in [Link] that sends an email notification if a test fails, using a
specific GitHub Action (such as actions/send-mail).

You might also like