From 9c022dd78ff0891d2bfa55f5bf6ffd63cb27a733 Mon Sep 17 00:00:00 2001 From: khaled Date: Wed, 30 Sep 2026 14:34:09 +0000 Subject: [PATCH] Upload files to "vars" --- vars/deployToK3s.groovy | 33 +++++++++++++++++++++++++++++++++ vars/publishArtifact.groovy | 26 ++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 vars/deployToK3s.groovy create mode 100644 vars/publishArtifact.groovy diff --git a/vars/deployToK3s.groovy b/vars/deployToK3s.groovy new file mode 100644 index 0000000..754e8c8 --- /dev/null +++ b/vars/deployToK3s.groovy @@ -0,0 +1,33 @@ +#!/usr/bin/env groovy +// Deploy a Deployment to k3s. +// Usage: deployToK3s(name: 'demo', image: 'nginx', replicas: 2) +def call(Map config = [:]) { + def name = config.name ?: error('deployToK3s: "name" is required') + def image = config.image ?: error('deployToK3s: "image" is required') + def replicas = config.replicas ?: 1 + def namespace = config.namespace ?: 'default' + stage("Deploy ${name}") { + writeFile file: 'deploy.yaml', text: """ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: ${name} + namespace: ${namespace} +spec: + replicas: ${replicas} + selector: + matchLabels: + app: ${name} + template: + metadata: + labels: + app: ${name} + spec: + containers: + - name: ${name} + image: ${image} +""" + sh 'kubectl apply -f deploy.yaml' + sh "kubectl rollout status deployment/${name} -n ${namespace} --timeout=90s" + } +} diff --git a/vars/publishArtifact.groovy b/vars/publishArtifact.groovy new file mode 100644 index 0000000..127ac0e --- /dev/null +++ b/vars/publishArtifact.groovy @@ -0,0 +1,26 @@ +#!/usr/bin/env groovy +// Upload a file to Artifactory. Shows the HTTP code and fails on >= 400. +// Usage: publishArtifact(file: 'app.tar.gz', path: 'demo/1.0.0') +def call(Map config = [:]) { + def file = config.file ?: error('publishArtifact: "file" is required') + def path = config.path ?: error('publishArtifact: "path" is required') + def repo = config.repo ?: 'lab-generic-local' + def base = config.base ?: 'http://10.0.1.4:8082/artifactory' + container('build') { + stage('Publish to Artifactory') { + withCredentials([string(credentialsId: 'artifactory-token', variable: 'ART_TOKEN')]) { + sh """ + HTTP=\$(curl -s -o /tmp/resp.txt -w '%{http_code}' \ + -H "Authorization: Bearer \${ART_TOKEN}" \ + -T ${file} \ + ${base}/${repo}/${path}/${file}) + echo "Artifactory responded HTTP \${HTTP}" + cat /tmp/resp.txt + [ "\${HTTP}" -lt 400 ] || exit 1 + echo "" + echo "Published: ${base}/${repo}/${path}/${file}" + """ + } + } + } +}