Enhance CI/CD: Connect Git Push to Ansible through Git Actions and Jenkins

Prem ChoudharyPrem Choudhary
5 min read

What is the idea to do this project?
We will learn how CI/CD works with jenkins by automating tasks with help of ansible, so that developer whenever pushes something to github it automatically deployed every server where the code in present.

I will explain you later, first follow the steps.

  1. Go to your local system where developer will code.

  2. Login your github there.

  3. Run : git init

  4. Project directory Structure:

     Simple-website
     ├── .github
            └── workflows
                    └── git-action.yml
     ├── index.html
     ├── Jenkinsfile
     ├── ansible
            └── playbooks
                    └── nginx-setup.yml
     ├── inventory.aws_ec2.yml
    
  5. With help of Project directory Structure copy the below files.

//Jenkinsfile- 


pipeline {
    agent any

    environment {
        // Using SSH key stored in Jenkins credentials
        EC2_SSH_KEY = credentials('EC2_SSH')
        ANSIBLE_HOST_KEY_CHECKING = 'False'
    }

    stages {
        stage('Checkout') {
            steps {
                // Checkout the code from the GitHub repository
                checkout scm
            }
        }

        stage('Run Ansible Playbook') {
            steps {
                script {
                    // Running Ansible Playbook to configure the server
                    sh '''
                       ansible-playbook -i ./ansible/inventory.aws_ec2.yml -e 'host_group=tag_git_gitaction' ./ansible/playbooks/nginx-setup.yml -u ubuntu --private-key $EC2_SSH_KEY
                    '''
                }
            }
        }
    }
}
#git-action.yml-

name: Trigger Jenkins Build

on:
  push:
    branches:
      - main  # Trigger on push to main branch

jobs:
  trigger-jenkins:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Trigger Jenkins Job
        run: |
          curl -X POST http://<ec2-public-ip>:8080/job/<job-name>/build \
          --user <your-user>:<Token-number>
#inventory.aws_ec2.yml-

plugin: aws_ec2
regions:
  - us-east-1
filters:
  "tag:git": "gitaction"
strict: true
keyed_groups:
  - prefix: tag
    key: tags
#nginx-setup.yml - 

---
- name: Install and Configure Nginx
  hosts: "{{ host_group }}"
  become: yes
  tasks:
    - name: Update apt cache
      apt:
        update_cache: yes

    - name: Install Nginx
      apt:
        name: nginx
        state: present

    - name: Start Nginx service
      systemd:
        name: nginx
        state: started
        enabled: yes

    - name: Remove default index.html
      file:
        path: /var/www/html/index.html
        state: absent

    - name: Clone custom HTML page from GitHub
      git:
        repo: 'https://github.com/<user-name>/<repo-name>'
        dest: /var/www/html
        clone: yes
        update: yes
        force: yes
    - name: Restart Nginx service
      systemd:
        name: nginx
        state: restarted

For deep explaination of these files i will explain in another article, first you setup the CI/CD pipeline.

  1. Create a EC2 instance

  2. ssh to your instance

  3. Install jenkins in your EC2 instance for Linux from https://www.jenkins.io/doc/book/installing/linux/

  4. Install awscli (Google it)

  5. Go to your AWS Management Console.

  6. Create a user under IAM.

  7. Give the administrator access to that user.

  8. After creating user, select that user.

  9. You will see a create access key. Click that and it will give you access key and secret access key.

  10. Go to your EC2.

  11. Run: aws configure

  12. Enter your access key then your secret access key, enter your region and format will be json.

  13. Go to http://<ec2-ip>:8080 and configure jenkins

  14. Go to Manage Jenkins → Plugin Manager and install these plugins
    Git Plugin
    Pipeline Plugin
    GitHub Plugin
    Credentials Plugin
    Ansible Plugin

    Git Parameter

  15. Install Ansible, run the following commands:
    apt update

    apt install ansible

  16. Install AWS SDK and Boto3 for Python
    If you're using Ansible and the AWS plugins, you'll also need the Boto3 library to interact with AWS APIs. Step-by-Step:
    sudo apt-get install python3-pip

    pip3 install boto3 botocore

  17. Install AWS Ansible Collection
    ansible-galaxy collection install amazon.aws

    ansible-galaxy collection list amazon.aws

  18. Now create a token
    In your jenkins, go to user profile → Configure → API token → Generate new token
    it will give you user and token now add this to git-action.yml you created in the local machine.
    - name: Trigger Jenkins Job run: | curl -X POST http://<ec2-public-ip>:8080/job/<job name>/build \ --user <your-user>:<Token-number>
    It is used to trigger jenkins

  19. Now we will create pipeline in jenkins:
    1. Go to jenkins dashboard
    2. Click on new item.
    3. Name your pipeline (this will be going in the Jenkinsfile)
    4. Select Pipeline script from SCM in the Pipeline section
    5. Under SCM, select Git.

    6. In the Repository URL field, provide the URL of your GitHub repository (e.g., https://github.com/yourusername/your-repository.git)
    7. If your GitHub repository is private, you’ll need to set up credentials. You can either create a GitHub token or use SSH keys for authentication.
    8. Specify the Branch to Build; you can find this on github; most probably ‘main’
    9. Create the Pipeline Script (Jenkinsfile)

Add the EC2 private key in Jenkins :-
Dashboard → Manage Jenkins → Credential → Add Credential

1. In the Add Credentials form:

  • Kind: Select SSH Username with private key.

  • Scope: Choose Global (unless you want it restricted to specific jobs).

  • Username: Enter the EC2 username (for example, ubuntu for Ubuntu instances or ec2-user for Amazon Linux).

  • Private Key:

    • Select Enter directly and paste the contents of your EC2 private key (my-key.pem) into the field, or

    • If you prefer to upload the private key file, select From the file on the Jenkins master and upload the .pem file.

  • Passphrase: If your private key is protected by a passphrase, enter it here. If not, leave it blank.

  1. ID: You can enter EC2_SSH as the ID for the credential (this ID will be used in your pipeline, as referenced in $EC2_SSH_KEY).

3. Click OK to save the credentials.

Here’s your CI/CD using jenkins is completed where whenever you push something on github then github action will trigger jenkins → Jenkins will trigger Ansible → Ansible will do tasks (Install Nginx, Start Nginx, Remove default index.html, Clone custom HTML page from GitHub, Restart Nginx) on the EC2 who have tag key=git and tag value=gitaction.

TIPS:
1. If jenkins is working slow after restarting the EC2 then
Manage Jenkins → System → Jenkins url (Insert the new public IP of your EC2)

This is the Basic model approach, further I will post advance version. Till then

Hari OM Tat Sat🕉️❤️

10
Subscribe to my newsletter

Read articles from Prem Choudhary directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Prem Choudhary
Prem Choudhary

DEVOPS ENGINEER | AWS | Java | Linux | Python | Git | Github | Docker | Spring Boot | Ansible | Jenkins | Algorithmic Trader | Pine Editior