虚拟机中 Jenkins 搭CI

Jenkins Doc

Install Java8

$ sudo add-apt-repository ppa:webupd8team/java
$ sudo apt-get update
$ sudo apt-get install oracle-java8-installer
...
$ java -version
...
$ sudo apt-get install oracle-java8-set-default
$ vim ~/.bashrc

...
JAVA_HOME=/usr/lib/jvm/java-8-oracle
JRE_HOME=/usr/lib/jvm/java-8-oracle/jre
...

$ source ~/.bashrc
$ echo $JAVA_HOME

Install Git

$ sudo apt-get install git
$ git config --global user.name *******
$ git config --global user.email *****@thoughtworks.com

Add SSH Key

  • $ ssh-keygen
  • $ cat ~/.ssh/id_rsa.pub
  • add SSH Key to GitHub

Jenkins

> mkdir jenkins
> cd jenkins
> wget http://mirrors.jenkins.io/war-stable/latest/jenkins.war

Update Vagrantfile

config.vm.network "forwarded_port", guest: 8080, host: 8080, host_ip: "127.0.0.1"

Get Password

java -jar jenkins.war --httpPort=8080
...
cat /home/vagrant/.jenkins/secrets/initialAdminPassword

Visit Here

Install Plugins

  • suggested
  • customized

Upload Jenkinsfile to GitHub

pipeline {
    agent { docker 'maven:3.3.3' }
    stages {
        stage('build') {
            steps {
                sh 'mvn --version'
            }
        }
    }
}

Jenkins

  • New Item
  • Multibranch Pipeline
  • Add Source: GitHub
  • Save

Build

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                sh 'echo "Hello World"'
                sh '''
                    echo "Multiline shell steps works too"
                    ls -lah
                '''
            }
        }
    }
}
pipeline {
    agent any
    stages {
        stage('Deploy') {
            steps {
                retry(3) {
                    sh './flakey-deploy.sh'
                }

                timeout(time: 3, unit: 'MINUTES') {
                    sh './health-check.sh'
                }
            }
        }
    }
}

need chmod 777 ./flakey-deploy.sh or chmod +x ./flakey-deploy.sh.

pipeline {
    agent any
    stages {
        stage('Deploy') {
            steps {
                timeout(time: 3, unit: 'MINUTES') {
                    retry(5) {
                        sh './flakey-deploy.sh'
                    }
                }
            }
        }
    }
}
pipeline {
    agent any
    stages {
        stage('Test') {
            steps {
                sh 'echo "Fail!"; exit 1'
            }
        }
    }
    post {
        always {
            echo 'This will always run'
        }
        success {
            echo 'This will run only if successful'
        }
        failure {
            echo 'This will run only if failed'
        }
        unstable {
            echo 'This will run only if the run was marked as unstable'
        }
        changed {
            echo 'This will run only if the state of the Pipeline has changed'
            echo 'For example, if the Pipeline was previously failing but is now successful'
        }
    }
}

Environment Variables

before stages or in stage

environment {
    DISABLE_AUTH = 'true'
    ...
    
    TEST_CREDENTIAL_PWD = credentials('TEST_CREDENTIAL_ID')
    TEST_CREDENTIAL_TEXT_ID = credentials('TEST_CREDENTIAL_TEXT_ID')
}

Jenkins -> Credentials -> Add Credential

Gradle

$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

$ brew update && brew install gradle
$ gradle -v

build.gradle

apply plugin: 'idea'
//and standard one
apply plugin: 'java'

repositories {
    jcenter()
    mavenCentral()
}

task wrapper(type: Wrapper) {
    gradleVersion = '4.2'
}

dependencies {
    testCompile 'junit:junit:4.12'  // for unit test
}

Gradle Wrapper

$ gradle wrapper

$ ./gradlew task
$ ./gradlew cleanIdea
$ ./gradlew idea
$ ./gradlew build 

Change Jenkinsfile

build stage: ./gradlew build
test stage: ./gradlew check

Project Demo

感谢我的pair: 亚鑫

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,982评论 19 139
  • 使用SpringBoot+SpringCloud写了一套APP后台服务,也引入了当下比较流行的微服务的理念,模块也...
    CptHiro阅读 34,543评论 4 124
  • 本文接上篇,在VirtualBox+Vagrant搭建虚拟机成功后,一步步从零开始基于Jenkins平台搭建CI,...
    sunnyaxin阅读 1,075评论 5 3
  • 我看不清 看不清柳条的婆娑枝叶 看不清花瓣上的跳跃露珠 我看不清 看不清湖面的微波荡漾 看不清柱石上的小篆楷体 我...
    翱蓝阅读 435评论 0 1
  • 说起来,一个月前就想着为你庆祝生日,我的好闺蜜。也许我是把你看得比较重要,也许是我感性,也许我还想要让你觉得很温暖...
    lvyei723阅读 575评论 0 6