Skip to content

Instantly share code, notes, and snippets.

@darinpope
Last active January 30, 2024 11:10
Show Gist options
  • Save darinpope/8457511dac9a0a3642507d7e81e24be7 to your computer and use it in GitHub Desktop.
Save darinpope/8457511dac9a0a3642507d7e81e24be7 to your computer and use it in GitHub Desktop.

Gist for https://www.youtube.com/watch?v=Zy_FQEYkaRw

Pipeline version 1

pipeline {
  agent any
  stages {
    stage('test') {
      steps {
        sh '''
          gcloud version
        '''
      }
    }
  }
}

Pipeline version 2

pipeline {
  agent any
  stages {
    stage('test') {
      steps {
        sh '''
          gcloud version
          gcloud compute zones list
        '''
      }
    }
  }
}

Pipeline version 3

pipeline {
  agent any
  stages {
    stage('test') {
      steps {
        withCredentials([file(credentialsId: 'gcloud-creds', variable: 'GCLOUD_CREDS')]) {
          sh '''
            gcloud version
            gcloud auth activate-service-account --key-file="$GCLOUD_CREDS"
            gcloud compute zones list
          '''
        }
      }
    }
  }
}

Pipeline version 4

pipeline {
  agent any
  environment {
    CLOUDSDK_CORE_PROJECT='x:y'
  }
  stages {
    stage('test') {
      steps {
        withCredentials([file(credentialsId: 'gcloud-creds', variable: 'GCLOUD_CREDS')]) {
          sh '''
            gcloud version
            gcloud auth activate-service-account --key-file="$GCLOUD_CREDS"
            gcloud compute zones list
          '''
        }
      }
    }
  }
}

Pipeline version 5

pipeline {
  agent any
  environment {
    CLOUDSDK_CORE_PROJECT='x:y'
    CLIENT_EMAIL='a@a.com'
  }
  stages {
    stage('test') {
      steps {
        withCredentials([file(credentialsId: 'gcloud-creds', variable: 'GCLOUD_CREDS')]) {
          sh '''
            gcloud version
            gcloud auth activate-service-account --key-file="$GCLOUD_CREDS"
            gcloud compute zones list
          '''
        }
      }
    }
  }
  post {
    always {
      sh 'gcloud auth revoke $CLIENT_EMAIL'
    }
  }
}

Pipeline version 6

pipeline {
  agent any
  environment {
    CLOUDSDK_CORE_PROJECT='x:y'
    CLIENT_EMAIL='a@a.com'
    GCLOUD_CREDS=credentials('gcloud-creds')
  }
  stages {
    stage('test') {
      steps {
        sh '''
          gcloud version
          gcloud auth activate-service-account --key-file="$GCLOUD_CREDS"
          gcloud compute zones list
        '''
      }
    }
  }
  post {
    always {
      sh 'gcloud auth revoke $CLIENT_EMAIL'
    }
  }
}

Documentation

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