Skip to content

Instantly share code, notes, and snippets.

@satyadeepk
Last active February 18, 2022 12:21
Show Gist options
  • Save satyadeepk/b0146a0ccefc87fc41a86ea7250ec23e to your computer and use it in GitHub Desktop.
Save satyadeepk/b0146a0ccefc87fc41a86ea7250ec23e to your computer and use it in GitHub Desktop.
Jenkins Google Cloud SDK install with auth script
#Ref: https://github.com/circleci/android-cloud-test-lab/blob/master/circle.yml
export DIRECTORY="/var/jenkins_home/GoogleCloudSDK/google-cloud-sdk/bin"
if [ ! -d "$DIRECTORY" ]; then
# Control will enter here if $DIRECTORY doesn't exist.
cd /var/jenkins_home
wget https://dl.google.com/dl/cloudsdk/release/google-cloud-sdk.zip -O google-cloud-sdk.zip
unzip -o google-cloud-sdk.zip -d ./GoogleCloudSDK/
./GoogleCloudSDK/google-cloud-sdk/install.sh
fi
export PATH=/var/jenkins_home/GoogleCloudSDK/google-cloud-sdk/bin:$PATH
gcloud --quiet components update
gcloud --quiet components install beta
gcloud auth activate-service-account --key-file $GCLOUD_SERVICE_AUTH
gcloud config set project addodoc-1
gcloud beta test android run --app $WORKSPACE/app/build/outputs/apk/app-debug.apk --test $WORKSPACE/app/build/outputs/apk/app-debug-androidTest-unaligned.apk --results-bucket cloud-test-jenkins
@msyaifullah
Copy link

hi bro, how did you set $GCLOUD_SERVICE_AUTH? via parameter?

@brianhyder
Copy link

For anyone who was looking for the answer to the above question. Create a secret file and upload the JSON key file through the Bindings section. All of this was done through the Job Configuration screen.
screen shot 2018-08-07 at 10 21 41 am

@vijayakumar-psg587
Copy link

Not sure if it would be helpful, but here is how I use it in my project and it is very helpful

pipeline {
agent any

environment {
    GOOGLE_PROJECT_ID = 'test-prj-cmd';

	GOOGLE_SERVICE_ACCOUNT_KEY = credentials('service_account_key');
}

stage('Deploy'){
steps{

			sh """
				echo "deploy stage";
				curl -o /tmp/google-cloud-sdk.tar.gz https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-220.0.0-linux-x86_64.tar.gz;
				tar -xvf /tmp/google-cloud-sdk.tar.gz -C /tmp/;
				/tmp/google-cloud-sdk/install.sh -q;
				source /tmp/google-cloud-sdk/path.bash.inc;
				 gcloud config set project ${GOOGLE_PROJECT_ID};
				 gcloud components install app-engine-java;
				 gcloud components install app-engine-python;
				 gcloud auth activate-service-account --key-file ${GOOGLE_SERVICE_ACCOUNT_KEY};
				 echo "After authentication gcloud";
				 gcloud config list;
				 mvn -X clean package appengine:deploy -Dmaven.test.failure.ignore=true;
			"""
		}	
		post{
			always{
				println "Result : ${currentBuild.result}";
				println "Deploy to GCP ..";
			}
		}
	}

}

Note: If you need more gcloud components to be installed please go ahead and install in the sh section. I am trying to figure out how to have the gcloud instance available for the entire set of jenkins job type (like a maven/git/jdk tool) instead of plainly using docker to do that

@yog560
Copy link

yog560 commented Apr 24, 2019

@vijaykumar.. how did you pass service_account_key (json file)... ?

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