sh 分析管道

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh 分析管道相关的知识,希望对你有一定的参考价值。

FROM maven:3.6.1-jdk-13-alpine AS build

COPY . /app/

WORKDIR /app

ARG goal

RUN mvn -U --batch-mode -s settings.xml clean $goal

RUN mvn  dependency:get     -DrepoUrl=http://artifactory:8081/artifactory/libs-snapshot-local/     -Dartifact=com.lidar:simulator:99-SNAPSHOT -Ddest=./target
RUN mvn  dependency:get     -DrepoUrl=http://artifactory:8081/artifactory/libs-snapshot-local/     -Dartifact=com.lidar:telemetry:99-SNAPSHOT -Ddest=./target

FROM openjdk:8u212-slim

RUN apt-get update && apt-get install -y curl && apt-get install -y wget

COPY --from=build /app/target /app/

WORKDIR /app

RUN wget http://gitlab/root/testing/raw/master/tests-full.txt
RUN wget http://gitlab/root/testing/raw/master/tests-sanity.txt

COPY ./entrypoint.sh .

RUN chmod +x ./entrypoint.sh

ENTRYPOINT [ "./entrypoint.sh" ]
pipeline {
    agent any

    tools { 
        maven 'maven' 
        jdk 'jdk8' 
    }
    options {
        gitLabConnection('gitlab')
        timestamps()
    }
    triggers {
        gitlab(triggerOnPush: true, triggerOnMergeRequest: true, branchFilterType: 'All')
    }

    stages {
        stage('Build'){
            parallel{
                stage('Build - Master') {
                    // handles master branch
                    when {
                        branch 'master'
                    }
                    steps {
                        echo 'Building from [Master Branch]'

                        configFileProvider([configFile(fileId: 'artifactory', variable: 'MAVEN_SETTINGS_XML')]) {
                            sh 'cp ${MAVEN_SETTINGS_XML} ./settings.xml'
                            sh 'docker build --build-arg goal=package -t analytics_builder --network=leader_network .'
                        }
                    }
                }
                stage('Build - Feature') {
                    // handles feature branches
                    when {
                        expression {
                            BRANCH_NAME ==~ /feature\/.+/ 
                        }
                    }
                    steps {
                        echo 'Building from [Feature Branch] - ' + env.BRANCH_NAME
                        configFileProvider([configFile(fileId: 'artifactory', variable: 'MAVEN_SETTINGS_XML')]) {
                            sh 'cp ${MAVEN_SETTINGS_XML} ./settings.xml'
                            sh 'docker build --build-arg goal=package -t analytics_builder --network=leader_network .'
                        }
                    }
                }
                stage('Build - Release') {
                    // handles release branches
                    when {
                        expression {
                            BRANCH_NAME ==~ /release\/[0-9]+\.[0-9]+/  
                        }
                    }
                    steps {
                        echo 'Building from [Release Branch] - ' + env.BRANCH_NAME

                        echo 'Determining latest tag...'

                        configFileProvider([configFile(fileId: 'artifactory', variable: 'MAVEN_SETTINGS_XML')]) {
                        sh 'cp ${MAVEN_SETTINGS_XML} ./settings.xml'
                        sh 'docker build --build-arg goal=package -t analytics_builder --network=leader_network .'
                        } 
                    }
                }
            }
        }
        stage('Test') {
            // TODO: if master test
            //       if feature check commit message, if has #e2e test
            parallel{
                stage('Test - Master') {
                    // handles master branch
                    when {
                        branch 'master'
                    }
                    steps {
                        echo 'Testing..'
                        sh 'docker run --name analytics_tester --network=leader_network analytics_builder sanity'
                        sh 'docker container rm -f analytics_tester'
                    }
                }
                stage('Test - Feature') {
                    // handles feature branches
                    when {
                        expression {
                            BRANCH_NAME ==~ /feature\/.+/ 
                        }
                        // expression {
                        //     sh(returnStdout: true, script: '$(echo $(git log -1 --pretty=%B) | grep "#e2e" | wc -l)').trim() == '1'
                        // }
                    }
                    steps {
                        echo 'Testing [Feature Branch] - ' + env.BRANCH_NAME
                        sh  '''
                            COMMIT_MESSAGE=$(echo $(git log -1 --pretty=%B))
                            echo "Commit message: $COMMIT_MESSAGE"

                            case "$COMMIT_MESSAGE" in
                                *#e2e*) docker run --name analytics_tester --network=leader_network analytics_builder e2e;
                                        docker container rm -f analytics_tester ;;
                                *)      docker run --name analytics_tester --network=leader_network analytics_builder sanity;
                                        docker container rm -f analytics_tester ;;
                            esac
                            '''
                        configFileProvider([configFile(fileId: 'artifactory', variable: 'MAVEN_SETTINGS_XML')]) {
                            sh 'cp ${MAVEN_SETTINGS_XML} ./settings.xml'
                        }
                    }
                }
            }
        }
        stage('Deploy') {
            parallel{
                stage('Deploy - Master') {
                    when {
                        branch 'master'
                    }
                    steps {
                        configFileProvider([configFile(fileId: 'artifactory', variable: 'MAVEN_SETTINGS_XML')]) {
                            sh 'cp ${MAVEN_SETTINGS_XML} ./settings.xml'
                            sh 'docker build --build-arg goal=deploy -t analytics_builder --network=leader_network .'
                        }
                    }
                }
                stage('Deploy - Release') {
                    when {
                        expression {
                            BRANCH_NAME ==~ /release\/[0-9]+\.[0-9]+/  
                        }
                    }
                    steps {
                        echo 'Deploying ' + env.BRANCH_NAME

                        echo 'Determining latest tag...'

                        configFileProvider([configFile(fileId: 'artifactory', variable: 'MAVEN_SETTINGS_XML')]) {
                            withCredentials(bindings: [sshUserPrivateKey(credentialsId: 'gitlab_ssh', \
                                        keyFileVariable: 'ssh_key')]){
                                sh label: 'tag', 
                                script: ''' LAST_TAG=$(curl --header "PRIVATE-TOKEN: Skg1qx7hrR-sKVn6zBfx" -s http://gitlab/api/v4/projects/3/repository/tags/ | python -c "import sys, json; print(json.load(sys.stdin)[0][\'name\'])" || echo 0.0.0)
                                            
                                            RELEASE_NUMBER=$(echo $BRANCH_NAME | cut -d \'/\' -f 2)
                                            echo "Current branch - $BRANCH_NAME (Release $RELEASE_NUMBER)"

                                            echo "Last version is: $LAST_TAG"

                                            echo "Determining next tag..."
                                            LAST_RELEASE=$(echo $LAST_TAG | cut -d \'.\' -f 1,2 | cut -c 2-)
                                            echo "Previous release was $LAST_RELEASE, current one is $RELEASE_NUMBER."
                                            if [ "$LAST_RELEASE" = "$RELEASE_NUMBER" ]
                                            then
                                                LAST_PATCH_VERSION=$(echo $LAST_TAG | cut -d \'.\' -f 3)
                                                NEXT_PATCH_VERSION=$(($LAST_PATCH_VERSION + 1))
                                                
                                                echo "It is the same release, incrementing patch version from $LAST_PATCH_VERSION to $NEXT_PATCH_VERSION."
                                                NEXT_TAG="v$RELEASE_NUMBER.$NEXT_PATCH_VERSION"
                                            else
                                                echo "It is a new release!"
                                                NEXT_TAG="v$RELEASE_NUMBER.0"
                                            fi
                                            #NEXT_TAG=$(echo $LAST_TAG | awk -F. -v OFS=. \'NF==1{print ++$NF}; NF>1{if(length($NF+1)>length($NF))$(NF-1)++; $NF=sprintf("%0*d", length($NF), ($NF+1)%(10^length($NF))); print}\')

                                            echo "Next tag is $NEXT_TAG"

                                            echo "Configurating git"
                                            git remote set-url origin git@gitlab:root/analytics.git
                                            eval $(ssh-agent) && ssh-add ${ssh_key}
                                            git config --global user.email "argamanza@gmail.com"
                                            git config --global user.name "Tzahi Argaman"

                                            echo "Updating version in POM file"
                                            NEXT_VERSION=$(echo $NEXT_TAG | cut -c 2-)
                                            #git checkout -b release/$NEXT_VERSION
                                            mvn versions:set -DnewVersion=$NEXT_VERSION

                                            echo "Writing version number into file"
                                            echo $NEXT_VERSION > v.txt
                                            
                                            # echo "Commiting changes"
                                            # git commit -am 'release version $NEXT_TAG'

                                            # echo "Pushing Changes"
                                            # git push origin release/$NEXT_VERSION

                                            echo 'Executing Maven Package'
                                            docker build --build-arg goal=package -t analytics_builder --network=leader_network .

                                            echo "Reverting changes in POM file"
                                            mvn versions:revert

                                            echo "Tagging with $NEXT_TAG"
                                            git tag -a $NEXT_TAG -m "New release - $NEXT_TAG"

                                            echo "Pushing Tag"
                                            git push origin $NEXT_TAG

                                            echo "Creating release in GitLab"
                                            curl --header \'Content-Type: application/json\' --header "PRIVATE-TOKEN: Skg1qx7hrR-sKVn6zBfx" --data \'{ "name": "New release", "tag_name": "\'$NEXT_TAG\'", "description": "New release", "assets": { "links": [{ "name": "some asset", "url": "https://google.com" }] } }\' --request POST http://gitlab/api/v4/projects/3/releases
                                        '''
                            }
                        }
                    }
                }
            }
        }
    }

    post {
      failure {
        updateGitlabCommitStatus name: 'build', state: 'failed'
      }
      success {
        updateGitlabCommitStatus name: 'build', state: 'success'
      }
    }
}
#!/usr/bin/env bash
echo "Starting tests..."
mv tests-$1.txt tests.txt

java -cp analytics-99-SNAPSHOT.jar:telemetry-99-SNAPSHOT.jar:simulator-99-SNAPSHOT.jar com.lidar.simulation.Simulator

#java -cp <jar1>:<jar2>:<jar3> com.lidar.simulation.Simulator

以上是关于sh 分析管道的主要内容,如果未能解决你的问题,请参考以下文章

Jenkins:管道 sh 错误替换错误

Jenkins 管道 sh 在 Windows 上因“无法运行程序 nohup”而失败

/bin/sh:如何从 &3 重定向到命名管道?

sh 使用管道脚本添加时间戳

sh bash管道过滤器

sh 管道