Ubuntu 上实现 Jenkins 自定义脚本执行
一 方式总览与选择
二 前置准备
三 方式一 Freestyle 项目执行自定义脚本
#!/usr/bin/env bash
set -euo pipefail
echo "Running custom script..."
chmod +x ./build.sh
./build.sh
python3 test.py
四 方式二 Pipeline 项目执行自定义脚本
pipeline {
agent any
stages {
stage('Checkout') {
steps { git 'https://github.com/your-org/your-repo.git' }
}
stage('Build') {
steps { sh './build.sh' }
}
stage('Test') {
steps { sh './run-tests.sh' }
}
stage('Report') {
steps {
junit '**/test-reports/*.xml'
publishHTML(target: [
allowMissing: false,
alwaysLinkToLastBuild: true,
keepAll: true,
reportDir: 'test-reports',
reportFiles: 'index.html',
reportName: 'Test Report'
])
}
}
}
}
五 方式三 远程执行与发布示例
ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa -q
ssh-copy-id user@target-host
#!/usr/bin/env bash
set -euo pipefail
APP="bird"
APP_PATH="/var/www/html"
HOSTS=("10.0.0.13" "10.0.0.14")
download() {
rm -rf "$APP"
git clone git@your-gitlab.com:dev/"$APP".git
}
deploy() {
for h in "${HOSTS[@]}"; do
scp -r "$APP"/* "$h:$APP_PATH/"
done
}
download
deploy
六 常见问题与最佳实践