Sunday, 30 August 2015

jenkins

1. jenkins is a ci tool
  1. (optional) scm                                //where jenkin find source code to build, git etc
  2. (optional) trigger to build
  3. build script to perform the build    //ant, maven, shell script, batch file etc
  4. (optional) steps to notify                //email, im, issue tracker etc

2. install jenkins on windows
  1. download jenkins windows installer
  2. run installer will install jenkins as a windows wervice, and it runs on local SYSTEM account.  so it will use 'system environment variable' instead of 'user environment account variable', which will cause trouble (JAVA_HOME etc missing)
  3. also it will not allow interaction with desktop (selenium browser will run in the background)
  4. so instead, after run installer, stop jenkins windows service, and run jenkins as 'java -jar jenkins.war'

3. jenkins configuration
  1. jenkins configuration will read  environment variables from windows account
  2. jenkins allow user to change environment variables

4. build non source control code
  1. build empty project (will fail), but jenkins will create folder jenkins/workspace/PROJECTNAME/
  2. copy local project files to the above folder
  3. build again

5. jenkins email notification
  1. if using gmail, need to change 'Sign-in & Security' -> 'Signing into Google' -> 'Allow less secure apps: ON'

6. jenkinsfile (declarative and scripted syntax)
      pipeline {                                                                //declarative
          agent any
          stages {
              stage('Build') {
                  steps {
                      //
                  }
              }
              stage('Test') {
                  steps {
                      //
                  }
              }
          }
      }

      node {                                                                      //scripted
          stage('Build') {
              //
          }
          stage('Test') {
              //
          }
          stage('Deploy') {
              //
          }
      }

7. jenkinsfile
  • sh """ ... """                       //multiline, sh is a 'step' provide by plugin
  • def                                     //groovy, define var or func
  • readProperties                   //read file in java property file format

8. parameterise pipeline
  • use <JENKINS URL>/pipeline-syntax             //snippet generator
  • select 'properties' step to generate code

9. jenkins github integration webhook to jenkins
  • trigger build whenever commit pushed
  • use 'ngrok' if jenkins is localhost
  • on github side, config github-webhook
  • on jenkins side, github-webhook trigger build when commit happens

10. restart jenkins
  • (jenkins_url)/safeRestart
  • (jenkins_url)/restart

11. port diagnose
  • kill -9 $(lsof -ti tcp:4444)
  • netstat -vanp tcp | grep 3000
  • ps -p 1337 -o comm=
  • fuser -k 5000/tcp || true                    - kill process in jenkins, without true jenkins will fail

12. withCredentials/usernamePassword
  • it's a bind credentials to variable feature
  • update ssh key/certificate, and it's converted to env variable

13. schedule run with params
  • parameterizedCron(env.BRANCH_NAME == 'master' ? "0 8 * * * %TARGET_ENV=staging;TEST_GROUP=All;TEST_COVERAGE=Regression" : '')

references
1. my software builds on my computer but not on jenkins

No comments:

Post a Comment