Day 9: Setting Up CI/CD Pipeline with Jenkins: A Hands-On Guide ๐Ÿš€

Day 9: Setting Up CI/CD Pipeline with Jenkins: A Hands-On Guide ๐Ÿš€

ยท

3 min read

๐Ÿ”ง Creating and Assigning Jobs to Jenkins Agents

  • Start by writing a description for the job.

  • Specify the GitHub project repository.

  • Enable GitHub hook trigger for GITScm polling.

  • Remember to integrate your Jenkins URL with GitHub and allow necessary ports for seamless integration.

๐ŸŒ GitHub Integration

  • Add your Jenkins URL to GitHub and configure port settings for smooth communication, just like you would with AWS ECS instances.

๐ŸŒฑ Getting Started with the Pipeline

  • Embrace the #grovestntax as you kick off the pipeline setup.

  • Begin your pipeline with the declaration of the agent. ๐Ÿ› ๏ธ

  • Use a specific agent by adding its name: Agent {label 'dev-agent'}.

๐Ÿ”จ Defining Pipeline Stages

  • Your pipeline needs to have stages that define the workflow of your CI/CD process.

  • Start with the 'Code' stage:

    • Use the git step to fetch code from your GitHub repository.
  • Move on to the 'Build & Test' stage:

    • Employ the docker build command to create your Docker image.
  • Conclude with the 'Deploy' stage:

    • Use the docker-compose commands to manage container deployment.

๐Ÿงฉ Agent Distribution

  • Distribute your Jenkins workload across different agents.

  • Create multiple agents (e.g., 'agent1', 'agent2') to parallelize tasks.

๐Ÿ”ง Setting Up Agents

  • Configure agents by:

    • Naming them appropriately.

    • Adding descriptions for clarity.

    • Specifying root directories (e.g., /home/ubuntu).

    • Assigning labels for categorization (e.g., 'Django', 'Dev').

    • Launching them via SSH, configuring IP, private key, and host key verification strategy.

๐Ÿ”‘ SSH Key Setup

  • On both the master and agents, generate SSH keys to enable secure communication.

  • On master: cd .ssh && ssh-keygen && cat id_rsa.pub.

  • On agent: cd .ssh (similar steps).

๐Ÿ”— Connecting Master and Agents

  • Share the public key from the agent with the master for authentication.

๐Ÿš€ GitHub to DockerHub to AWS

  • The journey begins with GitHub triggering a Docker build.

  • Through your pipeline, code is packaged and pushed to DockerHub.

  • From there, AWS EC2 instances pull and deploy the image.

๐Ÿ“ก GitHub Integration

  • Enable SCM polling and set up a webhook for smoother GitHub integration.

๐ŸŒ DockerHub Login

  • Install the environment Injector plugin.

  • Create global credentials for DockerHub login.

  • Use the withCredentials block to securely log in: sh "docker login -u ${env.dockerHubUsername} -p ${env.dockerHubPassword}".

๐Ÿ“ Putting It All Together: Jenkinsfile

pipeline {
    agent {label 'dev-agent'}
    stages {
        stage('Code') {
            steps {
                git url: 'https://github.com/test12/node', branch: 'master'
            }
        }
        stage('Build & Test') {
            steps {
                sh 'docker build . -t test12/node-todo-app-cicd:latest'
            }
        }
        stage('Login & push image') {
            steps {
                withCredentials([usernamePassword(credentialsId: 'dockerhub', passwordVariable: 'dockerHubPassword', usernameVariable: 'dockerHubUsername')]) {
                    sh "docker login -u ${env.dockerHubUsername} -p ${env.dockerHubPassword}"
                    sh "docker push ${env.dockerHubUsername}/node-todo-app-cicd:latest"
                }
            }
        }
        stage('Deploy') {
            steps {
                sh 'docker-compose down && docker-compose up -d'
            }
        }
    }
}

Conclusion

By following these steps, you'll have your CI/CD pipeline up and running, allowing you to seamlessly build, test, and deploy your Node.js application. Now you're ready to automate and streamline your development workflow!

Feel free to connect with me on LinkedIn for more insights and discussions: Muhammad Zubair ๐Ÿ‘‹๐ŸŒŸ

ย