虛擬機器中 Jenkins 搭CI

weixin_33866037發表於2017-09-27

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: 亞鑫

相關文章