#51 Allocate openshift project to be used by pre/post merge workflow
Merged 4 years ago by mkovarik. Opened 4 years ago by mkovarik.
mkovarik/c3i-library request_project_for_build  into  master

file modified
+2 -2
@@ -31,7 +31,7 @@ 

  Workflows

  ---------

  ### Pre-merge

- Job is listening on PR changes - new, update, rebase, reopen. New changes are checkouted into current directory. Pagure PR is marked as `Pending` in pagure. Snippet `c3i_build_and_test_snippet` is executed. Test results should be sent as part of `c3i_build_and_test_snippet`. Pagure PR state is updated. Temporary images are deleted.

+ Job is listening on PR changes - new, update, rebase, reopen. New changes are checkouted into current directory. New C3IaaS project is requested and provided via `PIPELINE_ID` variable. Pagure PR is marked as `Pending` in pagure. Snippet `c3i_build_and_test_snippet` is executed. Test results should be sent as part of `c3i_build_and_test_snippet`. Pagure PR state is updated. Temporary images are deleted.

  

  ### Post-merge

  Job is listening on commits in master branch. New changes are checkouted into current directory. Pagure PR is marked as `Pending` in pagure. Snippet `c3i_build_and_test_snippet` is executed. Test results should be sent as
@@ -56,7 +56,7 @@ 

  ### c3i\_build\_and\_test\_snippet

  Path to snippet for Build container and test acceptance for 'latest' tag.

  

- Git repository is checkout into current working directory.

+ Git repository is checkout into current working directory. New openshift project is created and available in `PIPELINE_ID` variable.

  

  Input parameters are defined in [templates/build.yml](templates/build.yml). Input parameters can be extended by `c3i_build_custom_parameters`, eg.:

  ```yaml

file modified
+7 -4
@@ -51,9 +51,6 @@ 

  # Create service accounts and role bindings - can be done only by admin

  c3i_skip_service_accounts: false

  

- c3i_imagestream_namespace: "{{ c3i_ocp_namespace }}"

- c3i_imagestream_name: "{{ c3i_component }}"

- 

  c3i_quay_address: quay.io

  c3i_quay_namespace: factory2

  
@@ -69,7 +66,13 @@ 

  

  c3i_tracked_container_repo: "{{ c3i_quay_address }}/{{ c3i_quay_namespace }}/{{ c3i_component }}"

  

- c3i_pipeline_as_a_service_namespace: c3i 

+ c3i_c3iaas_default_lifetime: 60

+ c3i_c3iaas_build_lifetime: "{{ c3i_c3iaas_default_lifetime }}"

+ c3i_c3iaas_integration_test_lifetime: "{{ c3i_c3iaas_default_lifetime }}"

+ 

+ c3i_c3iaas_namespace: c3i

+ c3i_c3iaas_job_name: c3iaas-request-project

+ c3i_pipeline_as_a_service_namespace: "{{ c3i_c3iaas_namespace }}"

  c3i_messaging_provider: Red Hat UMB

  c3i_fedmsg_provider: fedmsg

  c3i_cloud_name: openshift

@@ -43,7 +43,7 @@ 

            name: params.MESSAGING_FEDMSG_PROVIDER,

            overrides: [topic: "{{ topic }}"],

            checks: [

-             [field: '$.repo.url_path', expectedValue: params.GIT_REPO.split('/')[3..-1].join('/').replace('forks/', 'fork/').replaceAll(/.git$/, '')],

+             [field: '$.repo.url_path', expectedValue: "^${params.GIT_REPO.split('/')[3..-1].join('/').replace('forks/', 'fork/').replaceAll(/.git$/, '')}\$"],

              [field: '$.branch', expectedValue: params.GIT_MAIN_BRANCH],

            ]

          )
@@ -144,6 +144,36 @@ 

        }

      }

      {% else %}

+     stage('Allocate C3IaaS project') {

+       steps {

+         script {

+           if (env.PR_NO) {

+             env.PIPELINE_ID = "c3i-{{ c3i_component }}-pr-${env.PR_NO}-git${env.GIT_COMMIT.take(8)}-${currentBuild.id}"

+           } else {

+             env.PIPELINE_ID = "c3i-{{ c3i_component }}-${env.GIT_REPO_REF}-git${env.GIT_COMMIT.take(8)}-${currentBuild.id}"

+           }

+           echo "Requesting new OpenShift project ${env.PIPELINE_ID}..."

+           openshift.withCluster() {

+             openshift.withProject(params.C3IAAS_NAMESPACE) {

+               c3i.buildAndWait(script: this, objs: "bc/${params.C3IAAS_JOB_NAME}",

+                 '-e', "PROJECT_NAME=${env.PIPELINE_ID}",

+                 '-e', "ADMIN_GROUPS=system:serviceaccounts:${env.TRIGGER_NAMESPACE},system:serviceaccounts:${env.C3IAAS_NAMESPACE}",

+                 '-e', "LIFETIME_IN_MINUTES=${params.C3IAAS_LIFETIME}"

+               )

+             }

+           }

+         }

+       }

+       post {

+         success {

+           echo "Allocated project ${env.PIPELINE_ID}"

+         }

+         failure {

+           echo "Failed to allocate ${env.PIPELINE_ID} project"

+         }

+       }

+     }

+ 

      {{ task_var_build_and_test }}

      stage('Push container') {

        when {
@@ -187,19 +217,6 @@ 

      }

    }

    post {

-     cleanup {

-       script {

-         if (env.RESULTING_TAG) {

-           echo "Removing tag ${env.RESULTING_TAG} from the ImageStream..."

-           openshift.withCluster() {

-             openshift.withProject("${params.IMAGESTREAM_NAMESPACE}") {

-               openshift.tag("${params.IMAGESTREAM_NAME}:${env.RESULTING_TAG}",

-                 "-d")

-             }

-           }

-         }

-       }

-     }

      success {

        script {

          // on pre-merge workflow success

@@ -41,12 +41,14 @@ 

          value: {{ c3i_fedmsg_provider }}

        - name: PAGURE_API_KEY_SECRET_NAME

          value: {{ c3i_pagure_api_key_secret }}

-       - name: IMAGESTREAM_NAMESPACE

-         value: {{ c3i_imagestream_namespace }}

-       - name: IMAGESTREAM_NAME

-         value: {{ c3i_imagestream_name }}

        - name: MAIL_ADDRESS

          value: {{ c3i_mail_address }}

+       - name: C3IAAS_NAMESPACE

+         value: {{ c3i_c3iaas_namespace }}

+       - name: C3IAAS_JOB_NAME

+         value: {{ c3i_c3iaas_job_name }}

+       - name: C3IAAS_LIFETIME

+         value: '"{{ c3i_c3iaas_build_lifetime }}"'

  {% for param in c3i_build_custom_parameters %}

        - name: {{ param.name }}

          value: {{ param.value }}

file modified
+2 -1
@@ -226,6 +226,7 @@ 

   * @param args.provider UMB provider name defined in Jenkins.

   * @param args.docs Documentation link

   * @param args.email Contact email

+  * @param args.xunit URL of xunit results

   * @return Result from sendCIMessage.

   */

  def sendResultToMessageBus(Map args) {
@@ -278,7 +279,7 @@ 

        "type": "integration",

        "category": "${environment}",

        "status": "${currentBuild.result == null || currentBuild.result == 'SUCCESS' ? 'passed':'failed'}",

-       "xunit": "",

+       "xunit": "${args.xunit ?: ''}",

        "generated_at": "${new Date().format("yyyy-MM-dd'T'HH:mm:ss'Z'", TimeZone.getTimeZone('UTC'))}",

        "namespace": "c3i",

        "version": "0.1.0"