From 01770a5f55e6af1ec6fb2e4e104681fc10706c50 Mon Sep 17 00:00:00 2001 From: Matan Heled Date: Wed, 15 Mar 2023 18:48:03 +0200 Subject: [PATCH 01/10] added jenkins template in markdown --- jenkins/readme.md | 97 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 jenkins/readme.md diff --git a/jenkins/readme.md b/jenkins/readme.md new file mode 100644 index 0000000..08a147a --- /dev/null +++ b/jenkins/readme.md @@ -0,0 +1,97 @@ +# Jenkins installation + +Insert template explanation here + +--- + +Follow these steps to report your Jenkins pipeline runs to Port: +1. Install the necessary Jenkins plugins: +- [Plain Credentials](https://plugins.jenkins.io/credentials-binding/) (>=143.v1b_df8b_d3b_e48) +- [HTTP Request](https://plugins.jenkins.io/http_request/) (>=1.16) +- [build user vars plugin](https://plugins.jenkins.io/build-user-vars-plugin/) (>=v1.9) + +2. Make sure the following methods are [script approved](https://www.jenkins.io/doc/book/managing/script-approval/): + +``` +new groovy.json.JsonSlurperClassic +method groovy.json.JsonSlurperClassic parseText java.lang.String +staticMethod java.time.OffsetDateTime now +method java.time.OffsetDateTime format java.time.format.DateTimeFormatter +``` + + +3. Add your `PORT_CLIENT_ID` and `PORT_CLIENT_SECRET` as [Jenkins Credentials](https://www.jenkins.io/doc/book/using/using-credentials/) to use them in your CI pipelines. + +4. Add the following stage to the end of your deployment pipeline: + +```groovy +import java.time.OffsetDateTime +import java.time.format.DateTimeFormatter +import groovy.json.JsonSlurperClassic + stage('Report Deployment to Port') { + steps { + script { + // load credentials as variables + withCredentials([ + string(credentialsId: 'port-client-id', variable: 'PORT_CLIENT_ID'), + string(credentialsId: 'port-client-secret', variable: 'PORT_CLIENT_SECRET') + ]){ + auth_body = """ + { + "clientId": "${PORT_CLIENT_ID}", + "clientSecret": "${PORT_CLIENT_SECRET}" + } + """ + token_response = httpRequest contentType: 'APPLICATION_JSON', + httpMode: "POST", + requestBody: auth_body, + url: "${API_URL}/v1/auth/access_token" + def slurped_response = new JsonSlurperClassic().parseText(token_response.content) + def token = slurped_response.accessToken // Use this token for authentication with Port + + def pipeline_body = """ + { + "identifier": "${env.JOB_NAME}", + "properties": { + "jobUrl": "${env.JENKINS_URL}jobs/${env.JOB_NAME}" + } + } + """ + pipeline_response = httpRequest contentType: "APPLICATION_JSON", httpMode: "POST", + url: "${API_URL}/v1/blueprints/jenkinsPipeline/entities?upsert=true&validation_only=false&merge=true", + requestBody: pipeline_body, + customHeaders: [ + [name: "Authorization", value: "Bearer ${token}"], + ] + + + // uses the 'build user vars` + wrap([$class: 'BuildUser']) { + def user = env.BUILD_USER_ID + } + OffsetDateTime cur_time = OffsetDateTime.now(); + String formatted_time = cur_time.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXX")); + + def deploy_body = """ + { + "identifier": "${env.BUILD_TAG}", + "properties": { + "triggeredBy": "${user}", + "runLink": "${env.BUILD_URL}", + "jobEndTime": "${formatted_time}", + "jobStatus": "Success" + }, + "relations": { + "Pipeline": "${env.JOB_NAME}" + } + } + """ + deployment_response = httpRequest contentType: "APPLICATION_JSON", httpMode: "POST", + url: "${API_URL}/v1/blueprints/jenkinsPipelineRun/entities?upsert=true&validation_only=false&merge=true", + requestBody: deploy_body, + customHeaders: [ + [name: "Authorization", value: "Bearer ${token}"], + ] + } + } +``` \ No newline at end of file From 80655850b36eca7326d9657cf92081b80ed86942 Mon Sep 17 00:00:00 2001 From: Matan Heled Date: Wed, 15 Mar 2023 18:51:22 +0200 Subject: [PATCH 02/10] fix --- jenkins/readme.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/jenkins/readme.md b/jenkins/readme.md index 08a147a..76a445e 100644 --- a/jenkins/readme.md +++ b/jenkins/readme.md @@ -57,6 +57,7 @@ import groovy.json.JsonSlurperClassic } } """ + // update jenkinsPipeline entity pipeline_response = httpRequest contentType: "APPLICATION_JSON", httpMode: "POST", url: "${API_URL}/v1/blueprints/jenkinsPipeline/entities?upsert=true&validation_only=false&merge=true", requestBody: pipeline_body, @@ -72,7 +73,7 @@ import groovy.json.JsonSlurperClassic OffsetDateTime cur_time = OffsetDateTime.now(); String formatted_time = cur_time.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXX")); - def deploy_body = """ + def run_entity_body = """ { "identifier": "${env.BUILD_TAG}", "properties": { @@ -86,9 +87,10 @@ import groovy.json.JsonSlurperClassic } } """ - deployment_response = httpRequest contentType: "APPLICATION_JSON", httpMode: "POST", + // create jenkinsPipelineRun entity + run_entity_response = httpRequest contentType: "APPLICATION_JSON", httpMode: "POST", url: "${API_URL}/v1/blueprints/jenkinsPipelineRun/entities?upsert=true&validation_only=false&merge=true", - requestBody: deploy_body, + requestBody: run_entity_body, customHeaders: [ [name: "Authorization", value: "Bearer ${token}"], ] From 5a391b0cce420531332f1cc2a897994218d0ba89 Mon Sep 17 00:00:00 2001 From: Matan Heled Date: Sun, 26 Mar 2023 15:21:10 +0300 Subject: [PATCH 03/10] Added scm and no scm template --- jenkins/readme_no_scm_pipeline.md | 99 ++++++++++++++++++++++++++++ jenkins/readme_scm_pipeline.md | 104 ++++++++++++++++++++++++++++++ 2 files changed, 203 insertions(+) create mode 100644 jenkins/readme_no_scm_pipeline.md create mode 100644 jenkins/readme_scm_pipeline.md diff --git a/jenkins/readme_no_scm_pipeline.md b/jenkins/readme_no_scm_pipeline.md new file mode 100644 index 0000000..76a445e --- /dev/null +++ b/jenkins/readme_no_scm_pipeline.md @@ -0,0 +1,99 @@ +# Jenkins installation + +Insert template explanation here + +--- + +Follow these steps to report your Jenkins pipeline runs to Port: +1. Install the necessary Jenkins plugins: +- [Plain Credentials](https://plugins.jenkins.io/credentials-binding/) (>=143.v1b_df8b_d3b_e48) +- [HTTP Request](https://plugins.jenkins.io/http_request/) (>=1.16) +- [build user vars plugin](https://plugins.jenkins.io/build-user-vars-plugin/) (>=v1.9) + +2. Make sure the following methods are [script approved](https://www.jenkins.io/doc/book/managing/script-approval/): + +``` +new groovy.json.JsonSlurperClassic +method groovy.json.JsonSlurperClassic parseText java.lang.String +staticMethod java.time.OffsetDateTime now +method java.time.OffsetDateTime format java.time.format.DateTimeFormatter +``` + + +3. Add your `PORT_CLIENT_ID` and `PORT_CLIENT_SECRET` as [Jenkins Credentials](https://www.jenkins.io/doc/book/using/using-credentials/) to use them in your CI pipelines. + +4. Add the following stage to the end of your deployment pipeline: + +```groovy +import java.time.OffsetDateTime +import java.time.format.DateTimeFormatter +import groovy.json.JsonSlurperClassic + stage('Report Deployment to Port') { + steps { + script { + // load credentials as variables + withCredentials([ + string(credentialsId: 'port-client-id', variable: 'PORT_CLIENT_ID'), + string(credentialsId: 'port-client-secret', variable: 'PORT_CLIENT_SECRET') + ]){ + auth_body = """ + { + "clientId": "${PORT_CLIENT_ID}", + "clientSecret": "${PORT_CLIENT_SECRET}" + } + """ + token_response = httpRequest contentType: 'APPLICATION_JSON', + httpMode: "POST", + requestBody: auth_body, + url: "${API_URL}/v1/auth/access_token" + def slurped_response = new JsonSlurperClassic().parseText(token_response.content) + def token = slurped_response.accessToken // Use this token for authentication with Port + + def pipeline_body = """ + { + "identifier": "${env.JOB_NAME}", + "properties": { + "jobUrl": "${env.JENKINS_URL}jobs/${env.JOB_NAME}" + } + } + """ + // update jenkinsPipeline entity + pipeline_response = httpRequest contentType: "APPLICATION_JSON", httpMode: "POST", + url: "${API_URL}/v1/blueprints/jenkinsPipeline/entities?upsert=true&validation_only=false&merge=true", + requestBody: pipeline_body, + customHeaders: [ + [name: "Authorization", value: "Bearer ${token}"], + ] + + + // uses the 'build user vars` + wrap([$class: 'BuildUser']) { + def user = env.BUILD_USER_ID + } + OffsetDateTime cur_time = OffsetDateTime.now(); + String formatted_time = cur_time.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXX")); + + def run_entity_body = """ + { + "identifier": "${env.BUILD_TAG}", + "properties": { + "triggeredBy": "${user}", + "runLink": "${env.BUILD_URL}", + "jobEndTime": "${formatted_time}", + "jobStatus": "Success" + }, + "relations": { + "Pipeline": "${env.JOB_NAME}" + } + } + """ + // create jenkinsPipelineRun entity + run_entity_response = httpRequest contentType: "APPLICATION_JSON", httpMode: "POST", + url: "${API_URL}/v1/blueprints/jenkinsPipelineRun/entities?upsert=true&validation_only=false&merge=true", + requestBody: run_entity_body, + customHeaders: [ + [name: "Authorization", value: "Bearer ${token}"], + ] + } + } +``` \ No newline at end of file diff --git a/jenkins/readme_scm_pipeline.md b/jenkins/readme_scm_pipeline.md new file mode 100644 index 0000000..894ba29 --- /dev/null +++ b/jenkins/readme_scm_pipeline.md @@ -0,0 +1,104 @@ +# Jenkins installation + +Insert template explanation here + +--- + +Follow these steps to report your Jenkins pipeline runs to Port: +1. Install the necessary Jenkins plugins: +- [Plain Credentials](https://plugins.jenkins.io/credentials-binding/) (>=143.v1b_df8b_d3b_e48) +- [HTTP Request](https://plugins.jenkins.io/http_request/) (>=1.16) +- [build user vars plugin](https://plugins.jenkins.io/build-user-vars-plugin/) (>=v1.9) + +2. Make sure the following methods are [script approved](https://www.jenkins.io/doc/book/managing/script-approval/): + +``` +new groovy.json.JsonSlurperClassic +method groovy.json.JsonSlurperClassic parseText java.lang.String +staticMethod java.time.OffsetDateTime now +method java.time.OffsetDateTime format java.time.format.DateTimeFormatter +``` + + +3. Add your `PORT_CLIENT_ID` and `PORT_CLIENT_SECRET` as [Jenkins Credentials](https://www.jenkins.io/doc/book/using/using-credentials/) to use them in your CI pipelines. + +4. Add the following stage to the end of your deployment pipeline: + +```groovy +import java.time.OffsetDateTime +import java.time.format.DateTimeFormatter +import groovy.json.JsonSlurperClassic + stage('Report Deployment to Port') { + steps { + script { + // load credentials as variables + withCredentials([ + string(credentialsId: 'port-client-id', variable: 'PORT_CLIENT_ID'), + string(credentialsId: 'port-client-secret', variable: 'PORT_CLIENT_SECRET') + ]){ + def scmVars = checkout scmGit( + branches: [[name: 'main']], + userRemoteConfigs: [[url: 'https://github.com/port-labs/temaplate-assets.git']]) + echo "${scmVars.GIT_COMMIT}" + auth_body = """ + { + "clientId": "${PORT_CLIENT_ID}", + "clientSecret": "${PORT_CLIENT_SECRET}" + } + """ + token_response = httpRequest contentType: 'APPLICATION_JSON', + httpMode: "POST", + requestBody: auth_body, + url: "${API_URL}/v1/auth/access_token" + def slurped_response = new JsonSlurperClassic().parseText(token_response.content) + def token = slurped_response.accessToken // Use this token for authentication with Port + + def pipeline_body = """ + { + "identifier": "${env.JOB_NAME}", + "properties": { + "jobUrl": "${env.JENKINS_URL}jobs/${env.JOB_NAME}", + "gitUrl": "${GIT_}" + } + } + """ + // update jenkinsPipeline entity + pipeline_response = httpRequest contentType: "APPLICATION_JSON", httpMode: "POST", + url: "${API_URL}/v1/blueprints/jenkinsPipeline/entities?upsert=true&validation_only=false&merge=true", + requestBody: pipeline_body, + customHeaders: [ + [name: "Authorization", value: "Bearer ${token}"], + ] + + + // uses the 'build user vars` + wrap([$class: 'BuildUser']) { + def user = env.BUILD_USER_ID + } + OffsetDateTime cur_time = OffsetDateTime.now(); + String formatted_time = cur_time.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXX")); + + def run_entity_body = """ + { + "identifier": "${env.BUILD_TAG}", + "properties": { + "triggeredBy": "${user}", + "runLink": "${env.BUILD_URL}", + "jobEndTime": "${formatted_time}", + "jobStatus": "Success" + }, + "relations": { + "Pipeline": "${env.JOB_NAME}" + } + } + """ + // create jenkinsPipelineRun entity + run_entity_response = httpRequest contentType: "APPLICATION_JSON", httpMode: "POST", + url: "${API_URL}/v1/blueprints/jenkinsPipelineRun/entities?upsert=true&validation_only=false&merge=true", + requestBody: run_entity_body, + customHeaders: [ + [name: "Authorization", value: "Bearer ${token}"], + ] + } + } +``` \ No newline at end of file From ffb6f537b401000f52130b2085886477c2b1d28a Mon Sep 17 00:00:00 2001 From: Matan Heled Date: Sun, 26 Mar 2023 15:22:01 +0300 Subject: [PATCH 04/10] fix --- jenkins/readme_scm_pipeline.md | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/jenkins/readme_scm_pipeline.md b/jenkins/readme_scm_pipeline.md index 894ba29..76dc5b2 100644 --- a/jenkins/readme_scm_pipeline.md +++ b/jenkins/readme_scm_pipeline.md @@ -28,7 +28,7 @@ method java.time.OffsetDateTime format java.time.format.DateTimeFormatter import java.time.OffsetDateTime import java.time.format.DateTimeFormatter import groovy.json.JsonSlurperClassic - stage('Report Deployment to Port') { + stage('Report Deployment to Port') { steps { script { // load credentials as variables @@ -37,8 +37,8 @@ import groovy.json.JsonSlurperClassic string(credentialsId: 'port-client-secret', variable: 'PORT_CLIENT_SECRET') ]){ def scmVars = checkout scmGit( - branches: [[name: 'main']], - userRemoteConfigs: [[url: 'https://github.com/port-labs/temaplate-assets.git']]) + branches: [[name: 'origin/main']], + userRemoteConfigs: [[url: "${params.GIT_REPO_URL}"]]) echo "${scmVars.GIT_COMMIT}" auth_body = """ { @@ -53,12 +53,27 @@ import groovy.json.JsonSlurperClassic def slurped_response = new JsonSlurperClassic().parseText(token_response.content) def token = slurped_response.accessToken // Use this token for authentication with Port + def repo_body = """ + { + "identifier": "${env.JOB_NAME}-repo", + "properties": { + "gitUrl": "${params.GIT_REPO_URL}" + } + } + """ + // update jenkinsPipeline entity + pipeline_response = httpRequest contentType: "APPLICATION_JSON", httpMode: "POST", + url: "${API_URL}/v1/blueprints/gitRepo/entities?upsert=true&validation_only=false&merge=true", + requestBody: repo_body, + customHeaders: [ + [name: "Authorization", value: "Bearer ${token}"], + ] + def pipeline_body = """ { "identifier": "${env.JOB_NAME}", "properties": { - "jobUrl": "${env.JENKINS_URL}jobs/${env.JOB_NAME}", - "gitUrl": "${GIT_}" + "jobUrl": "${env.JENKINS_URL}jobs/${env.JOB_NAME}" } } """ @@ -101,4 +116,6 @@ import groovy.json.JsonSlurperClassic ] } } + } +} ``` \ No newline at end of file From 88f84eee143ebc03aaf9441f36dc3c3f6bbea9de Mon Sep 17 00:00:00 2001 From: Matan Heled Date: Sun, 26 Mar 2023 15:23:01 +0300 Subject: [PATCH 05/10] removed old readme --- jenkins/readme.md | 99 ----------------------------------------------- 1 file changed, 99 deletions(-) delete mode 100644 jenkins/readme.md diff --git a/jenkins/readme.md b/jenkins/readme.md deleted file mode 100644 index 76a445e..0000000 --- a/jenkins/readme.md +++ /dev/null @@ -1,99 +0,0 @@ -# Jenkins installation - -Insert template explanation here - ---- - -Follow these steps to report your Jenkins pipeline runs to Port: -1. Install the necessary Jenkins plugins: -- [Plain Credentials](https://plugins.jenkins.io/credentials-binding/) (>=143.v1b_df8b_d3b_e48) -- [HTTP Request](https://plugins.jenkins.io/http_request/) (>=1.16) -- [build user vars plugin](https://plugins.jenkins.io/build-user-vars-plugin/) (>=v1.9) - -2. Make sure the following methods are [script approved](https://www.jenkins.io/doc/book/managing/script-approval/): - -``` -new groovy.json.JsonSlurperClassic -method groovy.json.JsonSlurperClassic parseText java.lang.String -staticMethod java.time.OffsetDateTime now -method java.time.OffsetDateTime format java.time.format.DateTimeFormatter -``` - - -3. Add your `PORT_CLIENT_ID` and `PORT_CLIENT_SECRET` as [Jenkins Credentials](https://www.jenkins.io/doc/book/using/using-credentials/) to use them in your CI pipelines. - -4. Add the following stage to the end of your deployment pipeline: - -```groovy -import java.time.OffsetDateTime -import java.time.format.DateTimeFormatter -import groovy.json.JsonSlurperClassic - stage('Report Deployment to Port') { - steps { - script { - // load credentials as variables - withCredentials([ - string(credentialsId: 'port-client-id', variable: 'PORT_CLIENT_ID'), - string(credentialsId: 'port-client-secret', variable: 'PORT_CLIENT_SECRET') - ]){ - auth_body = """ - { - "clientId": "${PORT_CLIENT_ID}", - "clientSecret": "${PORT_CLIENT_SECRET}" - } - """ - token_response = httpRequest contentType: 'APPLICATION_JSON', - httpMode: "POST", - requestBody: auth_body, - url: "${API_URL}/v1/auth/access_token" - def slurped_response = new JsonSlurperClassic().parseText(token_response.content) - def token = slurped_response.accessToken // Use this token for authentication with Port - - def pipeline_body = """ - { - "identifier": "${env.JOB_NAME}", - "properties": { - "jobUrl": "${env.JENKINS_URL}jobs/${env.JOB_NAME}" - } - } - """ - // update jenkinsPipeline entity - pipeline_response = httpRequest contentType: "APPLICATION_JSON", httpMode: "POST", - url: "${API_URL}/v1/blueprints/jenkinsPipeline/entities?upsert=true&validation_only=false&merge=true", - requestBody: pipeline_body, - customHeaders: [ - [name: "Authorization", value: "Bearer ${token}"], - ] - - - // uses the 'build user vars` - wrap([$class: 'BuildUser']) { - def user = env.BUILD_USER_ID - } - OffsetDateTime cur_time = OffsetDateTime.now(); - String formatted_time = cur_time.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXX")); - - def run_entity_body = """ - { - "identifier": "${env.BUILD_TAG}", - "properties": { - "triggeredBy": "${user}", - "runLink": "${env.BUILD_URL}", - "jobEndTime": "${formatted_time}", - "jobStatus": "Success" - }, - "relations": { - "Pipeline": "${env.JOB_NAME}" - } - } - """ - // create jenkinsPipelineRun entity - run_entity_response = httpRequest contentType: "APPLICATION_JSON", httpMode: "POST", - url: "${API_URL}/v1/blueprints/jenkinsPipelineRun/entities?upsert=true&validation_only=false&merge=true", - requestBody: run_entity_body, - customHeaders: [ - [name: "Authorization", value: "Bearer ${token}"], - ] - } - } -``` \ No newline at end of file From 9bc6d7751fda20ad96b49559ac79fe5a2fda41d4 Mon Sep 17 00:00:00 2001 From: Matan Heled Date: Sun, 26 Mar 2023 15:42:51 +0300 Subject: [PATCH 06/10] fix --- jenkins/readme_scm_pipeline.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/jenkins/readme_scm_pipeline.md b/jenkins/readme_scm_pipeline.md index 76dc5b2..6f68e64 100644 --- a/jenkins/readme_scm_pipeline.md +++ b/jenkins/readme_scm_pipeline.md @@ -9,7 +9,7 @@ Follow these steps to report your Jenkins pipeline runs to Port: - [Plain Credentials](https://plugins.jenkins.io/credentials-binding/) (>=143.v1b_df8b_d3b_e48) - [HTTP Request](https://plugins.jenkins.io/http_request/) (>=1.16) - [build user vars plugin](https://plugins.jenkins.io/build-user-vars-plugin/) (>=v1.9) - +- [Git Client](https://plugins.jenkins.io/git-client/) (>=4.2.0) 2. Make sure the following methods are [script approved](https://www.jenkins.io/doc/book/managing/script-approval/): ``` @@ -36,6 +36,8 @@ import groovy.json.JsonSlurperClassic string(credentialsId: 'port-client-id', variable: 'PORT_CLIENT_ID'), string(credentialsId: 'port-client-secret', variable: 'PORT_CLIENT_SECRET') ]){ + + // checkout git repo def scmVars = checkout scmGit( branches: [[name: 'origin/main']], userRemoteConfigs: [[url: "${params.GIT_REPO_URL}"]]) From 45a0b6ab88c84cde8bd336d295ae7fc722fc1e36 Mon Sep 17 00:00:00 2001 From: Matan Heled Date: Sun, 26 Mar 2023 15:44:21 +0300 Subject: [PATCH 07/10] fix --- jenkins/readme_scm_pipeline.md | 170 +++++++++++++++++---------------- 1 file changed, 88 insertions(+), 82 deletions(-) diff --git a/jenkins/readme_scm_pipeline.md b/jenkins/readme_scm_pipeline.md index 6f68e64..2ac9038 100644 --- a/jenkins/readme_scm_pipeline.md +++ b/jenkins/readme_scm_pipeline.md @@ -28,96 +28,102 @@ method java.time.OffsetDateTime format java.time.format.DateTimeFormatter import java.time.OffsetDateTime import java.time.format.DateTimeFormatter import groovy.json.JsonSlurperClassic - stage('Report Deployment to Port') { - steps { - script { - // load credentials as variables - withCredentials([ - string(credentialsId: 'port-client-id', variable: 'PORT_CLIENT_ID'), - string(credentialsId: 'port-client-secret', variable: 'PORT_CLIENT_SECRET') - ]){ - - // checkout git repo - def scmVars = checkout scmGit( - branches: [[name: 'origin/main']], - userRemoteConfigs: [[url: "${params.GIT_REPO_URL}"]]) - echo "${scmVars.GIT_COMMIT}" - auth_body = """ + parameters { + string(name: 'GIT_REPO_URL', defaultValue: 'https://github.com/my-org/my-repo.git', description: 'The URL of the Git repository to check out') + } + stage('Report Deployment to Port') { + steps { + script { + // load credentials as variables + withCredentials([ + string(credentialsId: 'port-client-id', variable: 'PORT_CLIENT_ID'), + string(credentialsId: 'port-client-secret', variable: 'PORT_CLIENT_SECRET') + ]){ + + // checkout git repo + def scmVars = checkout scmGit( + branches: [[name: 'origin/main']], + userRemoteConfigs: [[url: "${params.GIT_REPO_URL}"]]) + echo "${scmVars.GIT_COMMIT}" + auth_body = """ + { + "clientId": "${PORT_CLIENT_ID}", + "clientSecret": "${PORT_CLIENT_SECRET}" + } + """ + token_response = httpRequest contentType: 'APPLICATION_JSON', + httpMode: "POST", + requestBody: auth_body, + url: "${API_URL}/v1/auth/access_token" + def slurped_response = new JsonSlurperClassic().parseText(token_response.content) + def token = slurped_response.accessToken // Use this token for authentication with Port + + def repo_body = """ { - "clientId": "${PORT_CLIENT_ID}", - "clientSecret": "${PORT_CLIENT_SECRET}" + "identifier": "${env.JOB_NAME}-repo", + "properties": { + "gitUrl": "${params.GIT_REPO_URL}" + } } """ - token_response = httpRequest contentType: 'APPLICATION_JSON', - httpMode: "POST", - requestBody: auth_body, - url: "${API_URL}/v1/auth/access_token" - def slurped_response = new JsonSlurperClassic().parseText(token_response.content) - def token = slurped_response.accessToken // Use this token for authentication with Port + // update jenkinsPipeline entity + repo_response = httpRequest contentType: "APPLICATION_JSON", httpMode: "POST", + url: "${API_URL}/v1/blueprints/gitRepo/entities?upsert=true&validation_only=false&merge=true", + requestBody: repo_body, + customHeaders: [ + [name: "Authorization", value: "Bearer ${token}"], + ] - def repo_body = """ - { - "identifier": "${env.JOB_NAME}-repo", - "properties": { - "gitUrl": "${params.GIT_REPO_URL}" - } - } - """ - // update jenkinsPipeline entity - pipeline_response = httpRequest contentType: "APPLICATION_JSON", httpMode: "POST", - url: "${API_URL}/v1/blueprints/gitRepo/entities?upsert=true&validation_only=false&merge=true", - requestBody: repo_body, - customHeaders: [ - [name: "Authorization", value: "Bearer ${token}"], - ] - - def pipeline_body = """ - { - "identifier": "${env.JOB_NAME}", - "properties": { - "jobUrl": "${env.JENKINS_URL}jobs/${env.JOB_NAME}" - } + def pipeline_body = """ + { + "identifier": "${env.JOB_NAME}", + "properties": { + "jobUrl": "${env.JENKINS_URL}jobs/${env.JOB_NAME}" } - """ - // update jenkinsPipeline entity - pipeline_response = httpRequest contentType: "APPLICATION_JSON", httpMode: "POST", - url: "${API_URL}/v1/blueprints/jenkinsPipeline/entities?upsert=true&validation_only=false&merge=true", - requestBody: pipeline_body, - customHeaders: [ - [name: "Authorization", value: "Bearer ${token}"], - ] + } + """ + // update jenkinsPipeline entity + pipeline_response = httpRequest contentType: "APPLICATION_JSON", httpMode: "POST", + url: "${API_URL}/v1/blueprints/jenkinsPipeline/entities?upsert=true&validation_only=false&merge=true", + requestBody: pipeline_body, + customHeaders: [ + [name: "Authorization", value: "Bearer ${token}"], + ] - // uses the 'build user vars` - wrap([$class: 'BuildUser']) { - def user = env.BUILD_USER_ID - } - OffsetDateTime cur_time = OffsetDateTime.now(); - String formatted_time = cur_time.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXX")); + // uses the 'build user vars` + wrap([$class: 'BuildUser']) { + def user = env.BUILD_USER_ID + } + OffsetDateTime cur_time = OffsetDateTime.now(); + String formatted_time = cur_time.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXX")); - def run_entity_body = """ - { - "identifier": "${env.BUILD_TAG}", - "properties": { - "triggeredBy": "${user}", - "runLink": "${env.BUILD_URL}", - "jobEndTime": "${formatted_time}", - "jobStatus": "Success" - }, - "relations": { - "Pipeline": "${env.JOB_NAME}" - } + def run_entity_body = """ + { + "identifier": "${env.BUILD_TAG}", + "properties": { + "triggeredBy": "${user}", + "runLink": "${env.BUILD_URL}", + "jobEndTime": "${formatted_time}", + "jobStatus": "Success" + }, + "relations": { + "Pipeline": "${env.JOB_NAME}" } - """ - // create jenkinsPipelineRun entity - run_entity_response = httpRequest contentType: "APPLICATION_JSON", httpMode: "POST", - url: "${API_URL}/v1/blueprints/jenkinsPipelineRun/entities?upsert=true&validation_only=false&merge=true", - requestBody: run_entity_body, - customHeaders: [ - [name: "Authorization", value: "Bearer ${token}"], - ] - } - } + } + """ + // create jenkinsPipelineRun entity + run_entity_response = httpRequest contentType: "APPLICATION_JSON", httpMode: "POST", + url: "${API_URL}/v1/blueprints/jenkinsPipelineRun/entities?upsert=true&validation_only=false&merge=true", + requestBody: run_entity_body, + customHeaders: [ + [name: "Authorization", value: "Bearer ${token}"], + ] + } } } -``` \ No newline at end of file +} +``` + +:::note +For this pipeline to work, you need to pass the GITHUB_UR \ No newline at end of file From aa8c00f92e2db358d44741f077d1eb58b331e6d1 Mon Sep 17 00:00:00 2001 From: Matan Heled Date: Sun, 26 Mar 2023 15:47:01 +0300 Subject: [PATCH 08/10] fix --- jenkins/readme_scm_pipeline.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/jenkins/readme_scm_pipeline.md b/jenkins/readme_scm_pipeline.md index 2ac9038..351ab59 100644 --- a/jenkins/readme_scm_pipeline.md +++ b/jenkins/readme_scm_pipeline.md @@ -123,7 +123,4 @@ import groovy.json.JsonSlurperClassic } } } -``` - -:::note -For this pipeline to work, you need to pass the GITHUB_UR \ No newline at end of file +``` \ No newline at end of file From ed422690a3a5896ee444d931f995046c0215ba6b Mon Sep 17 00:00:00 2001 From: Matan Heled Date: Mon, 27 Mar 2023 14:12:49 +0300 Subject: [PATCH 09/10] changed to readme --- jenkins/gitRepo.json | 19 +++ jenkins/jenkinspipeline.json | 24 ++++ jenkins/jenkinspipelinerun.json | 49 +++++++ .../{readme_no_scm_pipeline.md => readme.md} | 0 jenkins/readme_scm_pipeline.md | 126 ------------------ 5 files changed, 92 insertions(+), 126 deletions(-) create mode 100644 jenkins/gitRepo.json create mode 100644 jenkins/jenkinspipeline.json create mode 100644 jenkins/jenkinspipelinerun.json rename jenkins/{readme_no_scm_pipeline.md => readme.md} (100%) delete mode 100644 jenkins/readme_scm_pipeline.md diff --git a/jenkins/gitRepo.json b/jenkins/gitRepo.json new file mode 100644 index 0000000..df7f71e --- /dev/null +++ b/jenkins/gitRepo.json @@ -0,0 +1,19 @@ +{ + "identifier": "gitRepo", + "description": "This blueprint represents a git repo for storing Jenkins Pipeline in our software catalog", + "title": "Git Repo", + "icon": "Git", + "schema": { + "properties": { + "gitUrl": { + "type": "string", + "format": "url", + "description": "Jenkins pipeline job url" + } + }, + "required": [] + }, + "mirrorProperties": {}, + "calculationProperties": {}, + "relations": {} +} \ No newline at end of file diff --git a/jenkins/jenkinspipeline.json b/jenkins/jenkinspipeline.json new file mode 100644 index 0000000..d4146bf --- /dev/null +++ b/jenkins/jenkinspipeline.json @@ -0,0 +1,24 @@ +{ + "identifier": "jenkinsPipeline", + "description": "This blueprint represents a Jenkins Pipeline in our software catalog", + "title": "Jenkins Pipeline", + "icon": "Jenkins", + "schema": { + "properties": { + "jobUrl": { + "type": "string", + "format": "url", + "description": "Jenkins pipeline job url" + } + }, + "required": [] + }, + "mirrorProperties": {}, + "calculationProperties": {}, + "relations": { + "gitRepo": { + "target": "gitRepo", + "title": "Pipeline Git Repo" + } + } + } \ No newline at end of file diff --git a/jenkins/jenkinspipelinerun.json b/jenkins/jenkinspipelinerun.json new file mode 100644 index 0000000..de7241c --- /dev/null +++ b/jenkins/jenkinspipelinerun.json @@ -0,0 +1,49 @@ +{ + "identifier": "jenkinsPipelineRun", + "description": "This blueprint represents a service in our software catalog", + "title": "Pipeline Run", + "icon": "CICD", + "schema": { + "properties": { + "triggeredBy": { + "type": "string", + "description": "Who triggered the run" + }, + "runLink": { + "type": "string", + "title": "Run Link", + "icon": "CICD", + "format": "url", + "description": "Link for the CI run" + }, + "jobEndTime": { + "type": "string", + "format": "date-time", + "description": "Time for when run end status was set" + }, + "jobStatus": { + "type": "string", + "enum": [ + "Success", + "Failure" + ], + "enumColors": { + "Success": "green", + "Failure": "red" + } + } + }, + "required": [] + }, + "mirrorProperties": {}, + "calculationProperties": {}, + "relations": { + "Pipeline": { + "title": "Pipeline", + "description": "The pipeline of this run", + "target": "jenkinsPipeline", + "required": false, + "many": false + } + } + } \ No newline at end of file diff --git a/jenkins/readme_no_scm_pipeline.md b/jenkins/readme.md similarity index 100% rename from jenkins/readme_no_scm_pipeline.md rename to jenkins/readme.md diff --git a/jenkins/readme_scm_pipeline.md b/jenkins/readme_scm_pipeline.md deleted file mode 100644 index 351ab59..0000000 --- a/jenkins/readme_scm_pipeline.md +++ /dev/null @@ -1,126 +0,0 @@ -# Jenkins installation - -Insert template explanation here - ---- - -Follow these steps to report your Jenkins pipeline runs to Port: -1. Install the necessary Jenkins plugins: -- [Plain Credentials](https://plugins.jenkins.io/credentials-binding/) (>=143.v1b_df8b_d3b_e48) -- [HTTP Request](https://plugins.jenkins.io/http_request/) (>=1.16) -- [build user vars plugin](https://plugins.jenkins.io/build-user-vars-plugin/) (>=v1.9) -- [Git Client](https://plugins.jenkins.io/git-client/) (>=4.2.0) -2. Make sure the following methods are [script approved](https://www.jenkins.io/doc/book/managing/script-approval/): - -``` -new groovy.json.JsonSlurperClassic -method groovy.json.JsonSlurperClassic parseText java.lang.String -staticMethod java.time.OffsetDateTime now -method java.time.OffsetDateTime format java.time.format.DateTimeFormatter -``` - - -3. Add your `PORT_CLIENT_ID` and `PORT_CLIENT_SECRET` as [Jenkins Credentials](https://www.jenkins.io/doc/book/using/using-credentials/) to use them in your CI pipelines. - -4. Add the following stage to the end of your deployment pipeline: - -```groovy -import java.time.OffsetDateTime -import java.time.format.DateTimeFormatter -import groovy.json.JsonSlurperClassic - parameters { - string(name: 'GIT_REPO_URL', defaultValue: 'https://github.com/my-org/my-repo.git', description: 'The URL of the Git repository to check out') - } - stage('Report Deployment to Port') { - steps { - script { - // load credentials as variables - withCredentials([ - string(credentialsId: 'port-client-id', variable: 'PORT_CLIENT_ID'), - string(credentialsId: 'port-client-secret', variable: 'PORT_CLIENT_SECRET') - ]){ - - // checkout git repo - def scmVars = checkout scmGit( - branches: [[name: 'origin/main']], - userRemoteConfigs: [[url: "${params.GIT_REPO_URL}"]]) - echo "${scmVars.GIT_COMMIT}" - auth_body = """ - { - "clientId": "${PORT_CLIENT_ID}", - "clientSecret": "${PORT_CLIENT_SECRET}" - } - """ - token_response = httpRequest contentType: 'APPLICATION_JSON', - httpMode: "POST", - requestBody: auth_body, - url: "${API_URL}/v1/auth/access_token" - def slurped_response = new JsonSlurperClassic().parseText(token_response.content) - def token = slurped_response.accessToken // Use this token for authentication with Port - - def repo_body = """ - { - "identifier": "${env.JOB_NAME}-repo", - "properties": { - "gitUrl": "${params.GIT_REPO_URL}" - } - } - """ - // update jenkinsPipeline entity - repo_response = httpRequest contentType: "APPLICATION_JSON", httpMode: "POST", - url: "${API_URL}/v1/blueprints/gitRepo/entities?upsert=true&validation_only=false&merge=true", - requestBody: repo_body, - customHeaders: [ - [name: "Authorization", value: "Bearer ${token}"], - ] - - def pipeline_body = """ - { - "identifier": "${env.JOB_NAME}", - "properties": { - "jobUrl": "${env.JENKINS_URL}jobs/${env.JOB_NAME}" - } - } - """ - // update jenkinsPipeline entity - pipeline_response = httpRequest contentType: "APPLICATION_JSON", httpMode: "POST", - url: "${API_URL}/v1/blueprints/jenkinsPipeline/entities?upsert=true&validation_only=false&merge=true", - requestBody: pipeline_body, - customHeaders: [ - [name: "Authorization", value: "Bearer ${token}"], - ] - - - // uses the 'build user vars` - wrap([$class: 'BuildUser']) { - def user = env.BUILD_USER_ID - } - OffsetDateTime cur_time = OffsetDateTime.now(); - String formatted_time = cur_time.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXX")); - - def run_entity_body = """ - { - "identifier": "${env.BUILD_TAG}", - "properties": { - "triggeredBy": "${user}", - "runLink": "${env.BUILD_URL}", - "jobEndTime": "${formatted_time}", - "jobStatus": "Success" - }, - "relations": { - "Pipeline": "${env.JOB_NAME}" - } - } - """ - // create jenkinsPipelineRun entity - run_entity_response = httpRequest contentType: "APPLICATION_JSON", httpMode: "POST", - url: "${API_URL}/v1/blueprints/jenkinsPipelineRun/entities?upsert=true&validation_only=false&merge=true", - requestBody: run_entity_body, - customHeaders: [ - [name: "Authorization", value: "Bearer ${token}"], - ] - } - } -} -} -``` \ No newline at end of file From 52511f47f0e50700e8d58dd1994016984a4d0988 Mon Sep 17 00:00:00 2001 From: Matan Heled Date: Mon, 27 Mar 2023 14:14:35 +0300 Subject: [PATCH 10/10] fix --- jenkins/gitRepo.json | 19 ------------- jenkins/jenkinspipeline.json | 24 ---------------- jenkins/jenkinspipelinerun.json | 49 --------------------------------- 3 files changed, 92 deletions(-) delete mode 100644 jenkins/gitRepo.json delete mode 100644 jenkins/jenkinspipeline.json delete mode 100644 jenkins/jenkinspipelinerun.json diff --git a/jenkins/gitRepo.json b/jenkins/gitRepo.json deleted file mode 100644 index df7f71e..0000000 --- a/jenkins/gitRepo.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "identifier": "gitRepo", - "description": "This blueprint represents a git repo for storing Jenkins Pipeline in our software catalog", - "title": "Git Repo", - "icon": "Git", - "schema": { - "properties": { - "gitUrl": { - "type": "string", - "format": "url", - "description": "Jenkins pipeline job url" - } - }, - "required": [] - }, - "mirrorProperties": {}, - "calculationProperties": {}, - "relations": {} -} \ No newline at end of file diff --git a/jenkins/jenkinspipeline.json b/jenkins/jenkinspipeline.json deleted file mode 100644 index d4146bf..0000000 --- a/jenkins/jenkinspipeline.json +++ /dev/null @@ -1,24 +0,0 @@ -{ - "identifier": "jenkinsPipeline", - "description": "This blueprint represents a Jenkins Pipeline in our software catalog", - "title": "Jenkins Pipeline", - "icon": "Jenkins", - "schema": { - "properties": { - "jobUrl": { - "type": "string", - "format": "url", - "description": "Jenkins pipeline job url" - } - }, - "required": [] - }, - "mirrorProperties": {}, - "calculationProperties": {}, - "relations": { - "gitRepo": { - "target": "gitRepo", - "title": "Pipeline Git Repo" - } - } - } \ No newline at end of file diff --git a/jenkins/jenkinspipelinerun.json b/jenkins/jenkinspipelinerun.json deleted file mode 100644 index de7241c..0000000 --- a/jenkins/jenkinspipelinerun.json +++ /dev/null @@ -1,49 +0,0 @@ -{ - "identifier": "jenkinsPipelineRun", - "description": "This blueprint represents a service in our software catalog", - "title": "Pipeline Run", - "icon": "CICD", - "schema": { - "properties": { - "triggeredBy": { - "type": "string", - "description": "Who triggered the run" - }, - "runLink": { - "type": "string", - "title": "Run Link", - "icon": "CICD", - "format": "url", - "description": "Link for the CI run" - }, - "jobEndTime": { - "type": "string", - "format": "date-time", - "description": "Time for when run end status was set" - }, - "jobStatus": { - "type": "string", - "enum": [ - "Success", - "Failure" - ], - "enumColors": { - "Success": "green", - "Failure": "red" - } - } - }, - "required": [] - }, - "mirrorProperties": {}, - "calculationProperties": {}, - "relations": { - "Pipeline": { - "title": "Pipeline", - "description": "The pipeline of this run", - "target": "jenkinsPipeline", - "required": false, - "many": false - } - } - } \ No newline at end of file