"Streamlining Continuous Integration with CircleCI: A Python Project with GitHub Integration"

"Streamlining Continuous Integration with CircleCI: A Python Project with GitHub Integration"

·

6 min read

First, create a GitHub repository for the CircleCI project

And Git clone the repo in your Local Server

Now, Inside the repo create 2 files main.py and tests.py you can get both from the internet just use a small example code for the project

main.py

def say_hello():
    return "**Hi, this is Amit**"

def tell_joke():
    return "Joke of the day: I'm on a seafood diet. I see food and I eat it.!"

if __name__ == "__main__":
    print(say_hello())
    print(tell_joke())

test.py

import unittest
from main import say_hello, tell_joke

class TestMainFunctions(unittest.TestCase):

    def test_say_hello(self):
        greeting = say_hello()
        self.assertTrue(isinstance(greeting, str), "Greeting should be a string")

    def test_tell_joke(self):
        joke = tell_joke()
        self.assertTrue(isinstance(joke, str), "Joke should be a string")

if __name__ == "__main__":
    unittest.main()

Next, push the files into GitHub using git add and commit and git push to the main branch

Our code has been pushed to GitHub

Open a new tab and select "sign up"

Next, select "sign up with GitHub" as the second option.

Select "Authorize Circleci"

Signed with your GitHub password and confirm

Select the organization which is your GitHub account and select the repository and Select "Set Up Project"

As soon we select to set up the project - it will start the building project, but the build will fail as of now we didn't create the "Config.yml" file to build the project through Ciecleci

Now we create a config file, first, create a folder ".circleci" and inside the folder create a file named "config.yml"

we created the config.yml file here

Next, we push the config file to the GitHub repo add git add and commit

As we can see our .circleci folder containing the config file has been pushed successfully

As soon as we pushed the file - our project is successfully built in Circleci, this is the first stage which is - Build

We check our build steps - just select the project which we just build, and go through the steps - here in "Python main.py" we can see our message printed

As we can check every step

Now we make some changes in the main.py file and change the joke

And push the change to our GitHub and git add and commit

Here we can see as we push our updated code - our new build is started

Our new build is a success with one jobs as a build

Now again we can check our build steps, here on python main.py we can see our new message (the joke)

As we had our tests.py file, we run our test file in the terminal to see if our code is OK - the code is good and gave the OK output

Next, we make changes to our config.yml file and add a new Job as a test and add tests.py after the build for the main.py and also add job - tests to the workflow. Now we have 2 Jobs in the config file

Next, we git add and commit and push the change to our GitHub repo

Now again as we push our updated code our new build is started

Our new build is a success and here we can see 2 jobs for build and test

Click on either of the jos=bs to check the steps, here we can see the test build steps which show us the output "OK"

Now again we will add another Job and workflow in the config file for "deploy"

version: 2.1

jobs:
  build:
    docker:
      - image: cimg/python:3.11
    steps:
      - checkout
      - run: python main.py

  test:
    docker:
      - image: cimg/python:3.11
    steps:
      - checkout
      - run: python tests.py  

  deploy:
    docker:
      - image: cimg/python:3.11
    steps:
      - run: echo "Deploying to production server"    

workflows:
  build_and_test_deploy:
    jobs:
      - build
      - test:
          requires:
            - build      
      - deploy:
          requires:
            - test
          filters:
            branches:
              only: main

As we made our changes we again push the updated code to our GitHub repo

And again we can see our new build is started

After a successful build, we can see now we have 3 jobs - build, test, and deploy

We can check the deploy build steps and can see the output message that we gave in the config file

Now we try something different - let us create a New Branch and add changes to it and push the change to the repo.

Here we create a new branch named - "dev"

Here we made a slight change in the config file in the "workflow" section where previously it was "build_and_test" and now we added "build_and_test_deploy"

And push the change using git push origin dev not main as this is a dev branch

Now refresh the page and check the branch option and select "dev"

Here on the dev branch, we can see our build is successful and our 2 jobs are created but not the deploy jobs, why is that - is because if we check the config file in the workflow section on the deploy stage we see we added - filters and branch-only main.

Now let us see if our build triggers if we merge the dev branch to the main

select the pull request in the GitHub repo - it will appear after we push our code through a different branch

Here if we scroll down we can see the changes in the workflow

Select the "merge pull request"

and last select " confirm merge "

Here we can see if our merge is successful, let's see whether our build is triggered or not

indeed our build is started

As we check the workflow name we can see the new name - "build_and_test_deploy"

In conclusion, our CircleCI project integrated with GitHub and Python has proven to be a powerful combination for continuous integration and automated testing of our Python applications. By leveraging CircleCI's seamless integration with GitHub, we were able to automate the entire CI/CD pipeline, from code commits to deployment.

Furthermore, the ease of configuring build environments and managing dependencies with CircleCI allowed us to focus on writing code and delivering features without worrying about the underlying infrastructure.

It was a fun project to build and learn and I hope you too gain something useful from the project.

Thank you for reading the article !!!!