Skip to content

How to set environment variable in script? #5

@haxung

Description

@haxung

Thanks for your videos and examples, they help me much in jenkins study.

I got trouble when I wanted to set environment variable in script.
I write a shell script gen_tag.sh, like this:

#!/bin/bash

if [[ "$GIT_BRANCH" =~ ^v ]]; then
  export TAG="${GIT_BRANCH}"
else
  export TAG="${GIT_COMMIT:0:12}"
fi

echo "$TAG"

and in Jenkinsfile, this is a example:

pipeline {
  agent any
  stages {

    stage('PreTest') {
      steps {
        catchError(buildResult:'UNSTABLE',stageResult:'UNSTABLE') {
          sh 'chmod +x ./gen_tag.sh'
          sh './gen_tag.sh'
        }
      echo currentBuild.result
      }
    }

    stage('Prepare') {
      steps {
        catchError {
          echo "$TAG" // it cause wrong, cannot get TAG from env
        }
        echo currentBuild.result
      }
    }
  }
}

In fact, I cannot get TAG value from environment. Does there have some ideas to set environment variable in script?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions