jenkins远程触发构建

jenkins远程触发构建的方法介绍

方式一

设置API Token,其它Basic Auth调用任务构建,此方式适用于脚本调用和代码调用

设置API Token

指定用户->用户页面->设置->API Token

curl调用

1
2
3
4
# jobName就是点进jenkins任务页面,地址栏url里的
curl http://hostname/jobName/build -u userName:APIToken
# 带参数
curl http://hostname/jobName/buildWithParameters -u userName:APIToken

方式二

使用Build Authorization Token Root插件,适合webhook之类的

  • 安装Build Authorization Token Root插件
  • 设置API Token
  • job页面,勾选“触发远程构建 (例如,使用脚本)”,身份验证令牌中填入API Token

直接浏览器输入测试:

1
2
http://hostname/buildByToken/build?job=jobName&token=APIToken
http://hostname/buildByToken/buildWithParameters?job=jobName&token=APIToken

elasticsearch分页大于10000条数据限制修改

elasticsearch采用from+size分页时,数据量大于1W会报错

1
Result window is too large, from + size must be less than or equal to: [10000] but was [100001]

解决方法

1
2
3
4
# 所有索引
curl -X PUT -H 'Content-Type: application/json' 172.24.0.171:9200/_all/_settings -d '{ "index.max_result_window" :"100000"}'
# 指定product_order索引
curl -X PUT -H 'Content-Type: application/json' 172.24.0.171:9200/product_order/_settings -d '{ "index.max_result_window" :"100000"}'

wsl2和Proxifier兼容问题

使用过Proxifier后,发现wsl2出问题了,打开子系统时提示参考的对象类型不支持尝试的操作。,搜了下网上的解决方法是通过下载NoLsp.exe解决

问题链接:https://github.com/microsoft/WSL/issues/4177#issuecomment-597736482,提供的用不了

这个里的可以用,https://github.com/dyingsu/nolsp

下载之后,管理员打开cmd执行命令

1
NoLsp.exe C:\windows\system32\wsl.exe

方法二

管理员身份在CMD执行

1
netsh winsock reset

如何查询指定时间范围内的日志

搞代码的都离不开通过查看日志处理问题;服务器上记录的日志都比较大,不会把整个日志文件拿来分析,通常会截取时间范围内的日志来分析问题,介绍几种查询时间范围日志方法。

处理文件肯定是离不开linux三剑客grep、sed、awk的。

演示截取2点到9点的日志

测试日志文件test.log,模拟所有2点到11点日志

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
{"timestamp":"2022-09-27T02:12:59+08:00","level":"debug"}
{"timestamp":"2022-09-27T02:14:42+08:00","level":"debug"}
{"timestamp":"2022-09-27T02:41:03+08:00","level":"debug"}
{"timestamp":"2022-09-27T02:45:59+08:00","level":"debug"}
{"timestamp":"2022-09-27T04:35:31+08:00","level":"debug"}
{"timestamp":"2022-09-27T04:35:31+08:00","level":"error"}
{"timestamp":"2022-09-27T04:35:31+08:00","level":"debug"}
{"timestamp":"2022-09-27T04:38:40+08:00","level":"debug"}
{"timestamp":"2022-09-27T04:49:55+08:00","level":"debug"}
{"timestamp":"2022-09-27T09:21:04+08:00","level":"debug"}
{"timestamp":"2022-09-27T09:21:04+08:00","level":"error"}
{"timestamp":"2022-09-27T09:21:04+08:00","level":"debug"}
{"timestamp":"2022-09-27T09:21:04+08:00","level":"error"}
{"timestamp":"2022-09-27T09:21:04+08:00","level":"debug"}
{"timestamp":"2022-09-27T09:21:19+08:00","level":"debug"}
{"timestamp":"2022-09-27T09:30:30+08:00","level":"debug"}
{"timestamp":"2022-09-27T09:30:35+08:00","level":"debug"}
{"timestamp":"2022-09-27T09:33:46+08:00","level":"debug"}
{"timestamp":"2022-09-27T09:37:02+08:00","level":"debug"}
{"timestamp":"2022-09-27T10:29:54+08:00","level":"debug"}
{"timestamp":"2022-09-27T10:29:59+08:00","level":"debug"}
{"timestamp":"2022-09-27T10:30:02+08:00","level":"debug"}
{"timestamp":"2022-09-27T10:30:03+08:00","level":"debug"}
{"timestamp":"2022-09-27T10:30:43+08:00","level":"debug"}
{"timestamp":"2022-09-27T10:30:44+08:00","level":"debug"}
{"timestamp":"2022-09-27T10:30:45+08:00","level":"debug"}
{"timestamp":"2022-09-27T10:30:46+08:00","level":"debug"}
{"timestamp":"2022-09-27T10:30:48+08:00","level":"debug"}
{"timestamp":"2022-09-27T10:56:22+08:00","level":"debug"}
{"timestamp":"2022-09-27T11:09:36+08:00","level":"debug"}
{"timestamp":"2022-09-27T11:09:41+08:00","level":"debug"}

正则方式

grep不支持范围,awk和sed支持范围,三个都支持正则匹配

git统计提交记录中修改过的文件

使用git的log命令

1
2
3
# start_commit_id 起始提交ID
# end_commit_id 结束提交ID
git log --author=abc --name-only --pretty= start_commit_id..end_commit_id

只显示文件pretty必须为空

1
2
# 显示commit_id和文件
git log --author=abc --name-only --pretty=oneline start_commit_id..end_commit_id

git错误invalid path解决方法

在windows上git clone代码时报错:error: invalid path ‘xxxx’

解决方法:

1
git config core.protectNTFS false

参数说明:

1
2
core.protectNTFS
If set to true, do not allow checkout of paths that would cause problems with the NTFS filesystem, e.g. conflict with 8.3 "short" names. Defaults to true on Windows, and false elsewhere.

git文件名过长错误解决方法

git使用过程中,出现error: unable to create file xxx: Filename too long Updated 0 paths from the index

原因是windows的git版本中如果是文件名太长了,git会报错

解决方法:

1
2
3
4
5
# 选其一
# 全局设置
git config --global core.longpaths true
# 当前项目设置
git config core.longpaths true

对git仓库进行清理

git仓库使用很长一段时间后,因为提交次数多了.git目录会越来越大;导致构建检出tag时耗时特别长。

整理了清理git仓库方法

使用bfg工具清理

下载地址:https://github.com/rtyley/bfg-repo-cleaner

使用步骤:

下载bfg,是一个jar文件,需求安装java环境才能执行

清理命令

1
2
# 清理>10M的文件
java -jar bfg-path/bfg.jar --strip-blobs-bigger-than 10M --no-blob-protection your-git-repo-path

进入仓库目录,执行

1
2
cd your-git-repo-path
git reflog expire --expire=now --all && git gc --prune=now --aggressive

漫长的等待过后,推送到远程

1
git push --force

自已的项目可以像这样搞,如果是多人使用的仓库,最好是清理后迁移成一个新仓库,然后所有人重新clone

mysql创建只读用户

创建只读用户

1
2
3
4
# 格式
GRANT SELECT ON dbName.tableName TO 'username'@'host' IDENTIFIED BY "password";
# 刷新权限
FLUSH PRIVILEGES;

例子,用户readonly_user可以在当前mysql的服务器,只读方式访问所有数据库

1
2
GRANT SELECT ON *.* TO 'readonly_user'@'localhost' IDENTIFIED BY "12345678";
FLUSH PRIVILEGES;

分步创建

1
2
3
CREATE USER 'readonly_user'@'%' IDENTIFIED BY '12345678';
GRANT SELECT ON *.* TO 'readonly_user'@'%';
FLUSH PRIVILEGES;

certbot配置证书

之前配置证书一直用https://github.com/acmesh-official/acme.sh,网络原因生成时好时坏;换成Certbot后简单多了

contos7下安装Certbot

certbot官网:https://certbot.eff.org/instructions

先安装snapd

官网https://snapcraft.io/docs/installing-snap-on-centos

1
2
3
4
sudo yum install epel-release
sudo yum install snapd
sudo systemctl enable --now snapd.socket
sudo ln -s /var/lib/snapd/snap /snap

更新snapd

1
2
sudo snap install core;
sudo snap refresh core

安装Certbot

1
2
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot

nginx证书

会自动找到conf所有域名并设置好证书

1
sudo certbot --nginx

centos7升级gcc.md

centos7默认安装的gcc版本是4.8.5,升级为最新版本

安装centos-release-scl

1
sudo yum install centos-release-scl

查看可安装的devtoolset版本

1
sudo yum search devtoolset

安装对应的版本

1
2
sudo yum install devtoolset-8-gcc
sudo yum install devtoolset-8-gcc-c++

安装devtoolset之后的目录是/opt/rh/devtoolset-8

激活

1
sudo source /opt/rh/devtoolset-8/enable

替换centos是老版本

1
2
3
4
5
6
# 替换gcc
sudo mv /usr/bin/gcc /usr/bin/gcc_bak
sudo ln -s /opt/rh/devtoolset-8/root/bin/gcc /usr/bin/gcc
# 替换g++
sudo mv /usr/bin/g++ /usr/bin/g++_bak
sudo ln -s /opt/rh/devtoolset-8/root/bin/g++ /usr/bin/g++

golang设置GOROOT报错

升级了go到1.17.7后,goland打开项目设置GOROOT设置不了,提示:

1
The selected directory is not a valid home for Go SDK

解决方法:

找到go安装目录

1
2
go env | grep GOROOT
# set GOROOT=D:\Go

找到D:\Go\src\runtime\internal\sys\zversion.go文件,添加

1
2
# 指定版本
const TheVersion = `go1.17.7`

重启goland,设置下拉里就出现go1.17.7的选项了

git遇到pull出现403问题

git拉代码失败,不提示输入账号密码,出现The requested URL returned error: 403

解决方法,清除密码缓存

1
2
3
git config --local --unset credential.helper
git config --global --unset credential.helper
git config --system --unset credential.helper

之后就可以重新输入账号密码拉代码了

windows子系统目录位置

方式一

打开我的电脑,在地址栏输入\\wsl$,确定之后就看到所有的子系统了

方式二

安装的地址路径为

1
2
3
C:\Users\用户\AppData\Local\Packages\对应的子系统\LocalState
# 例如
C:\Users\用户\AppData\Local\Packages\CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc\LocalState

windows家庭版安装hyper-v

新建bat文件hyper-v.bat,内容为

1
2
3
4
5
6
7
8
9
pushd "%~dp0"

dir /b %SystemRoot%\servicing\Packages\*Hyper-V*.mum >hyper-v.txt

for /f %%i in ('findstr /i . hyper-v.txt 2^>nul') do dism /online /norestart /add-package:"%SystemRoot%\servicing\Packages\%%i"

del hyper-v.txt

Dism /online /enable-feature /featurename:Microsoft-Hyper-V-All /LimitAccess /ALL

以管理员身份执行hyper-v.bat,然后等待安装,需要确认的都选择y