bash 从“执行 shell”命令创建 Jenkinsfile

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46223912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:26:56  来源:igfitidea点击:

Create Jenkinsfile from "execute shell" commands

bashdockerjenkinsjenkins-pipeline

提问by Chris Welsh

I have a series of Execute shellboxes on a Jenkins build. After 3 days of Google and watching videos I need help. I am more a sysadmin than a coder so I'm having difficulty in creating a Jenkinsfile with the correct options and syntax. Can anyone advise? I need to create a pipeline. Anything in <name>is like that for security reasons, I have the real values in the files.

Execute shell在 Jenkins 构建上有一系列盒子。经过 3 天的谷歌和观看视频,我需要帮助。我更像是一个系统管理员而不是一个编码员,所以我很难用正确的选项和语法创建一个 Jenkinsfile。任何人都可以建议吗?我需要创建一个管道。<name>出于安全原因,其中的任何内容都是如此,我在文件中有实际值。

Execute shell

执行shell

mkdir -p deploy

mkdir -p deploy

Execute shell

执行shell

cp -R code/api deploy/
cp docker/Dockerfile.dev deploy/
(cd deploy/api/<Name>.<Name>.Web/ && aws s3 cp --recursive --region=eu-west-1 s3://config.<name>/audience-view/atg/dev/API/ .)

Execute shell

执行shell

cd deploy && docker build -t <name> -f Dockerfile.dev .

cd deploy && docker build -t <name> -f Dockerfile.dev .

Execute shell

执行shell

aws ecr get-login --region eu-west-1 > docker_login.sh && chmod +x 
docker_login.sh && ./docker_login.sh
docker tag <name>:latest 543573289192.dkr.ecr.eu-west-
1.amazonaws.com/<name>:latest
docker push <name>.dkr.ecr.eu-west-1.amazonaws.com/<name>:latest

Execute shell

执行shell

docker rmi audience-view-dev-api
docker rmi 543573289192.dkr.ecr.eu-west-1.amazonaws.com/<name>:latest

Execute shell

执行shell

RUNNING_TASKS=$(aws ecs list-tasks --region=eu-west-1 --cluster <name> --family <name> --query 'taskArns')
if [ "$RUNNING_TASKS" != "[]" ]; then
TASK_ARN=$(aws ecs list-tasks --region=eu-west-1 --cluster a<name> --family <name> --query 'taskArns[0]' | sed 's/\"//g')
aws ecs stop-task --region=eu-west-1 --cluster=<name> --task=$TASK_ARN --reason="Deployment from Jenkins" 
while [ $RUNNING_TASKS != "[]" ]; do
    sleep 5
    RUNNING_TASKS=$(aws ecs list-tasks --region=eu-west-1 --cluster <name> --family <name> --query 'taskArns')
done
fi

Execute shell

执行shell

TASK_ARN=$(aws ecs start-task --region=eu-west-1 --cluster <name> --task-definition <name> --container-instances 5f0c5b75-64a2-45cf-8ced-d6a6d13d2666 --query 'tasks[0].taskArn' | sed 's/arn:aws:ecs:eu-west-1:543573289192:task\///' | sed 's/\"//g')
TASK_STATUS=$(aws ecs describe-tasks --region=eu-west-1 --cluster <name> --tasks $TASK_ARN --query 'tasks[0].lastStatus')
while [ $TASK_STATUS == "PENDING" ]; do
echo $TASK_STATUS
TASK_STATUS=$(aws ecs describe-tasks --region=eu-west-1 --cluster <name> --tasks $TASK_ARN --query 'tasks[0].lastStatus' | sed 's/\"//g')
if [ $TASK_STATUS == "STOPPED" ]; then
    echo $(aws ecs describe-tasks --region=eu-west-1 --cluster <name> --tasks $TASK_ARN --query 'tasks[0].containers[0].exitCode')
    exit 1
fi
done

采纳答案by Rob Hales

Jenkins is best used as the glue to connect all the build pieces together, not the build script itself. As Alfe mentioned, it would be best to have this all in a shell script and then run the shell script with Jenkins.

Jenkins 最适合用作将所有构建部分连接在一起的粘合剂,而不是构建脚本本身。正如 Alfe 提到的,最好将这一切都放在一个 shell 脚本中,然后用 Jenkins 运行 shell 脚本。

BUT, if you really want to do this in a Pipeline job, it would look something like this (declarative pipeline):

但是,如果您真的想在流水线作业中执行此操作,它看起来像这样(声明式流水线):

pipeline {
    agent any

    stages {
        stage('setup') {
            steps {
                sh "mkdir -p deploy"
            }
        stage('nextStage') {
            steps {
                sh """
                    cp -R code/api deploy/
                    cp docker/Dockerfile.dev deploy/
                    (cd deploy/api/<Name>.<Name>.Web/ && aws s3 cp --recursive --region=eu-west-1 s3://config.<name>/audience-view/atg/dev/API/ .)
                """
            }
        }
        stage('anotherStage') {
            steps {
                echo "repeat for all your shell steps"
            }
        }
    }
}