Skip to main content

Jenkins

在本示例中,您将在Jenkins 和 Port 之间创建一个 webhook 集成,用于接收作业和构建实体。

Port 配置

创建以下蓝图定义:

Jenkins job blueprint
{
"identifier": "jenkinsJob",
"description": "This blueprint represents a job event from Jenkins",
"title": "Jenkins Job",
"icon": "Jenkins",
"schema": {
"properties": {
"jobName": {
"type": "string",
"title": "Project Name"
},
"jobStatus": {
"type": "string",
"title": "Job Status",
"enum": ["created", "updated", "deleted"],
"enumColors": {
"created": "green",
"updated": "yellow",
"deleted": "red"
}
},
"timestamp": {
"type": "string",
"format": "date-time",
"title": "Timestamp",
"description": "Last updated timestamp of the job"
},
"url": {
"type": "string",
"title": "Project URL"
}
},
"required": []
},
"mirrorProperties": {},
"calculationProperties": {
"jobUrl": {
"title": "Job Full URL",
"calculation": "'https://your_jenkins_url/' + .properties.url",
"type": "string",
"format": "url"
}
},
"relations": {}
}
Jenkins build blueprint (including the Jenkins job relation)
{
"identifier": "jenkinsBuild",
"description": "This blueprint represents a build event from Jenkins",
"title": "Jenkins Build",
"icon": "Jenkins",
"schema": {
"properties": {
"buildStatus": {
"type": "string",
"title": "Build Status",
"enum": ["SUCCESS", "FAILURE", "UNSTABLE"],
"enumColors": {
"SUCCESS": "green",
"FAILURE": "red",
"UNSTABLE": "yellow"
}
},
"buildUrl": {
"type": "string",
"title": "Build URL",
"description": "URL to the build"
},
"timestamp": {
"type": "string",
"format": "date-time",
"title": "Timestamp",
"description": "Last updated timestamp of the build"
},
"buildDuration": {
"type": "number",
"title": "Build Duration",
"description": "Duration of the build"
}
},
"required": []
},
"mirrorProperties": {},
"calculationProperties": {},
"relations": {
"jenkinsJob": {
"title": "Jenkins Job",
"target": "jenkinsJob",
"required": false,
"many": false
}
}
}

创建以下 webhook 配置using Port's UI

Jenkins job and build webhook configuration
  1. 基本信息 选项卡 - 填写以下详细信息: 1.title: Jenkins Mapper; 2.标识符 : jenkins_mapper; 3.Description : 将 Jenkins 构建和作业映射到 Port 的 webhook 配置; 4.图标 : Jenkins
  2. 集成配置选项卡 - 填写以下 JQ 映射:
    [
    {
    "blueprint": "jenkinsJob",
    "filter": ".body.type | startswith(\"item\")",
    "entity": {
    "identifier": ".body.url | sub(\"%20\"; \"-\"; \"g\") | sub(\"/\"; \"-\"; \"g\") | .[:-1]",
    "title": ".body.data.displayName",
    "properties": {
    "jobName": ".body.data.fullName",
    "url": ".body.url",
    "jobStatus": ".body.type | split(\".\") | last",
    "timestamp": ".body.time"
    }
    }
    },
    {
    "blueprint": "jenkinsBuild",
    "filter": ".body.type | startswith(\"run\")",
    "entity": {
    "identifier": ".body.data.fullDisplayName | sub(\" \"; \"-\"; \"g\") | sub(\"#\"; \"\"; \"g\")",
    "title": ".body.data.displayName",
    "properties": {
    "buildStatus": ".body.data.result",
    "buildUrl": ".body.url",
    "buildDuration": ".body.data.duration",
    "timestamp": ".body.data.timestamp / 1000 | todate"
    },
    "relations": {
    "jenkinsJob": ".body.source | tostring | sub(\"%20\"; \"-\"; \"g\") | sub(\"/\"; \"-\"; \"g\") | .[:-1]"
    }
    }
    }
    ]

3.点击页面底部的保存

在 Jenkins 中创建 webhook

  1. 进入 Jenkins 面板;
  2. 在页面左侧的侧边栏中选择 管理 Jenkins,然后点击 管理插件
  3. 导航至可用插件选项卡,在搜索栏中搜索通用事件。安装Generic Event 或一个合适的插件,该插件可将 Jenkins 中发生的所有事件通知一些端点;
  4. 返回 Jenkins 面板,点击左侧菜单中的管理 Jenkins
  5. 点击 "配置系统"选项卡,向下滚动到 "事件调度器"部分;
  6. 在文本框中输入创建 webhook 配置后收到的 url 键的值;
  7. 单击页面底部的保存
为了查看 Jenkins webhooks 中可用的不同有效载荷和事件、click here

完成!作业或构建流程的任何更改(排队、开始、完成、Finalizer 等)都会触发 webhook 事件,并将其发送到 Port 提供的 webhook URL。 Port 将根据映射解析事件,并相应地更新目录实体。