Add the script for Jenkins setup 14/1714/1
authorchengli3 <cheng1.li@intel.com>
Mon, 9 Sep 2019 02:17:14 +0000 (10:17 +0800)
committerKuralamudhan Ramakrishnan <kuralamudhan.ramakrishnan@intel.com>
Wed, 2 Oct 2019 20:43:10 +0000 (13:43 -0700)
This patch is to add the script/ansible_playbook for Jenkins setup:
1. Setup jenkins server by leverage jenkins role from galaxy
2. Add akraino gerrit server to gerrit list
3. Add pipeline job for ICN CI check

Change-Id: I2baf2a35ba246faab8a7be599af7bd5b2123dd79

Jenkinsfile
ci/README.md [new file with mode: 0644]
ci/gerrit.groovy [new file with mode: 0644]
ci/icn_check.xml [new file with mode: 0644]
ci/setup_jenkins.sh [new file with mode: 0755]
ci/site_jenkins.yaml [new file with mode: 0644]

index b7b5c7f..895065a 100644 (file)
@@ -9,8 +9,22 @@ pipeline {
     stages {
         stage("Build ISO Image") {
             steps {
-                echo "refspec: ${GERRIT_REFSPEC}, branch: ${GERRIT_BRANCH}"
+                sh "sudo rm -rf icn build/ubuntu icn-ubuntu-18.04.iso"
+                sh "git clone https://gerrit.akraino.org/r/icn"
+                dir("icn") {
+                    sh "git fetch origin ${GERRIT_REFSPEC}:${changeBranch}"
+                    sh "git checkout ${changeBranch}"
+                    sh "git rebase origin/${GERRIT_BRANCH}"
+                }
+                sh "sudo icn/tools/setup_build_machine.sh"
+                // sh "sudo icn/tools/collect.sh"
+                sh "sudo icn/tools/create_usb_bootable.sh"
             }
         }
     }
+    post {
+        always {
+            archiveArtifacts artifacts: "icn-ubuntu-18.04.iso", onlyIfSuccessful: true
+        }
+    }
 }
diff --git a/ci/README.md b/ci/README.md
new file mode 100644 (file)
index 0000000..c368dc3
--- /dev/null
@@ -0,0 +1,24 @@
+# Setup a private Jenkins server from a refresh ubuntu
+
+
+**Note:** As we don't support that downloading packages in sandbox for now,
+it means that the packages are all downloaded directely from the jenkins
+server. So that the jenkins server must have the same OS version with ICN
+nodes. Currently, it's ubuntu 18.04 with kernel version 4.15.0-45-generic.
+
+## How to setup jenkins server
+
+Put the gerrit ssh key under `icn/ci/gerrit.key`
+The default listening address is the default ip address of the Jenkins server.
+To override the listening address/domain name, use variable `jenkins_hostname`.
+The default Jenkins username/password is `admin/admin`. To overrides it, use variables
+`jenkins_admin_username` and `jenkins_admin_password`.
+
+```bash
+git clone "https://gerrit.akraino.org/r/icn" # may need to switch the branch based on your case
+cd icn/ci
+sudo ./setup_jenkins.sh
+sudo ansible-playbook site_jenkins.yaml -v
+```
+
+Once the playbook is successful, we can visite the jenkins server at http://<listen_address>:8080.
diff --git a/ci/gerrit.groovy b/ci/gerrit.groovy
new file mode 100644 (file)
index 0000000..38d3076
--- /dev/null
@@ -0,0 +1,34 @@
+import jenkins.*
+import jenkins.model.*
+import hudson.*
+import hudson.model.*
+import com.sonyericsson.hudson.plugins.gerrit.trigger.config.Config
+import com.sonyericsson.hudson.plugins.gerrit.trigger.GerritServer
+import com.sonyericsson.hudson.plugins.gerrit.trigger.PluginImpl
+
+
+def gerrit_server_name = 'gerrit.akraino.org'
+def gerrit_hostname = 'gerrit.akraino.org'
+def gerrit_key_path = '/var/lib/jenkins/.ssh/gerrit.key'
+def gerrit_url = 'https://gerrit.akraino.org/'
+def gerrit_username = 'icn.jenkins'
+
+if (PluginImpl.getInstance().getServer(gerrit_server_name) == null) {
+  GerritServer defaultServer = new GerritServer(gerrit_server_name)
+  Config config = defaultServer.getConfig()
+  PluginImpl.getInstance().addServer(defaultServer)
+  defaultServer.start()
+  // setting properties
+  config.setGerritHostName(gerrit_hostname)
+  config.setGerritFrontEndURL(gerrit_url)
+  config.setGerritUserName(gerrit_username)
+  config.setGerritAuthKeyFile(new File(gerrit_key_path))
+} else {
+  Config config = PluginImpl.getInstance().getServer(gerrit_server_name).getConfig()
+  config.setGerritHostName(gerrit_hostname)
+  config.setGerritFrontEndURL(gerrit_url)
+  config.setGerritUserName(gerrit_username)
+  config.setGerritAuthKeyFile(new File(gerrit_key_path))
+  PluginImpl.getInstance().save()
+}
+return "success"
diff --git a/ci/icn_check.xml b/ci/icn_check.xml
new file mode 100644 (file)
index 0000000..668f711
--- /dev/null
@@ -0,0 +1,105 @@
+<?xml version='1.1' encoding='UTF-8'?>
+<flow-definition plugin="workflow-job@2.35">
+  <actions>
+    <org.jenkinsci.plugins.pipeline.modeldefinition.actions.DeclarativeJobAction plugin="pipeline-model-definition@1.3.9"/>
+    <org.jenkinsci.plugins.pipeline.modeldefinition.actions.DeclarativeJobPropertyTrackerAction plugin="pipeline-model-definition@1.3.9">
+      <jobProperties/>
+      <triggers/>
+      <parameters/>
+      <options>
+        <string>skipDefaultCheckout</string>
+      </options>
+    </org.jenkinsci.plugins.pipeline.modeldefinition.actions.DeclarativeJobPropertyTrackerAction>
+  </actions>
+  <description>ICN CI job</description>
+  <keepDependencies>false</keepDependencies>
+  <properties>
+    <org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty>
+      <triggers>
+        <com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.GerritTrigger plugin="gerrit-trigger@2.30.0">
+          <spec></spec>
+          <gerritProjects>
+            <com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data.GerritProject>
+              <compareType>PLAIN</compareType>
+              <pattern>icn</pattern>
+              <branches>
+                <com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data.Branch>
+                  <compareType>ANT</compareType>
+                  <pattern>**</pattern>
+                </com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data.Branch>
+              </branches>
+              <disableStrictForbiddenFileVerification>false</disableStrictForbiddenFileVerification>
+            </com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data.GerritProject>
+          </gerritProjects>
+          <dynamicGerritProjects class="empty-list"/>
+          <skipVote>
+            <onSuccessful>false</onSuccessful>
+            <onFailed>false</onFailed>
+            <onUnstable>false</onUnstable>
+            <onNotBuilt>false</onNotBuilt>
+          </skipVote>
+          <gerritBuildSuccessfulVerifiedValue>0</gerritBuildSuccessfulVerifiedValue>
+          <gerritBuildSuccessfulCodeReviewValue>1</gerritBuildSuccessfulCodeReviewValue>
+          <gerritBuildFailedVerifiedValue>0</gerritBuildFailedVerifiedValue>
+          <gerritBuildFailedCodeReviewValue>-1</gerritBuildFailedCodeReviewValue>
+          <gerritBuildUnstableVerifiedValue>0</gerritBuildUnstableVerifiedValue>
+          <gerritBuildUnstableCodeReviewValue>-1</gerritBuildUnstableCodeReviewValue>
+          <silentMode>false</silentMode>
+          <notificationLevel></notificationLevel>
+          <silentStartMode>false</silentStartMode>
+          <escapeQuotes>true</escapeQuotes>
+          <nameAndEmailParameterMode>PLAIN</nameAndEmailParameterMode>
+          <dependencyJobsNames></dependencyJobsNames>
+          <commitMessageParameterMode>BASE64</commitMessageParameterMode>
+          <changeSubjectParameterMode>PLAIN</changeSubjectParameterMode>
+          <commentTextParameterMode>BASE64</commentTextParameterMode>
+          <buildStartMessage></buildStartMessage>
+          <buildFailureMessage>ci failed</buildFailureMessage>
+          <buildSuccessfulMessage>ci pass</buildSuccessfulMessage>
+          <buildUnstableMessage></buildUnstableMessage>
+          <buildNotBuiltMessage></buildNotBuiltMessage>
+          <buildUnsuccessfulFilepath></buildUnsuccessfulFilepath>
+          <customUrl></customUrl>
+          <serverName>gerrit.akraino.org</serverName>
+          <triggerOnEvents>
+            <com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.events.PluginPatchsetCreatedEvent>
+              <excludeDrafts>false</excludeDrafts>
+              <excludeTrivialRebase>false</excludeTrivialRebase>
+              <excludeNoCodeChange>false</excludeNoCodeChange>
+              <excludePrivateState>false</excludePrivateState>
+              <excludeWipState>false</excludeWipState>
+            </com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.events.PluginPatchsetCreatedEvent>
+          </triggerOnEvents>
+          <dynamicTriggerConfiguration>false</dynamicTriggerConfiguration>
+          <triggerConfigURL></triggerConfigURL>
+          <triggerInformationAction/>
+        </com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.GerritTrigger>
+      </triggers>
+    </org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty>
+  </properties>
+  <definition class="org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition" plugin="workflow-cps@2.74">
+    <scm class="hudson.plugins.git.GitSCM" plugin="git@3.12.1">
+      <configVersion>2</configVersion>
+      <userRemoteConfigs>
+        <hudson.plugins.git.UserRemoteConfig>
+          <name>origin</name>
+          <url>https://gerrit.akraino.org/r/icn.git</url>
+        </hudson.plugins.git.UserRemoteConfig>
+      </userRemoteConfigs>
+      <branches>
+        <hudson.plugins.git.BranchSpec>
+          <name>origin/${GERRIT_BRANCH}</name>
+        </hudson.plugins.git.BranchSpec>
+      </branches>
+      <doGenerateSubmoduleConfigurations>false</doGenerateSubmoduleConfigurations>
+      <submoduleCfg class="list"/>
+      <extensions>
+        <hudson.plugins.git.extensions.impl.CleanBeforeCheckout/>
+      </extensions>
+    </scm>
+    <scriptPath>Jenkinsfile</scriptPath>
+    <lightweight>true</lightweight>
+  </definition>
+  <triggers/>
+  <disabled>false</disabled>
+</flow-definition>
\ No newline at end of file
diff --git a/ci/setup_jenkins.sh b/ci/setup_jenkins.sh
new file mode 100755 (executable)
index 0000000..dfb523e
--- /dev/null
@@ -0,0 +1,4 @@
+export DEBIAN_FRONTEND=noninteractive
+apt update
+apt install -y ansible
+ansible-galaxy install geerlingguy.jenkins,3.7.0 --roles-path /etc/ansible/roles
diff --git a/ci/site_jenkins.yaml b/ci/site_jenkins.yaml
new file mode 100644 (file)
index 0000000..1510fc8
--- /dev/null
@@ -0,0 +1,89 @@
+- hosts: localhost
+  vars:
+    jenkins_hostname: "{{ ansible_default_ipv4.address }}"
+    jenkins_version: 2.192
+    jenkins_plugin_timeout: 360
+    jenkins_plugins:
+      - gerrit-trigger
+      - workflow-aggregator
+      - git
+  roles:
+    - role: geerlingguy.java
+      become: yes
+    - role: geerlingguy.jenkins
+      become: yes
+  tasks:
+    - name: restart jenkins to use new installed plugins
+      meta: flush_handlers
+
+    - name: Wait for Jenkins to start up before proceeding.
+      command: >
+        curl -D - --silent --max-time 5 http://{{ jenkins_hostname }}:{{ jenkins_http_port }}{{ jenkins_url_prefix }}/cli/
+      args:
+        warn: false
+      register: result
+      until: >
+        (result.stdout.find("403 Forbidden") != -1)
+        or (result.stdout.find("200 OK") != -1)
+        and (result.stdout.find("Please wait while") == -1)
+      retries: "{{ jenkins_connection_retries | default(60) }}"
+      delay: "{{ jenkins_connection_delay | default(5) }}"
+      changed_when: false
+      check_mode: false
+
+    - name: create directory /var/lib/jenkins/.ssh
+      file:
+        path: /var/lib/jenkins/.ssh
+        owner: jenkins
+        group: jenkins
+        state: directory
+
+    - name: place gerrit ssh key
+      copy:
+        src: "{{ gerrit_key_path | default('gerrit.key') }}"
+        dest: /var/lib/jenkins/.ssh/gerrit.key
+        owner: jenkins
+        group: jenkins
+        mode: '0600'
+      tags: [gerrit]
+
+    - name: grant jenkins user sudo
+      copy:
+        content: "jenkins        ALL=(ALL)       NOPASSWD: ALL"
+        dest: /etc/sudoers.d/jenkins
+        owner: root
+        group: root
+        mode: '0660'
+      tags: [gerrit]
+
+    - name: add gerrit server
+      jenkins_script:
+        script: "{{lookup('file', 'gerrit.groovy')}}"
+        url: "http://{{ jenkins_hostname }}:{{ jenkins_http_port }}{{ jenkins_url_prefix }}"
+        user: "{{ jenkins_admin_username }}"
+        password: "{{ jenkins_admin_password }}"
+        validate_certs: no
+      register: result
+      failed_when: "'Result: success\n' != result.output"
+      tags: [gerrit]
+
+    - name: Install python-pip
+      apt:
+        name: python-pip
+      tags: [jobs]
+
+    - name: Install python-jenkins and lxml(required by jenkins_job module)
+      pip:
+        name:
+          - python-jenkins
+          - lxml
+      tags: [jobs]
+
+    - name: Add pipeline job for ICN CI
+      jenkins_job:
+        name: icn_check
+        config: "{{lookup('file', 'icn_check.xml')}}"
+        url: "http://{{ jenkins_hostname }}:{{ jenkins_http_port }}{{ jenkins_url_prefix }}"
+        user: "{{ jenkins_admin_username }}"
+        password: "{{ jenkins_admin_password }}"
+      tags: [jobs]