極客時間運維進階訓練營第五週作業

1、完全基於 Pipeline 實現完整的代碼部署流水線

#!groovy
pipeline {
    agent any
    // agent { label 'jenkins-slave01' }
    options {
        buildDiscarder(logRotator(numToKeepStr: '5'))
        disableConcurrentBuilds()
        timeout(time: 5, unit: 'MINUTES') //任務執行超時時間,默認單位小時
        timestamps()  //在控制檯顯示命令執行時間,格式10:33:29
        retry(2) //流水線構建失敗後重置次數爲2次
    }
    // 聲明環境變量
    environment {
        // def GIT_URL = 'https://gitlab.iclinux.com/linux/app1.git'
        // def HARBOR_URL = 'harbor.iclinux.com'
        // def IMAGE_PROJECT = 'app'
        // def IMAAGE_NAME = 'nginx'
        def IMAGE_BUILD_NODE="192.168.56.201"
        def DOCKER_NODE="192.168.56.202"
        def DATE = sh(script: "date +%F_%H-%M-%S", returnStdout: true).trim()

    }
    // 參數定義
    parameters {
        string(name: 'BRANCH', defaultValue: 'develop', description: 'branck select')
        choice(name: 'DEPLOY_ENV', choices: ['develop', 'production'], description: 'deploy env')
    }
    // 流水線開始工作
    stages {
        stage('code clone'){
            // agent { label 'master' }
            steps {
                deleteDir()  //清空目錄
                script {
                    if ( env.BRANCH == 'main' ){
                        git branch: 'main', credentialsId: 'root', url: 'https://gitlab.iclinux.com/linux/app1.git'

                    } else if ( env.BRANCH == 'develop' ) {
                        git branch: 'develop', credentialsId: 'root', url: 'https://gitlab.iclinux.com/linux/app1.git'

                    } else {
                        echo 'BRANCH ERROR, please check it.'
                    }
                GIT_COMMIT_TAG = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim()
                }
                // echo "${GIT_COMMIT_TAG}"
            }
        }

        stage('sonarqube-scanner'){
            // agent { label 'master' }
            steps{
                sh 'pwd'
                dir('/var/lib/jenkins/workspace/pipeline-groovy-test') {
                    // some block
                    sh '/apps/sonar-scanner/bin/sonar-scanner -Dsonar.projectKey=iclinx -Dsonar.projectName=iclinux-python-app3 -Dsonar.projectVersion=1.3 -Dsonar.sources=./src -Dsonar.language=py -Dsonar.sourceEncoding=UTF-8'
                }
            }
        }

        stage('code build'){
            steps{
                dir('/var/lib/jenkins/workspace/pipeline-groovy-test'){
                    sh 'tar cvzf frontend.tar.gz ./index.html ./images'
                }
            }
        }

        stage('file sync'){
            steps {
                echo "file sync"
                sh "scp frontend.tar.gz root@${IMAGE_BUILD_NODE}:/opt/dockerfiles/app/web1"
            }

        }

        stage("image build"){
            steps{
                echo "sync file to docker images server and make docker image"
                sh """
                    ssh root@${IMAGE_BUILD_NODE} "cd /opt/dockerfiles/app/web1 && bash build-command.sh ${GIT_COMMIT_TAG}-${DATE}"
                """
            }
        }

        stage('docker-compose image update'){
            steps{
                sh """
                    #ssh [email protected] "echo 123"
                    ssh root@${DOCKER_NODE} "cd /opt/iclinux-web && sed -i  's#image: harbor.iclinux.com/app/iclinux-web1:.*#image: harbor.iclinux.com/app/iclinux-web1:${GIT_COMMIT_TAG}-${DATE}#' docker-compose.yml && sleep 1 && docker-compose pull"
                    """

            }
        }

        stage('docker-compose app update'){
            steps{
                echo 'update app'
                sh """
                    ssh root@${DOCKER_NODE} "cd /opt/iclinux-web && docker-compose  up -d"
                """
            }
        }

        stage('send email'){
            steps {
                sh 'echo send email'
            }
            post {
                always{
                    script {
                    mail to: '[email protected]',
                    subject: "Pipeline Name: ${currentBuild.fullDisplayName}",
                    body: " ${env.JOB_NAME} -Build Number-${env.BUILD_NUMBER}\n 點擊鏈接 ${env.BUILD_URL} 查看詳情"
                    }

                }
            }
        }

    }
}

  


2、熟悉 ELK 各組件的功能、Elasticsearch 的節點角色類型


3、熟悉索引、doc、分片與副本的概念


4、掌握不同環境的 ELK 部署規劃,基於 deb 或二進制部署 Elasticsearch 集羣


5、瞭解 Elasticsearch API 的簡單使用,安裝 head 插件管理 ES 的數據


6、安裝 Logstash 收集不同類型的系統日誌並寫入到 ES 的不同 index


7、安裝 Kibana、查看 ES 集羣的數據

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章