1

我已经使用 VS2017 buildtools 创建了一个 docker 映像(windowsservercore)来充当 Jenkins JNLP 代理。

dockerfile (以此为例):

FROM microsoft/windowsservercore

SHELL ["powershell.exe", "-ExecutionPolicy", "Bypass", "-Command"]

ENV TEST_CONTAINER=1 \
    VS_CHANNEL_URI=https://aka.ms/vs/15/release/799c44140/channel \
    VS_BUILDTOOLS_URI=https://aka.ms/vs/15/release/799c44140/vs_buildtools.exe \
    VS_BUILDTOOLS_SHA256=FA29EB83297AECADB0C4CD41E54512C953164E64EEDD9FB9D3BF9BD70C9A2D29 \
    NUGET_URI=https://dist.nuget.org/win-x86-commandline/v4.1.0/nuget.exe \
    NUGET_SHA256=4C1DE9B026E0C4AB087302FF75240885742C0FAA62BD2554F913BBE1F6CB63A0

# Install Java
RUN Invoke-WebRequest -Uri 'http://javadl.oracle.com/webapps/download/AutoDL?BundleId=210185' -OutFile 'C:\jreinstaller.exe'; \
    $p = Start-Process -Wait -PassThru -FilePath C:\jreinstaller.exe -ArgumentList "/s,INSTALLDIR=c:\Java\jre1.8.0_91,/L,install64.log" ; \
    del C:\jreinstaller.exe

RUN [System.Environment]::SetEnvironmentVariable('JAVA_HOME', 'c:\Java\jre1.8.0_91', 'Machine'); \
    [System.Environment]::SetEnvironmentVariable('PATH', "\"${env:PATH};c:\Java\jre1.8.0_91"", 'Machine')

#CMD [ "java.exe" ]

### Install VS2017 Build Tools

# Download nuget.exe
RUN $ErrorActionPreference = 'Stop'; \
    $ProgressPreference = 'SilentlyContinue'; \
    $VerbosePreference = 'Continue'; \
    New-Item -Path C:\bin -Type Directory | Out-Null; \
    [System.Environment]::SetEnvironmentVariable('PATH', "\"${env:PATH};C:\bin\"", 'Machine'); \
    Invoke-WebRequest -Uri $env:NUGET_URI -OutFile C:\bin\nuget.exe; \
    if ((Get-FileHash -Path C:\bin\nuget.exe -Algorithm SHA256).Hash -ne $env:NUGET_SHA256) { throw 'Download hash does not match' }    

# Download log collection utility
RUN $ErrorActionPreference = 'Stop'; \
    $ProgressPreference = 'SilentlyContinue'; \
    $VerbosePreference = 'Continue'; \
    Invoke-WebRequest -Uri https://aka.ms/vscollect.exe -OutFile C:\collect.exe

# Download vs_buildtools.exe
RUN $ErrorActionPreference = 'Stop'; \
    $ProgressPreference = 'SilentlyContinue'; \
    $VerbosePreference = 'Continue'; \
    Invoke-WebRequest -Uri $env:VS_BUILDTOOLS_URI -OutFile C:\vs_buildtools.exe; \
    if ((Get-FileHash -Path C:\vs_buildtools.exe -Algorithm SHA256).Hash -ne $env:VS_BUILDTOOLS_SHA256) { throw 'Download hash does not match' }

# Install Visual Studio Build Tools
RUN $ErrorActionPreference = 'Stop'; \
    $VerbosePreference = 'Continue'; \
    $p = Start-Process -Wait -PassThru -FilePath C:\vs_buildtools.exe -ArgumentList '--quiet --nocache --wait --installPath C:\BuildTools'; \
    if ($ret = $p.ExitCode) { c:\collect.exe; throw ('Install failed with exit code 0x{0:x}' -f $ret) }

ARG VERSION=3.14
ARG AGENT_WORKDIR=c:\\home\\jenkins\\agent

RUN mkdir c:\home\jenkins ; \ 
    mkdir c:\home\jenkins\.jenkins ; \ 
    mkdir -p c:\home\jenkins\agent

RUN Invoke-WebRequest -Uri "https://repo.jenkins-ci.org/public/org/jenkins-ci/main/remoting/3.14/remoting-3.14.jar" -OutFile C:\slave.jar

VOLUME c:\\home\\jenkins\\.jenkins
VOLUME ${AGENT_WORKDIR}

COPY jenkins-slave.ps1 c:\\jenkins-slave.ps1

WORKDIR /home/jenkins

ENTRYPOINT ["powershell.exe", ". C:\\jenkins-slave.ps1"]

jenkins-slave.ps1 文件:

param(
    [String]$url,
    [String]$secret,
    [String]$agentName,
    [String]$tunnel,
    [String]$workDir 
) 

$TUNNEL = ""
if (![String]::IsNullOrWhitespace($tunnel)) 
{
    TUNNEL="-tunnel " + $tunnel
} else {
    #TODO: testr ENV VAR JENKINS_TUNNEL
}

$OPT_JENKINS_SECRET = ""
if (![String]::IsNullOrWhitespace($secret))
{
    $OPT_JENKINS_SECRET = """" + $secret + """"
}

$OPT_JENKINS_AGENT_NAME = ""
if (![String]::IsNullOrWhitespace($agentName)) 
{
    $OPT_JENKINS_AGENT_NAME = """" + $agentName + """"
}
$OPT_JENKINS_WORKDIR = ""
if (![String]::IsNullOrWhitespace($workDir)) 
{
    $OPT_JENKINS_WORKDIR = "-workDir """ + $workDir + """"
}


$JNLP_PROTOCOL_OPTS="-Dorg.jenkinsci.remoting.engine.JnlpProtocol3.disabled=true"

& C:\Java\jre1.8.0_91\bin\java.exe $JAVA_OPTS $JNLP_PROTOCOL_OPTS -cp c:\slave.jar hudson.remoting.jnlp.Main -headless $TUNNEL -url $url $OPT_JENKINS_SECRET $OPT_JENKINS_AGENT_NAME 

ACI 容器组由Jenkins 插件分配

容器无法运行:

raphael@Azure:~$ az container logs -n windows-aci-3v8j6 -g dev-build-agents
Dec 22, 2017 3:17:02 AM hudson.remoting.jnlp.Main createEngine
INFO: Setting up slave: windows-aci-3v8j6
Dec 22, 2017 3:17:03 AM hudson.remoting.jnlp.Main$CuiListener <init>
INFO: Jenkins agent is running in headless mode.
Dec 22, 2017 3:17:03 AM hudson.remoting.Engine startEngine
WARNING: No Working Directory. Using the legacy JAR Cache location: C:\Users\ContainerAdministrator\.jenkins\cache\jars
Dec 22, 2017 3:17:06 AM hudson.remoting.jnlp.Main$CuiListener status
INFO: Locating server among [http://mybuildserver:8080/]
Dec 22, 2017 3:17:27 AM hudson.remoting.jnlp.Main$CuiListener error
SEVERE: Failed to connect to http://mybuildserver:8080/tcpSlaveAgentListener/: Connection timed out: connect
java.io.IOException: Failed to connect to http://mybuildserver:8080/tcpSlaveAgentListener/: Connection timed out: connect
        at org.jenkinsci.remoting.engine.JnlpAgentEndpointResolver.resolve(JnlpAgentEndpointResolver.java:168)
        at hudson.remoting.Engine.innerRun(Engine.java:503)
        at hudson.remoting.Engine.run(Engine.java:455)
Caused by: java.net.ConnectException: Connection timed out: connect
        at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
        at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
        at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
        at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
        at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
        at java.net.PlainSocketImpl.connect(Unknown Source)
        at java.net.SocksSocketImpl.connect(Unknown Source)
        at java.net.Socket.connect(Unknown Source)
        at sun.net.NetworkClient.doConnect(Unknown Source)
        at sun.net.www.http.HttpClient.openServer(Unknown Source)
        at sun.net.www.http.HttpClient.openServer(Unknown Source)
        at sun.net.www.http.HttpClient.<init>(Unknown Source)
        at sun.net.www.http.HttpClient.New(Unknown Source)
        at sun.net.www.http.HttpClient.New(Unknown Source)
        at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
        at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(Unknown Source)
        at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
        at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
        at org.jenkinsci.remoting.engine.JnlpAgentEndpointResolver.resolve(JnlpAgentEndpointResolver.java:165)
        ... 2 more

“mybuildserver”是 Azure 中的一个 VM,8080 端口对任何地方都是开放的。

Linux版本的container连接到mybuildserver;

在 Windows 2016 Server 上运行相同的映像(容器)可以工作。

更新:

使用内部 IP 也不起作用。

xxxx@Azure:~$ az container logs -n windows-aci-vgpz4 -g group
Jan 03, 2018 6:18:37 PM hudson.remoting.jnlp.Main createEngine
INFO: Setting up slave: windows-aci-vgpz4
Jan 03, 2018 6:18:38 PM hudson.remoting.jnlp.Main$CuiListener <init>
INFO: Jenkins agent is running in headless mode.
Jan 03, 2018 6:18:38 PM hudson.remoting.Engine startEngine
WARNING: No Working Directory. Using the legacy JAR Cache location: C:\Users\ContainerAdministrator\.jenkins\cache\jars
Jan 03, 2018 6:18:40 PM hudson.remoting.jnlp.Main$CuiListener status
INFO: Locating server among [http://10.0.0.5:8080/]
Jan 03, 2018 6:19:01 PM hudson.remoting.jnlp.Main$CuiListener error
SEVERE: Failed to connect to http://10.0.0.5:8080/tcpSlaveAgentListener/: Connection timed out: connect
java.io.IOException: Failed to connect to http://10.0.0.5:8080/tcpSlaveAgentListener/: Connection timed out: connect
        at org.jenkinsci.remoting.engine.JnlpAgentEndpointResolver.resolve(JnlpAgentEndpointResolver.java:168)
        at hudson.remoting.Engine.innerRun(Engine.java:503)
        at hudson.remoting.Engine.run(Engine.java:455)
Caused by: java.net.ConnectException: Connection timed out: connect
        at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
        at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
        at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
        at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
        at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
        at java.net.PlainSocketImpl.connect(Unknown Source)
        at java.net.SocksSocketImpl.connect(Unknown Source)
        at java.net.Socket.connect(Unknown Source)
        at sun.net.NetworkClient.doConnect(Unknown Source)
        at sun.net.www.http.HttpClient.openServer(Unknown Source)
        at sun.net.www.http.HttpClient.openServer(Unknown Source)
        at sun.net.www.http.HttpClient.<init>(Unknown Source)
        at sun.net.www.http.HttpClient.New(Unknown Source)
        at sun.net.www.http.HttpClient.New(Unknown Source)
        at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
        at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(Unknown Source)
        at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
        at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
        at org.jenkinsci.remoting.engine.JnlpAgentEndpointResolver.resolve(JnlpAgentEndpointResolver.java:165)
        ... 2 more
4

0 回答 0