@@ -7,14 +7,175 @@ pipeline {
77 yamlFile 'jenkins/agent.yaml'
88 }
99 }
10+
11+ environment {
12+ // Set PATH to include our custom bin directories
13+ PATH = "${env.PATH}:${env.HOME}/go/bin:${env.HOME}/bin"
14+
15+ // GCP Configuration - these should be set in Jenkins configuration or Vault
16+ GCP_PROJECT_ID = "${env.GCP_PROJECT_ID ?: 'your-gcp-project'}"
17+ GCP_ZONE = "${env.GCP_ZONE ?: 'us-west1-b'}"
18+ GCP_SERVICE_ACCOUNT = "${env.GCP_SERVICE_ACCOUNT ?: 'your-service-account@developer.gserviceaccount.com'}"
19+ GCP_NETWORK_SUBNET = "${env.GCP_NETWORK_SUBNET ?: 'primary-west'}"
20+ GCP_MACHINE_TYPE = "${env.GCP_MACHINE_TYPE ?: 'e2-standard-16'}"
21+ GCP_DISK_SIZE = "${env.GCP_DISK_SIZE ?: '100'}"
22+ GCP_DISK_POLICY = "${env.GCP_DISK_POLICY ?: ''}"
23+ GCP_IMAGE = "${env.GCP_IMAGE ?: 'projects/debian-cloud/global/images/debian-12-bookworm-v20240910'}"
24+
25+ // K3sup version
26+ K3SUP_VERSION = "0.13.9"
27+
28+ // Go and kubectl versions
29+ GO_VERSION = "1.24.3"
30+ KUBECTL_VERSION = "v1.32.0"
31+ }
32+
1033 options {
11- timeout(time: 20 , unit: 'MINUTES')
34+ timeout(time: 60 , unit: 'MINUTES')
1235 }
36+
1337 stages {
1438 stage('Setup') {
1539 steps {
1640 sh 'apk add bash curl'
1741 }
1842 }
43+
44+ stage('Install k3sup') {
45+ steps {
46+ sh '''#!/bin/bash
47+ curl -O -L https://github.com/alexellis/k3sup/releases/download/${K3SUP_VERSION}/k3sup
48+ chmod +x k3sup
49+ mkdir -p $HOME/bin
50+ mv k3sup $HOME/bin/
51+ '''
52+ }
53+ }
54+
55+ stage('GCloud Auth & SSH Setup') {
56+ steps {
57+ withVault(vaultSecrets: [[
58+ path: 'secret/support/private/gcloud-service-account',
59+ secretValues: [
60+ [envVar: 'GOOGLE_APPLICATION_CREDENTIALS_JSON', vaultKey: 'credentials-file'],
61+ ]
62+ ]]) {
63+ sh '''#!/bin/bash
64+ # Write the credentials to a temporary file
65+ echo "${GOOGLE_APPLICATION_CREDENTIALS_JSON}" > /tmp/gcloud-key.json
66+
67+ gcloud auth activate-service-account --key-file /tmp/gcloud-key.json
68+ ssh-keygen -t ed25519 -f $HOME/.ssh/id_ed25519 -q -P ""
69+
70+ # Clean up the temporary file
71+ rm -f /tmp/gcloud-key.json
72+ '''
73+ }
74+ }
75+ }
76+
77+ stage('Create GCE Instances') {
78+ steps {
79+ sh '''#!/bin/bash
80+ # Function to find and read SSH public key
81+ get_ssh_public_key() {
82+ local ssh_dir="$HOME/.ssh"
83+ local public_key=""
84+
85+ # Look for common SSH public key files in order of preference
86+ for key_file in "id_ed25519.pub" "id_rsa.pub" "id_ecdsa.pub" "id_dsa.pub"; do
87+ if [ -f "$ssh_dir/$key_file" ]; then
88+ public_key=$(cat "$ssh_dir/$key_file" | tr -d '\\n\\r')
89+ echo "Found SSH public key: $ssh_dir/$key_file" >&2
90+ break
91+ fi
92+ done
93+
94+ if [ -z "$public_key" ]; then
95+ echo "Error: No SSH public key found in $ssh_dir" >&2
96+ echo "Please ensure you have one of the following files:" >&2
97+ echo " - $ssh_dir/id_ed25519.pub" >&2
98+ echo " - $ssh_dir/id_rsa.pub" >&2
99+ echo " - $ssh_dir/id_ecdsa.pub" >&2
100+ echo " - $ssh_dir/id_dsa.pub" >&2
101+ exit 1
102+ fi
103+
104+ echo "$public_key"
105+ }
106+
107+ SSH_PUBLIC_KEY="$(get_ssh_public_key)"
108+
109+ # Build disk policy parameter if set
110+ DISK_POLICY_PARAM=""
111+ if [ -n "${GCP_DISK_POLICY}" ]; then
112+ DISK_POLICY_PARAM="disk-resource-policy=${GCP_DISK_POLICY},"
113+ fi
114+
115+ for n in {1..4}; do
116+ node_name=k8s-ddc-ci-$n-$BUILD_NUMBER
117+ gcloud compute instances create $node_name \\
118+ --project=${GCP_PROJECT_ID} \\
119+ --zone=${GCP_ZONE} \\
120+ --machine-type=${GCP_MACHINE_TYPE} \\
121+ --network-interface=network-tier=PREMIUM,stack-type=IPV4_ONLY,subnet=${GCP_NETWORK_SUBNET} \\
122+ --maintenance-policy=MIGRATE \\
123+ --provisioning-model=STANDARD \\
124+ --metadata="ssh-keys=jenkins:${SSH_PUBLIC_KEY}" \\
125+ --service-account=${GCP_SERVICE_ACCOUNT} \\
126+ --scopes=https://www.googleapis.com/auth/devstorage.read_only,https://www.googleapis.com/auth/logging.write,https://www.googleapis.com/auth/monitoring.write,https://www.googleapis.com/auth/service.management.readonly,https://www.googleapis.com/auth/servicecontrol,https://www.googleapis.com/auth/trace.append \\
127+ --create-disk=auto-delete=yes,boot=yes,device-name=$node_name,${DISK_POLICY_PARAM}image=${GCP_IMAGE},mode=rw,size=${GCP_DISK_SIZE},type=pd-balanced \\
128+ --no-shielded-secure-boot \\
129+ --shielded-vtpm \\
130+ --shielded-integrity-monitoring \\
131+ --labels=goog-ec-src=vm_add-gcloud \\
132+ --reservation-affinity=any &
133+ done
134+ wait < <( jobs -p )
135+ sleep 60
136+ '''
137+ }
138+ }
139+
140+ stage('Setup K3s Cluster') {
141+ steps {
142+ sh '''#!/bin/bash
143+ for n in {1..4}; do
144+ node_name=k8s-ddc-ci-$n-$BUILD_NUMBER
145+ if [ "$n" -eq 1 ]; then
146+ MASTER_IP=$(gcloud compute instances describe $node_name --zone=${GCP_ZONE} --format='get(networkInterfaces[0].networkIP)')
147+ k3sup install --ip $MASTER_IP --user jenkins --ssh-key $HOME/.ssh/id_ed25519
148+ else
149+ IP=$(gcloud compute instances describe $node_name --zone=${GCP_ZONE} --format='get(networkInterfaces[0].networkIP)')
150+ k3sup join --ip $IP --server-ip $MASTER_IP --user jenkins --ssh-key $HOME/.ssh/id_ed25519
151+ fi
152+ done
153+
154+ mkdir -p $HOME/.kube
155+ mv kubeconfig $HOME/.kube/config
156+ '''
157+ }
158+ }
159+
160+ stage('Install Build Tools') {
161+ steps {
162+ sh '''#!/bin/bash
163+ wget https://go.dev/dl/go${GO_VERSION}.linux-amd64.tar.gz
164+ tar -C ../ -xzf go${GO_VERSION}.linux-amd64.tar.gz
165+ curl -LO https://dl.k8s.io/release/${KUBECTL_VERSION}/bin/linux/amd64/kubectl
166+ chmod +x kubectl
167+ mv kubectl $HOME/bin
168+ '''
169+ }
170+ }
171+
172+ stage('Build') {
173+ environment {
174+ KUBECONFIG = "${env.HOME}/.kube/config"
175+ }
176+ steps {
177+ sh './script/cibuild'
178+ }
179+ }
19180 }
20181}
0 commit comments