jenkins配置gitea-webhook触发构建任务

前言

  • Jenkins 是一款广泛使用的开源持续集成(CI)和持续交付(CD)工具。
  • Gitea 是一款开源的轻量级 Git 代码托管平台,以其简单易用、高效和轻量级的特点受到众多开发者和团队的青睐。

背景

我的Jenkins通过apt安装,访问地址为http://192.168.31.8:8080
Gitea通过docker安装,宿主机与Jenkins在同一台机器上,访问地址为http://192.168.31.8:3000/, 其docker实例主机IP为:172.19.0.3

需求

此前使用jenkins来创建构建任务时, 触发器一直使用的是轮询Poll SCM, 这比较耗费资源, 想着是否有回调的方式来触发构建。

通过AI搜索,发现可以通过Generic Webhook Trigger插件来生成webhook地址, 然后在gitea上面配置webhook即可。

安装插件

http://192.168.31.8:8080/manage/pluginManager/advanced界面,上传已经下载好的generic-webhook-trigger.hpi

安装完成后就可以在构建实例配置中使用webhook触发器了:

当我们勾选这个触发器后,就能看到它对应的回调地址是http://JENKINS_URL/generic-webhook-trigger/invoke,此时我们可以在gitea设置中配置webhook

由于回调地址都是一样的, 对于具体的构建项目而言, 可能需要更多信息来识别目标仓库,甚至是目标分支,只有在满足构建条件的情况下, 我们再触发构建,如何识别呢??

实际上插件本身提供了变量提取和过滤功能, 比如我可以根据仓库名称和分支名称来确认是否触发本次构建, 所以可以添加REPO_FULL_NAMEREF变量,对应的值从请求的body体中进行提取,对应的JSONPATH表达式分别为$.repository.full_name$.ref

完整body体:

{
  "ref": "refs/heads/main",
  "before": "96be1d61fd823492a00d8f4bb7c1f09666f6307c",
  "after": "9079955191e92c5f749d86c4a56ba8e7510cc7fc",
  "compare_url": "http://192.168.31.8:3000/yanwei/happy-site/compare/96be1d61fd823492a00d8f4bb7c1f09666f6307c...9079955191e92c5f749d86c4a56ba8e7510cc7fc",
  "commits": [
    {
      "id": "9079955191e92c5f749d86c4a56ba8e7510cc7fc",
      "message": "更新 README.MD\n",
      "url": "http://192.168.31.8:3000/yanwei/happy-site/commit/9079955191e92c5f749d86c4a56ba8e7510cc7fc",
      "author": {
        "name": "yanwei",
        "email": "yanwei88@aliyun.com",
        "username": "yanwei"
      },
      "committer": {
        "name": "yanwei",
        "email": "yanwei88@aliyun.com",
        "username": "yanwei"
      },
      "verification": null,
      "timestamp": "2025-02-21T16:55:30Z",
      "added": [],
      "removed": [],
      "modified": [
        "README.MD"
      ]
    }
  ],
  "total_commits": 1,
  "head_commit": {
    "id": "9079955191e92c5f749d86c4a56ba8e7510cc7fc",
    "message": "更新 README.MD\n",
    "url": "http://192.168.31.8:3000/yanwei/happy-site/commit/9079955191e92c5f749d86c4a56ba8e7510cc7fc",
    "author": {
      "name": "yanwei",
      "email": "yanwei88@aliyun.com",
      "username": "yanwei"
    },
    "committer": {
      "name": "yanwei",
      "email": "yanwei88@aliyun.com",
      "username": "yanwei"
    },
    "verification": null,
    "timestamp": "2025-02-21T16:55:30Z",
    "added": [],
    "removed": [],
    "modified": [
      "README.MD"
    ]
  },
  "repository": {
    "id": 1,
    "owner": {
      "id": 1,
      "login": "yanwei",
      "login_name": "",
      "source_id": 0,
      "full_name": "",
      "email": "yanwei88@aliyun.com",
      "avatar_url": "http://192.168.31.8:3000/avatars/cb23cdd8377fb61c193212b961e7f33b",
      "html_url": "http://192.168.31.8:3000/yanwei",
      "language": "",
      "is_admin": false,
      "last_login": "0001-01-01T00:00:00Z",
      "created": "2025-02-10T13:46:45Z",
      "restricted": false,
      "active": false,
      "prohibit_login": false,
      "location": "",
      "website": "",
      "description": "",
      "visibility": "public",
      "followers_count": 0,
      "following_count": 0,
      "starred_repos_count": 0,
      "username": "yanwei"
    },
    "name": "happy-site",
    "full_name": "yanwei/happy-site",
    "description": "",
    "empty": false,
    "private": false,
    "fork": false,
    "template": false,
    "parent": null,
    "mirror": false,
    "size": 360,
    "language": "",
    "languages_url": "http://192.168.31.8:3000/api/v1/repos/yanwei/happy-site/languages",
    "html_url": "http://192.168.31.8:3000/yanwei/happy-site",
    "url": "http://192.168.31.8:3000/api/v1/repos/yanwei/happy-site",
    "link": "",
    "ssh_url": "git@192.168.31.8:yanwei/happy-site.git",
    "clone_url": "http://192.168.31.8:3000/yanwei/happy-site.git",
    "original_url": "",
    "website": "",
    "stars_count": 0,
    "forks_count": 0,
    "watchers_count": 1,
    "open_issues_count": 0,
    "open_pr_counter": 0,
    "release_counter": 0,
    "default_branch": "main",
    "archived": false,
    "created_at": "2025-02-10T13:47:24Z",
    "updated_at": "2025-02-21T15:43:02Z",
    "archived_at": "1970-01-01T00:00:00Z",
    "permissions": {
      "admin": true,
      "push": true,
      "pull": true
    },
    "has_issues": true,
    "internal_tracker": {
      "enable_time_tracker": true,
      "allow_only_contributors_to_track_time": true,
      "enable_issue_dependencies": true
    },
    "has_wiki": true,
    "has_pull_requests": true,
    "has_projects": true,
    "projects_mode": "all",
    "has_releases": true,
    "has_packages": true,
    "has_actions": true,
    "ignore_whitespace_conflicts": false,
    "allow_merge_commits": true,
    "allow_rebase": true,
    "allow_rebase_explicit": true,
    "allow_squash_merge": true,
    "allow_fast_forward_only_merge": true,
    "allow_rebase_update": true,
    "default_delete_branch_after_merge": false,
    "default_merge_style": "merge",
    "default_allow_maintainer_edit": false,
    "avatar_url": "",
    "internal": false,
    "mirror_interval": "",
    "object_format_name": "sha1",
    "mirror_updated": "0001-01-01T00:00:00Z",
    "repo_transfer": null,
    "topics": null,
    "licenses": null
  },
  "pusher": {
    "id": 1,
    "login": "yanwei",
    "login_name": "",
    "source_id": 0,
    "full_name": "",
    "email": "yanwei@noreply.localhost",
    "avatar_url": "http://192.168.31.8:3000/avatars/cb23cdd8377fb61c193212b961e7f33b",
    "html_url": "http://192.168.31.8:3000/yanwei",
    "language": "",
    "is_admin": false,
    "last_login": "0001-01-01T00:00:00Z",
     "created": "2025-02-10T13:46:45Z",
    "restricted": false,
    "active": false,
    "prohibit_login": false,
    "location": "",
    "website": "",
    "description": "",
    "visibility": "public",
    "followers_count": 0,
    "following_count": 0,
    "starred_repos_count": 0,
    "username": "yanwei"
  },
  "sender": {
    "id": 1,
    "login": "yanwei",
    "login_name": "",
    "source_id": 0,
    "full_name": "",
    "email": "yanwei@noreply.localhost",
    "avatar_url": "http://192.168.31.8:3000/avatars/cb23cdd8377fb61c193212b961e7f33b",
    "html_url": "http://192.168.31.8:3000/yanwei",
    "language": "",
    "is_admin": false,
    "last_login": "0001-01-01T00:00:00Z",
    "created": "2025-02-10T13:46:45Z",
    "restricted": false,
    "active": false,
    "prohibit_login": false,
    "location": "",
    "website": "",
    "description": "",
    "visibility": "public",
    "followers_count": 0,
    "following_count": 0,
    "starred_repos_count": 0,
    "username": "yanwei"
  }
}

以下是gitea的示例:
变量名:REPO_FULL_NAME
表达式:$.repository.full_name
表达式类型:JSONPath
这个表达式会从 Gitea 发送的 JSON 数据中提取仓库的完整名称。

REF变量同理, 不再赘述.

白名单

以上配置完成后还需要配置白名单, 而且是互相配置, 详情待续

posted @ 2025-02-24 16:39  舒山  阅读(8)  评论(0编辑  收藏  举报