'Set environmental variables from shell script in Jenkins pipeline

I have a requirement where I have a shell file (init.sh) which sets environmental variables based on conditions.

My pipeline looks something like this

pipeline {
    agent {
        kubernetes {
            label 'test'
        }

    environment { 
        token = credentials('xyz')
    }

    stages {
     stage('test'){
                  container('test') {
                  sh """#!/bin/bash
                  printenv
                  """
              }
     }
    }
}

Is it possible to set the env(s) from the init.sh file which can be used inside the container as well?

Can anything be done in the environment block?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source