首页 > 解决方案 > 如何使用 Jenkins Pipeline 在 GCP 上创建 VM

问题描述

我尝试使用此命令直接在管道中使用 gcloud 创建一个 VM,

管道{

      agent any

      stages{

          stage('Create a VM'){
                   gcloud compute instances create centos-7 --image-family=centos-7 --image-project=centos-cloud  --zone=europe-west2-c

                        }
               } 
  }

我尝试传递 auth-key 但没有找到正确的语法来做到这一点,有人可以帮忙吗?

标签: google-cloud-platformjenkins-pipeline

解决方案


语法是下一个,即使有很多其他步骤要遵循来实现连接并能够创建机器,请遵循本指南Using Jenkins for Distributed builds on Compute Engine,在提到的步骤中,我们将展示你一个代码示例:

export PROJECT=$(gcloud info --format='value(config.project)')
cat > jenkins-agent.json <<EOF
{
  "builders": [
    {
      "type": "googlecompute",
      "project_id": "$PROJECT",
      "source_image_family": "ubuntu-2004-lts",
      "source_image_project_id": "ubuntu-os-cloud",
      "zone": "us-central1-a",
      "disk_size": "10",
      "image_name": "jenkins-agent-{{timestamp}}",
      "image_family": "jenkins-agent",
      "ssh_username": "ubuntu"
    }
  ],
  "provisioners": [
    {
      "type": "shell",
      "inline": ["sudo apt-get update && sudo apt-get install -y default-jdk"]
    }
  ]
}
EOF

推荐阅读