简体   繁体   中英

Starting already existing VM with Jenkins on Google Cloud

I am trying to start a VM that already exist in Google cloud with my jenkins to use it as a slave. The reason is because if I start the template of this VM I need to do a few things before I can use my Jenkins code.

Does anyone know how to start VM's that already exist in my VM Pool in Google Could via Jenkins?

There might be 2 approaches to this depending on the operations that you need to run before in your machine that is preventing you from just recreating it.

First and possibly the most straightforward given the restriction that the machine already exists would be talking directly to the GCE API in order to list and start the machine from Jenkins (using a build step).

Basically you can make requests to the GCE API to do operations with your instances. I suggest doing this using gcloud from within the Jenkins master node as it'll save you having to write your own client. It's straightforward as you only have to "install" it in your master and you can make it work safely using a service account.

Below is the outline of this approach:

  1. Download the cloud-sdk to your master node following these release instructions .

You can do this once outside of Jenkins or directly in the build step, doesn't matter as long as Jenkins and its user is able to get the binary.

  1. Create the service account , generate authentication keys and give it permissions to interact with GCE .

Using a service account is the way to go as you can restrict its permissions to the operations that are relevant for you.

  1. Once you get the service account that will be bound to your gcloud client, you'll need to set it up in Jenkins. You might want to do this in a build step (I'm using Groovy here but it should be easy to translate it to the UI):
stage('Start_machine'){
            steps{

                //Considering that you already installed gcloud in this node, but you can also curl it from here if that's convenient

                     // You can set this as an scope env var in Jenkins or just hard code it
                     gcloud config set project ${GOOGLE_PROJECT_ID};

                     // This needs a json file location accessible by jenkins like: --key-file /var/lib/jenkins/..key.json
                     gcloud auth activate-service-account --key-file ${GOOGLE_SERVICE_ACCOUNT_KEY};

                     // Check the reference on this command: https://cloud.google.com/sdk/gcloud/reference/compute/instances/start
                     gcloud compute instances start my_existing_instance;

                     echo "Instance started"
                }   
                post{
                   always{
                    println "Result : ${currentBuild.result}";



                }
            }

Wrapping up: You basically create a service account that has the permissions to start your instances. Download an client that can interact with the GCE API ( gcloud ), authenticate it and start the instance, all from within your pipeline.

The second approach might be easier if there were no constraints regarding the preexisting machine.

Jenkins has a plugin for Compute Engine that will automatically spin up new workers whenever needed.

I know that you need to do some previous operations before Jenkins sends some work to these slave machines. However, I want to bring to your attention that this plugin also considers the start up scripts .

So there's always the option to preload your operations there before the machine takes off and by the time it's ready, you might have everything done.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM