ubuntu

Jenkins如何在Ubuntu上实现自定义脚本执行

小樊
42
2025-12-08 13:02:45
栏目: 智能运维

在 Ubuntu 上使用 Jenkins 执行自定义脚本的完整指南

一 环境准备与前提

二 方式一 Pipeline 项目执行脚本

pipeline {
  agent any

  stages {
    stage('Build') {
      steps {
        sh './build_script.sh'
      }
    }
    stage('Test') {
      steps {
        sh './test_script.sh'
      }
    }
    stage('Deploy') {
      steps {
        sh './deploy_script.sh'
      }
    }
  }
}
chmod +x *.sh

三 方式二 Freestyle 项目执行脚本

#!/usr/bin/env bash
set -euo pipefail
echo "Running custom script..."
./your_script.sh

四 实用场景示例 拉取代码并发布到远程 Web 服务器

ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa -q
ssh-copy-id 10.0.0.13
ssh-copy-id 10.0.0.14
#!/usr/bin/env bash
set -euo pipefail

HOST_LIST="10.0.0.13 10.0.0.14"
APP=bird
APP_PATH=/var/www/html

download() {
  rm -rf "${APP}"
  git clone git@www.yinzhengjie.com:dev/bird.git
}

deploy() {
  for i in ${HOST_LIST}; do
    scp -r "${APP}"/* "${i}:${APP_PATH}/"
  done
}

download
deploy

五 常见问题与最佳实践

0
看了该问题的人还看了