Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save mauricioduarte01/fba0de12457c9c3167fa0694c6a9b566 to your computer and use it in GitHub Desktop.

Select an option

Save mauricioduarte01/fba0de12457c9c3167fa0694c6a9b566 to your computer and use it in GitHub Desktop.
nada
https://app.eraser.io/workspace/zXtfgL0Wih22fXbAK8cm
In a Jenkins pipeline, the cleanWs() step is used to clean up the workspace, deleting directories and files that may have been left from previous builds. Whether you should use it at the beginning or the end of a pipeline (or both) can depend on your specific needs and workflow.
Here are the common practices:
At the Beginning: Using cleanWs() at the start of a pipeline ensures that you have a clean state before you begin your build process. This is helpful to prevent any conflicts or inconsistencies that may arise from residual files from a previous build.
At the End: Placing cleanWs() at the end of a pipeline is useful for housekeeping purposes, ensuring that no unnecessary files are left on the build agent after the job is completed. This helps to conserve disk space on the agent.
Both: Using cleanWs() both at the beginning and the end can be considered a more rigorous approach, where you ensure a clean workspace before you start and also clean up after yourself when you're done.
It's also important to consider the following:
If you have multiple builds running on the same agent in parallel, you should be careful with the cleanup process so that one build does not delete the workspace of another.
If your builds are incremental and rely on artifacts from previous builds, using cleanWs() might not be appropriate as it would delete those needed artifacts.
Some teams prefer to clean up at the beginning to minimize the time spent in the "post" section of the pipeline, which is generally used for steps like sending notifications, collecting artifacts, and other end-of-pipeline tasks.
It's always good to tailor the usage of cleanWs() to the specific needs of your CI/CD process.
@mauricioduarte01
Copy link
Author

In Jenkins Pipeline, the post section defines one or more additional steps that are run upon the completion of a stage. These steps can depend on the outcome of the stage (success, failure, always, unstable, changed, aborted). The post section helps in performing cleanup actions, sending notifications, or taking any steps based on the stage result.

Here’s an example of how you can define a post section within a stage:

groovy

pipeline {
    agent any
    
    stages {
        stage('Example') {
            steps {
                // steps for this stage
            }
            post {
                success {
                    // steps to run only if the stage succeeds
                    echo 'The Example stage completed successfully.'
                }
                failure {
                    // steps to run only if the stage fails
                    echo 'The Example stage failed.'
                }
                always {
                    // steps to run regardless of the stage outcome
                    echo 'This will always run irrespective of the stage result.'
                }
            }
        }
    }
}

Example:

Let's say you have a Deploy to UTA stage and you want to clean up resources or send notifications if the deployment fails or succeeds:

groovy

stage('Deploy to UTA') {
    steps {
        // Your deployment steps here
    }
    post {
        success {
            // Maybe notify the team that the deployment was successful
            notify('Deployment to UTA was successful')
        }
        failure {
            // Perhaps perform some cleanup or notify someone of the failure
            cleanupEnvironments()
            notify('Deployment to UTA failed')
        }
        always {
            // You could use this block to perform steps that should happen
            // whether the deployment succeeds or fails
            echo 'The Deploy to UTA stage has completed'
        }
    }
}

In this example, the notify and cleanupEnvironments are hypothetical function calls representing some kind of notification or cleanup step you might perform.

Remember to define any custom steps or methods you use in your Jenkins Pipeline (notify and cleanupEnvironments in the example) within a script block or as part of shared libraries that you import into your Pipeline.

@mauricioduarte01
Copy link
Author

Define Code Freeze Criteria:

    Determine the conditions under which code should be frozen. This could be a certain time before a release, after significant features have been merged, or when automated tests pass a certain threshold of code coverage or quality.

Automated Quality Gates:

    Implement automated quality gates that check for test pass rates, static code analysis results, code coverage metrics, and any other relevant quality metrics. You can use Jenkins plugins like Code Coverage API, Checkstyle, PMD, FindBugs, or SonarQube to automatically assess code quality.
    Only allow merges to the release branch if these quality gates pass.

Environment-Based Triggers:

    Utilize environment-based triggers to automate the code freeze. For example, you can set up a Jenkins job that is triggered by a date (time-based trigger) or by a specific condition in your version control system (like the creation of a release branch).

Use Pipeline as Code:

    Use Jenkinsfiles to define pipeline as code, which can include stages that implement the code freeze logic. This ensures version control for your pipelines and allows for code review on the pipeline itself.

Pre-Merge Checks:

    Set up pre-merge checks using Jenkins pull request builder or similar plugins to run a series of tests whenever a new commit is made to a branch that is designated for merging into the release branch. These checks should pass before the merge is allowed.

Automate Branch Management:

    Use Jenkins to automate the creation and management of release branches, ensuring that new branches are created from a stable mainline and that they conform to your branching strategy.

@mauricioduarte01
Copy link
Author

1. Define Stages Clearly

You should define each stage in your Jenkins pipeline to perform a single responsibility. For instance:

    Build: Compile the code, run unit tests, and create an artifact.
    Release: Increment version numbers and tag the repository.
    Docker: Build the Docker image and push it to a registry.
    Deploy to UTA: Deploy the application to a User Test Acceptance (UTA) environment.
    QA Test: Execute automated QA tests in the UTA environment.

2. Use Checkpoints

For the ability to restart from a specific stage, you can use the Jenkins "Checkpoint" plugin. This plugin allows you to define checkpoints at which you can restart your pipeline without running from the beginning.

Here's an example of how you could define a checkpoint:

groovy

pipeline {
    agent any

    stages {
        stage('Build') {
            steps {
                // Build steps
            }
        }
        stage('Release') {
            steps {
                // Release steps
            }
        }
        stage('Docker') {
            steps {
                checkpoint('BeforeDeployToUTA')
                // Docker steps
            }
        }
        stage('Deploy to UTA') {
            steps {
                // Deploy to UTA steps
            }
        }
        stage('QA Test') {
            steps {
                // QA Test steps
            }
        }
    }
}

3. Ensure Idempotency

Make sure that each stage can be executed independently and is idempotent, meaning it can run multiple times without causing issues. For example, the Docker image build stage should check if the image already exists before attempting to rebuild it.
4. Environment Readiness Check

Before the 'Deploy to UTA' stage, you should include a readiness check for the runtime environment. If the environment isn't ready, you can fail the stage gracefully with a clear error message. This prevents unnecessary runs of the following stages.

@mauricioduarte01
Copy link
Author

The Unix philosophy emphasizes designing small, modular utilities that do one thing and do it well, rather than creating complex, multi-functional programs. This approach favors simplicity and the ability to chain programs together to perform complex tasks. Each program receives input, processes it, and produces output in a straightforward manner, which makes debugging and adaptation easier.

The KISS principle, which stands for "Keep It Simple, Stupid," advocates for simplicity in design. It suggests that systems work best when they are kept simple rather than made complicated; simplicity should be a key goal in design, and unnecessary complexity should be avoided.

Synthesizing both, the core idea is that simplicity leads to greater efficiency, maintainability, and functionality in system design. By focusing on simple, single-purpose tools that perform their function well, and by avoiding unnecessary complexities, you create a system that is more robust, easier to understand, and easier to manage. Both philosophies celebrate the elegance and efficiency found in simplicity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment