Wednesday 14 August 2024

Setup local git branch and push to origin

How to Setup a local Git Branch, then push to origin be it github, gitlab or else and synch branchs ..

This pipeline will be generic and applicable to a typical project, which could include building, testing, and deploying a project.

Let’s assume your project is a Node.js application that you want to test and deploy using GitHub Actions. You can adapt this example to fit other languages or deployment environments.

1. Directory Structure

First, ensure your project directory is structured as follows:

 

2. GitHub Actions Workflow Configuration

Create the .github/workflows/ci-cd-pipeline.yml file with the following content:

name: CI/CD 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 Node.js
        uses: actions/setup-node@v3
        with:
          node-version: '18'

      - name: Install Dependencies
        run: npm install

      - name: Lint Code
        run: npm run lint

      - name: Run Unit Tests
        run: npm test

      - name: Build Application
        run: npm run build

      - name: Upload Build Artifacts
        uses: actions/upload-artifact@v3
        with:
          name: build-artifacts
          path: ./dist

  deploy:
    needs: build
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main'

    steps:
      - name: Checkout Code
        uses: actions/checkout@v3

      - name: Download Build Artifacts
        uses: actions/download-artifact@v3
        with:
          name: build-artifacts

      - name: Deploy to Production
        run: |
          # Assuming you're deploying to a server or a cloud provider
          scp -r ./dist user@yourserver:/path/to/deployment
          ssh user@yourserver "cd /path/to/deployment && pm2 restart your-app"


.....


 

3. Explanation of the Pipeline

Trigger

  • The pipeline is triggered on every push to the main branch and on pull_request targeting the main branch.

Jobs

  • Build Job: This job runs on an Ubuntu runner and does the following:

    • Checkout Code: Pulls your latest code from the repository.
    • Set up Node.js: Sets up Node.js using the specified version.
    • Install Dependencies: Installs the dependencies specified in package.json.
    • Lint Code: Runs your linter to ensure code quality.
    • Run Unit Tests: Runs the unit tests to ensure your application is working as expected.
    • Build Application: Builds your application (if it’s a frontend project, this typically means creating a production build).
    • Upload Build Artifacts: Saves the build output to be used in the deployment job.
  • Deploy Job: This job runs after the build job and only if the push is to the main branch.

    • Checkout Code: Pulls your latest code.
    • Download Build Artifacts: Downloads the build output from the build job.
    • Deploy to Production: Deploys the application to your production environment. This is a placeholder for whatever deployment method you use, such as SSH to a server, deployment to a cloud provider, etc.

4. Additional Steps

  • Secret Management: Ensure you store any sensitive information like server credentials, API keys, etc., in GitHub Secrets. You can reference these in the workflow using ${{ secrets.YOUR_SECRET_NAME }}.

  • Docker Integration: If your app is containerized, you can add steps to build and push Docker images in the deploy job.

5. Running Locally

You should also be able to run your CI/CD steps locally to catch any issues before committing:

And then deploy manually or via a script if needed.

Conclusion

This pipeline provides a robust starting point for CI/CD, ensuring that your application is linted, tested, built, and deployed automatically whenever changes are made to the main branch. You can extend or modify it to fit the specific needs of your project, such as adding more jobs for integration testing, security scanning, or different deployment strategies.

 

Tuesday 16 July 2024

How to Use Jenkinsfile to connect into Ansible Tower and execute a server reboot running a ansible playbook ?

Let’s create a Jenkinsfile that integrates with Ansible Tower and executes a playbook to reboot a server. We’ll also include parameters for environment, target hostname group, and boolean options.


First, make sure you have the following prerequisites:

  1. Jenkins: Set up Jenkins on your system.
  2. Ansible Tower: Ensure you have Ansible Tower installed and configured.
  3. SSH Key Pair: Set up an SSH key pair for authentication between Jenkins and Ansible Tower.

Now, let’s create the Jenkinsfile:

pipeline {
    agent any

    parameters {
        string(name: 'ENV', description: 'Environment (e.g., dev, staging, prod)')
        string(name: 'TARGET_HOSTNAME_GROUP', description: 'Ansible inventory group for target hosts')
        booleanParam(name: 'REBOOT_ENABLED', defaultValue: true, description: 'Enable server reboot')
    }

    stages {
        stage('Checkout') {
            steps {
                checkout scm
            }
        }

        stage('Run Ansible Playbook') {
            steps {
                script {
                    def ansiblePlaybookCmd = """
                        ansible-playbook -i ${TARGET_HOSTNAME_GROUP} playbook.yml
                        --extra-vars 'reboot_enabled=${REBOOT_ENABLED}'
                    """
                    sh ansiblePlaybookCmd
                }
            }
        }
    }
}

Explanation:

  • The Jenkinsfile defines a pipeline with two stages: “Checkout” and “Run Ansible Playbook.”
  • We use parameters to allow users to input the environment, target hostname group, and whether to enable server reboot.
  • In the “Run Ansible Playbook” stage, we execute the Ansible playbook using the provided parameters.
  • The ansible-playbook command includes the inventory group and an extra variable (reboot_enabled) to control the reboot behavior.


Make sure to adjust the playbook.yml path and customize the playbook according to your specific requirements. Additionally, ensure that Jenkins has the necessary permissions to execute Ansible playbooks on the target hosts.

Remember to replace placeholders (such as ${TARGET_HOSTNAME_GROUP}) with actual values in your environment. If you encounter any issues, feel free to ask for further assistance! 🚀


- For more information on integrating Ansible with Jenkins, you can explore the official Ansible Tower plugin for Jenkins1

- Additionally, check out the Red Hat blog post on integrating Ansible with Jenkins in a CI/CD process2..

####################################################################


Let’s enhance the Jenkinsfile by adding the three boolean parameters: rebootstop_application, and start_application. These parameters will allow you to control additional actions during your pipeline execution. Here’s the updated Jenkinsfile:


pipeline {
    agent any

    parameters {
        string(name: 'ENV', description: 'Environment (e.g., dev, staging, prod)')
        string(name: 'TARGET_HOSTNAME_GROUP', description: 'Ansible inventory group for target hosts')
        booleanParam(name: 'REBOOT_ENABLED', defaultValue: true, description: 'Enable server reboot')
        booleanParam(name: 'STOP_APP_ENABLED', defaultValue: false, description: 'Stop application before reboot')
        booleanParam(name: 'START_APP_ENABLED', defaultValue: false, description: 'Start application after reboot')
    }

    stages {
        stage('Checkout') {
            steps {
                checkout scm
            }
        }

        stage('Run Ansible Playbook') {
            steps {
                script {
                    def ansiblePlaybookCmd = """
                        ansible-playbook -i ${TARGET_HOSTNAME_GROUP} playbook.yml
                        --extra-vars 'reboot_enabled=${REBOOT_ENABLED}'
                        --extra-vars 'stop_app_enabled=${STOP_APP_ENABLED}'
                        --extra-vars 'start_app_enabled=${START_APP_ENABLED}'
                    """
                    sh ansiblePlaybookCmd
                }
            }
        }
    }
}

Explanation:

  • I’ve added three new boolean parameters: STOP_APP_ENABLED and START_APP_ENABLED.
  • If STOP_APP_ENABLED is set to true, the pipeline will stop the application before rebooting the server.
  • If START_APP_ENABLED is set to true, the pipeline will start the application after the server reboot.

Remember to adjust the playbook (playbook.yml) to handle these additional actions based on the provided parameters. Feel free to customize the playbook according to your specific requirements.

Tuesday 9 July 2024

How to Write a DevOps Handover Document

A DevOps Handover document template to help a person leaving a project pass on the work done to another engineer, so it is continued, the work is around the Ansible playbook, for ci/cd automation, Jenkins pipeline and GitLab repository..



                          ##### BEGINNING OF  DOCUMENT TEMPLATE #########

DevOps Handover Document

Project Overview

Provide a brief overview of the project, its objectives, and its key components.

  • Project Name:
  • Project Description:
  • Key Stakeholders:
  • Project Timeline:
  • Current Status:

Ansible Playbook

Overview

Provide an overview of the Ansible playbook used in the project.

  • Purpose: Describe the purpose of the playbook.
  • Main Playbooks and Roles: List the primary playbooks and roles, their locations, and their functions.
  • Dependencies: Outline any dependencies or prerequisites for running the playbooks.

Directory Structure

├── ansible/ │ ├── playbooks/ │ │ ├── main_playbook.yml │ │ └── other_playbooks.yml │ ├── roles/ │ │ ├── role1/ │ │ └── role2/ │ ├── inventory/ │ │ ├── production │ │ └── staging │ └── ansible.cfg
  • Playbooks: Detailed description of key playbooks and their functions.
  • Roles: Detailed description of key roles and their tasks.
  • Inventory: Description of the inventory files and their purpose.
  • Configuration: Any special configurations in ansible.cfg.

Running Playbooks

Provide step-by-step instructions for running the playbooks.

  1. Setup Environment:
    bash
    ansible-playbook -i inventory/production main_playbook.yml
  2. Common Commands:
    • Command for running a specific playbook.
    • Command for running playbooks in staging/production environments.
    • Command for checking the playbook syntax.
    • Any other relevant commands.

CI/CD Automation

Jenkins Pipeline

Overview

Provide an overview of the Jenkins pipeline configuration.

  • Purpose: Describe the pipeline’s role in the project.
  • Pipeline Stages: List and describe each stage of the pipeline.

Configuration

  • Jenkinsfile Location:
  • Pipeline Script: Provide a snippet or full script of the Jenkinsfile if possible.
  • Plugins: List the Jenkins plugins used and their purposes.

Common Tasks

  • Triggering Builds: How to manually trigger builds.
  • Monitoring Builds: How to monitor build status and logs.
  • Debugging Issues: Steps to debug common issues.

GitLab Repository

Overview

Provide an overview of the GitLab repository setup.

  • Repository URL:
  • Branches: List and describe the purpose of key branches (e.g., main, develop, feature/*, hotfix/*).
  • Branching Strategy: Explain the branching strategy used in the project.
  • Merge Requests: Outline the process for creating and reviewing merge requests.

Repository Structure

bash
├── .gitlab/ │ ├── ci/ │ │ ├── stages/ │ │ ├── jobs/ │ │ └── scripts/ ├── src/ │ ├── main/ │ └── test/ ├── docs/ └── README.md
  • Key Directories: Description of key directories and their contents.
  • CI Configuration: Details of GitLab CI configuration (.gitlab-ci.yml).

Common Tasks

  • Cloning the Repository:
    bash
    git clone <repository_url>
  • Working with Branches:
    • Creating a new branch.
    • Merging branches.
  • CI/CD Pipelines:
    • How to trigger pipelines.
    • Monitoring pipeline status.
    • Debugging failed pipelines.

Contacts and Resources

Provide a list of key contacts and resources for further assistance.

  • Project Manager: Name, email, phone number.
  • Technical Lead: Name, email, phone number.
  • Relevant Documentation: Links to any additional documentation or resources.
  • Access Credentials: Securely provide any necessary credentials or access details.

Additional Notes

Any additional notes, tips, or important information to be aware of.


Prepared by: [Your Name]
Date: [Date]


                                   ##### END OF  DOCUMENT TEMPLATE #########

Monday 8 July 2024

Kubernetes CKA Exam preparation Links

 Here is a link to of links for the material to help me prepare for the Kubernetes Administration Exam.








How to Prepare for Certified Kubernetes Administration (CKA) Exam - https://vmtechie.blog/2019/01/12/how-to-prepare-for-certified-kubernetes-administration-cka-exam/
PASSING CKA IS NOT HARD, BUT PREPARATION IS --- https://thecloudcaptain.com/blog/passing_cka_is_not_hard/
Studying for the Certified Kubernetes Administrator Exam - http://www.kubernet.io/

How GitHub Advanced Security for Azure DevOps Saves the Day (and Your Reputation)

Let's face it, developers: we've all accidentally committed a secret (or two) to our code repository at some point. Maybe it was a...