Search code examples
jenkinsdeploymentcicd

Gitlab Environments equivalent in Jenkins


Gitlab allows me to create different environments for a project (for example staging, uat & prod). This is very helpful since I can use same variable's name (eg: AWS_SECRET_KEY) in my pipeline script for difference environments. I'm quited new to Jenkins, so far find it pretty cool due it its customization via plugins. But when it comes to deployment environment, I cannot find any plugins/ways to implement the same as in Gitlab. It's like if I want to deploy to staging, uat & prod, I need to create 3 variables with diff names, eg. STAGING_AWS_SECRET_KEY, UAT_AWS_SECRET_KEY, PROD_AWS_SECRET_KEY and my pipelines's scripts need to adjust accordingly to the env variables. I just wonder how people deal with the environment in Jenkins? Editted: I was wondering that did Jenkins have a feature that similar to Gitlab's Environments, but the answer is NO (according to @ AlexanderPletnev's answer).

I've been trying to find plugins that support that behavior for days, but found no viable solutions.


Solution

  • Note: if you're already using GitLab, I highly recommend using its own CI/CD functionality instead of building parallel solutions with Jenkins.

    Now, to your question.

    There are at least two options depending on the pipeline architecture you want to use. Also, it's usually simpler to use one variable name that has different values.

    First, you can use different stages for different environments. This will allow you to make some deployments conditional, perform different operations before or after deployment depending on the target environment, and is generally useful in build pipelines that trigger the deployments automatically or if you want to have a single pipeline for the whole CI/CD process of an application:

    // Jenkinsfile
    
    pipeline {
      // ...
      stages {
        stage('Deploy to staging') {
          environment {
            AWS_SECRET_KEY = credentials('aws-key-staging')
          }
          steps {
            // Use AWS_SECRET_KEY environment variable here
          }
        }
        
        stage('Deploy to UAT') {
          environment {
            AWS_SECRET_KEY = credentials('aws-key-uat')
          }
          steps {
            // Steps could be different if you want
            // You can use the same AWS_SECRET_KEY environment variable here
          }
        }
    
        // ...
      }
    }
    

    Alternatively, you can parametrize the secret name. This could be useful for the deployment pipelines that are used separately, when you have a significant number of the environments and/or want to control the deployments manually. Note the usage of double quotes for the secret name:

    // Jenkinsfile
    
    pipeline {
      // ...
      parameters {
        choice name: 'Target environment',
               description: 'Some description',
               choices: [
                 'staging',
                 'uat',
                 'prod'
               ]   
      }
      stages {
        stage('Deployment') {
          environment {
            AWS_SECRET_KEY = credentials("aws-key-${params.'Target environment'}")
          }
          steps {
            // Use AWS_SECRET_KEY environment variable here
          }
        }
    
        // ...
      }
    }
    

    You can also combine these two approaches and set the target environment name in the previous stages.