module JenkinsTools

Constants

GITLAB_PLUGIN
GIT_PLUGIN
WORKFLOW_PLUGIN
WORkFLOW_CPS_PLUGIN

Public Class Methods

delete!(job_name, base_url, user, token) click to toggle source
# File lib/citasks/ci_lib.rb, line 227
def self.delete! job_name, base_url, user, token
  # system %Q(curl -XPOST "#{base_url}/job/#{job_name}/doDelete" --user "#{user}:#{token}")
  req = RestClient::Request.new(
    method: :post,
    user: user,
    password: token,
    url: "#{base_url}/job/#{job_name}/doDelete",
    timeout: 30
  )

  begin
    res = req.execute
  rescue RestClient::ExceptionWithResponse => err
    case err.http_code
    when 301, 302, 307
      err.response.follow_redirection
    else
      raise
    end
  end
end
download_job(job_name, xml_file, base_url, user, token) click to toggle source
# File lib/citasks/ci_lib.rb, line 210
def self.download_job job_name, xml_file, base_url, user, token
  # system %Q(curl -s "#{base_url}/job/#{job_name}/config.xml" -o #{xml_file} --user "#{user}:#{token}")
  req = RestClient::Request.new(
    method: :get,
    user: user,
    password: token,
    url: "#{base_url}/job/#{job_name}/config.xml",
    timeout: 30
  )

  res = req.execute

  File.open xml_file, "w" do |fh|
    fh.puts res.body
  end
end
gen_jenkins_file() click to toggle source
# File lib/citasks/ci_lib.rb, line 108
  def self.gen_jenkins_file
    _write "Jenkinsfile", <<~EOF
      //A Jenkinsfile for start
      podTemplate(label: 'my-pod',
        containers:[
          containerTemplate(name: 'compiler', image:'#{ENV["COMPILER_DOCKER_IMAGE"]}',ttyEnabled: true, command: 'cat', envVars:[
              containerEnvVar(key: 'BUILD_NUMBER', value: env.BUILD_NUMBER),
              containerEnvVar(key: 'BUILD_ID', value: env.BUILD_ID),
              containerEnvVar(key: 'BUILD_URL', value: env.BUILD_URL),
              containerEnvVar(key: 'BUILD_TAG', value: env.BUILD_TAG),
              containerEnvVar(key: 'JOB_NAME', value: env.JOB_NAME)
            ],
          ),
          containerTemplate(name: 'citools', image:'zhiminwen/citools',ttyEnabled: true, command: 'cat', envVars:[
              // these env is only available in container template? podEnvVar deosn't work?!
              containerEnvVar(key: 'BUILD_NUMBER', value: env.BUILD_NUMBER),
              containerEnvVar(key: 'BUILD_ID', value: env.BUILD_ID),
              containerEnvVar(key: 'BUILD_URL', value: env.BUILD_URL),
              containerEnvVar(key: 'BUILD_TAG', value: env.BUILD_TAG),
              containerEnvVar(key: 'JOB_NAME', value: env.JOB_NAME)
            ],
          )
        ],
        volumes: [
          //for docker to work
          hostPathVolume(hostPath: '/var/run/docker.sock', mountPath: '/var/run/docker.sock')
        ]
      ){
        node('my-pod') {
          stage('check out') {
            checkout scm
          }

          stage('compile'){
            container('compiler'){
              sh "echo compile"
            }
          }

          stage('Docker Build'){
            container('citools'){
              // sleep 3600
              sh "echo build docker image"
              // sh "rake -f build.rb docker:01_build_image docker:02_push_to_ICp_registry"
            }
          }

          stage('Deploy to ICP'){
            container('citools'){
              // sleep 3600
              echo "deploy to icp..."
              // sh "rake -f build.rb k8s:01_deploy_to_k8s"
            }
          }

          //stage('Deployment'){
          //  parallel 'deploy to icp': {
          //    container('citools'){
          //      echo "deploy to icp..."
          //      // sh "rake -f build.rb k8s:01_deploy_to_k8s"

          //    }
          //  },

          //  'deploy to others': {
          //    container('citools'){
          //      echo "deploy to others..."
          //    }
          //  }
          //}
        }
      }
    EOF
  end
gen_job_xml(job_name, xml_file_name, git_repo_url, repo_credential_id_in_jenkins, secret_token=nil) click to toggle source

git_repo_url = virtuous-porcupine-gitlab-ce/wenzm/icp-static-web.git, gitlab-wenzm-password

# File lib/citasks/ci_lib.rb, line 35
  def self.gen_job_xml job_name, xml_file_name, git_repo_url, repo_credential_id_in_jenkins, secret_token=nil
    enable_secret_token = true

    if enable_secret_token
      secret_token = token_shared_persistently if secret_token.nil?
    end

    token_to_trigger_build_remotely = SecureRandom.uuid
    erb = ERB.new <<~EOF
      <?xml version='1.0' encoding='UTF-8'?>
      <flow-definition plugin="#{WORKFLOW_PLUGIN}">
        <actions/>
        <description>Workflow Created with template</description>
        <keepDependencies>false</keepDependencies>
        <properties>
          <com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty plugin="#{GITLAB_PLUGIN}">
            <gitLabConnection>gitlab</gitLabConnection>
          </com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty>
          <org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty>
            <triggers>
              <com.dabsquared.gitlabjenkins.GitLabPushTrigger plugin="#{GITLAB_PLUGIN}">
                <spec></spec>
                <triggerOnPush>true</triggerOnPush>
                <triggerOnMergeRequest>false</triggerOnMergeRequest>
                <triggerOnAcceptedMergeRequest>false</triggerOnAcceptedMergeRequest>
                <triggerOnClosedMergeRequest>false</triggerOnClosedMergeRequest>
                <triggerOpenMergeRequestOnPush>never</triggerOpenMergeRequestOnPush>
                <triggerOnNoteRequest>true</triggerOnNoteRequest>
                <noteRegex>Jenkins please build one more</noteRegex>
                <ciSkip>true</ciSkip>
                <skipWorkInProgressMergeRequest>true</skipWorkInProgressMergeRequest>
                <setBuildDescription>true</setBuildDescription>
                <branchFilterType>All</branchFilterType>
                <includeBranchesSpec></includeBranchesSpec>
                <excludeBranchesSpec></excludeBranchesSpec>
                <targetBranchRegex></targetBranchRegex>
                <% if enable_secret_token %>
                <secretToken><%= secret_token %></secretToken>
                <% end %>
              </com.dabsquared.gitlabjenkins.GitLabPushTrigger>
            </triggers>
          </org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty>
        </properties>
        <definition class="org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition" plugin="#{WORkFLOW_CPS_PLUGIN}">
          <scm class="hudson.plugins.git.GitSCM" plugin="#{GIT_PLUGIN}">
            <configVersion>2</configVersion>
            <userRemoteConfigs>
              <hudson.plugins.git.UserRemoteConfig>
                <url>#{git_repo_url}</url>
                <credentialsId>#{repo_credential_id_in_jenkins}</credentialsId>
              </hudson.plugins.git.UserRemoteConfig>
            </userRemoteConfigs>
            <branches>
              <hudson.plugins.git.BranchSpec>
                <name>*/master</name>
              </hudson.plugins.git.BranchSpec>
            </branches>
            <doGenerateSubmoduleConfigurations>false</doGenerateSubmoduleConfigurations>
            <submoduleCfg class="list"/>
            <extensions/>
          </scm>
          <scriptPath>Jenkinsfile</scriptPath>
          <lightweight>true</lightweight>
        </definition>
        <triggers/>
        <authToken>#{token_to_trigger_build_remotely}</authToken>
        <disabled>false</disabled>
      </flow-definition>
    EOF

    _write xml_file_name, erb.result(binding)
  end
post_new_job(job_name, xml_file, base_url, user, token) click to toggle source
# File lib/citasks/ci_lib.rb, line 183
def self.post_new_job job_name, xml_file, base_url, user, token
  # system %Q(curl -s -XPOST "#{base_url}/createItem?name=#{job_name}" --data-binary "@#{xml_file}" -H "Content-Type:text/xml" --user "#{user}:#{token}")
  req = RestClient::Request.new(
    method: :post,
    user: user,
    password: token,
    url: "#{base_url}/createItem?name=#{job_name}",
    timeout: 30,
    headers: {
      "Content-Type" => "text/xml",
    },
    payload: File.read(xml_file)
  )

  begin
    res = req.execute
  rescue RestClient::ExceptionWithResponse => err
    case err.http_code
    when 301, 302, 307
      err.response.follow_redirection
    else
      raise
    end
  end

end
trigger_build(job_name,build_token, base_url) click to toggle source
# File lib/citasks/ci_lib.rb, line 249
def self.trigger_build job_name,build_token, base_url
  # system %Q(curl "#{base_url}/job/#{job_name}/build?token=#{build_token}")
  req = RestClient::Request.new(
    method: :get,
    url: "#{base_url}/job/#{job_name}/build?token=#{build_token}",
    timeout: 30
  )
  res = req.execute
end