安装Jenkins并在ruby中访问

1. 安装Jenkins

最近不知道为啥,根据官网Linux安装Jenkins的时候下不来安装包,提示连接超时。尝试多次无果后决定在window上安装

curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo tee /usr/share/keyrings/jenkins-keyring.asc > /dev/null
curl: (7) Failed to connect to pkg.jenkins.io port 443: Connection timed out

1.1.下载了一个jenkins.msi

安装步骤  Windows

安装到这步的时候,由于公司电脑权限问题我无法打开“Local Security Policy” 导致无法继续安装。 

1.2. 下载war包安装

大家如果装的是java8, 建议可以下载老一点的版本,比如2.2xx, 最新版用的是java11,不兼容。

下载地址:jenkins.war

安装步骤:WAR files

贴上内容:

The Jenkins Web application ARchive (WAR) file can be started from the command line like this:

  1. Download the latest Jenkins WAR file to an appropriate directory on your machine

  2. Open up a terminal/command prompt window to the download directory

  3. Run the command java -jar jenkins.war

  4. Browse to http://localhost:8080 and wait until the Unlock Jenkins page appears

  5. Continue on with the Post-installation setup wizard below

Notes:

  • This process does not automatically install any specific plugins. They need to installed separately via the Manage Jenkins > Manage Plugins page in Jenkins.

  • You can change the port by specifying the --httpPort option when you run the java -jar jenkins.war command. For example, to make Jenkins accessible through port 9090, then run Jenkins using the command:
    java -jar jenkins.war --httpPort=9090

有时候因为公司代理可能没法安装插件:可以通过Manage Jenkins ->Plugin Manager->Advanced->HTTP Proxy Configuration 配置好代理就可以了。

配置job的时候可以使用这个方式模拟sleep

echo "---start---"
ping 127.0.0.1 -n 5 -w 1000 > nul
echo "---end---"

好啦,Jenkins安装成功啦。

2.在ruby环境中调用Jenkins

2.1连接超时

2.6.5 :005 >  client
 => #<JenkinsApi::Client:0x562208267200 @ssl=false, @log_location=#<IO:<STDOUT>>, @log_level=1, @crumbs_enabled=nil, @follow_redirects=nil, @jenkins_path="", @timeout=120>, @http_open_timeout=10>, @http_read_timeout=120>
2.6.5 :004 > client.api_get_request("")
Traceback (most recent call last):
        1: from (irb):4
Net::OpenTimeout (execution expired)

使用下面命令测一下正常

traceroute 10.x.x.x -p 8080

后来发现设置代理后可以正常访问

http_proxy=http://代理IP:8080 rails c

2.2 调用接口时总是提醒“ForbiddenWithCrumb: Access denied”

I, [2022-11-28T02:33:38.752464 #22057]  INFO -- : Crumb expired.  Refetching from the server.
I, [2022-11-28T02:33:39.375018 #22057]  INFO -- : Retrying: 3 out of 3 times...
E, [2022-11-28T02:33:39.375166 #22057] ERROR -- : JenkinsApi::Exceptions::ForbiddenWithCrumb: Access denied. Please ensure that Jenkins is set up to allow access to this operation. A crumb was used in attempt to access operation. Access denied. Please ensure that Jenkins is set up to allow access to this operation.

通过创建一个token, 然后替换掉密码就可以啦。

token 生成步骤

好啦,我已经可以成功调用啦