Skip to content

Commit

Permalink
Merge pull request jenkinsci/docker-inbound-agent#130 from jglick/web…
Browse files Browse the repository at this point in the history
…socket

[JEP-222] Enable WebSocket mode by passing JENKINS_WEB_SOCKET=true
  • Loading branch information
MarkEWaite committed Feb 12, 2020
2 parents 14e26df + aeac3ab commit fdf9ed8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Optional environment variables:
* `JENKINS_SECRET`: agent secret, if not set as an argument
* `JENKINS_AGENT_NAME`: agent name, if not set as an argument
* `JENKINS_AGENT_WORKDIR`: agent work directory, if not set by optional parameter `-workDir`
* `JENKINS_WEB_SOCKET`: `true` if the connection should be made via WebSocket rather than TCP

## Configuration specifics

Expand Down
7 changes: 6 additions & 1 deletion jenkins-agent
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
# * JENKINS_SECRET : agent secret, if not set as an argument
# * JENKINS_AGENT_NAME : agent name, if not set as an argument
# * JENKINS_AGENT_WORKDIR : agent work directory, if not set by optional parameter -workDir
# * JENKINS_WEB_SOCKET: true if the connection should be made via WebSocket rather than TCP
# * JENKINS_DIRECT_CONNECTION: Connect directly to this TCP agent port, skipping the HTTP(S) connection parameter download.
# Value: "<HOST>:<PORT>"
# * JENKINS_INSTANCE_IDENTITY: The base64 encoded InstanceIdentity byte array of the Jenkins master. When this is set,
Expand Down Expand Up @@ -68,6 +69,10 @@ else
JENKINS_AGENT_NAME="$JENKINS_NAME"
fi

if [ "$JENKINS_WEB_SOCKET" = true ]; then
WEB_SOCKET=-webSocket
fi

if [ -n "$JENKINS_PROTOCOLS" ]; then
PROTOCOLS="-protocols $JENKINS_PROTOCOLS"
fi
Expand Down Expand Up @@ -108,5 +113,5 @@ else
#TODO: Handle the case when the command-line and Environment variable contain different values.
#It is fine it blows up for now since it should lead to an error anyway.

exec $JAVA_BIN $JAVA_OPTS -cp /usr/share/jenkins/agent.jar hudson.remoting.jnlp.Main -headless $TUNNEL $URL $WORKDIR $DIRECT $PROTOCOLS $INSTANCE_IDENTITY $OPT_JENKINS_SECRET $OPT_JENKINS_AGENT_NAME "$@"
exec $JAVA_BIN $JAVA_OPTS -cp /usr/share/jenkins/agent.jar hudson.remoting.jnlp.Main -headless $TUNNEL $URL $WORKDIR $WEB_SOCKET $DIRECT $PROTOCOLS $INSTANCE_IDENTITY $OPT_JENKINS_SECRET $OPT_JENKINS_AGENT_NAME "$@"
fi
8 changes: 7 additions & 1 deletion jenkins-agent.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Param(
$Name = '',
$Tunnel = '',
$WorkDir = 'C:/Users/jenkins/Agent',
$WebSocket = '',
$JavaHome = $env:JAVA_HOME
)

Expand All @@ -38,6 +39,7 @@ Param(
# * JENKINS_SECRET : agent secret, if not set as an argument
# * JENKINS_AGENT_NAME : agent name, if not set as an argument
# * JENKINS_AGENT_WORKDIR : agent work directory, if not set by optional parameter -workDir
# * JENKINS_WEB_SOCKET : true if the connection should be made via WebSocket rather than TCP

if(![System.String]::IsNullOrWhiteSpace($Cmd)) {
# if `docker run` only has one arguments, we assume user is running alternate command like `bash` to inspect the image
Expand Down Expand Up @@ -67,6 +69,10 @@ if(![System.String]::IsNullOrWhiteSpace($Cmd)) {
$WorkDir = " -workDir `"$WorkDir`""
}

if($Env:JENKINS_WEB_SOCKET -eq "true") {
$WebSocket = " -webSocket"
}

# if -Url is not provided, try env vars
if(![System.String]::IsNullOrWhiteSpace($env:JENKINS_URL)) {
if(![System.String]::IsNullOrWhiteSpace($Url)) {
Expand Down Expand Up @@ -108,5 +114,5 @@ if(![System.String]::IsNullOrWhiteSpace($Cmd)) {

#TODO: Handle the case when the command-line and Environment variable contain different values.
#It is fine it blows up for now since it should lead to an error anyway.
Start-Process -FilePath $JAVA_BIN -Wait -NoNewWindow -ArgumentList $("-cp C:/ProgramData/Jenkins/agent.jar hudson.remoting.jnlp.Main -headless$Tunnel$Url$WorkDir$Secret $Name")
Start-Process -FilePath $JAVA_BIN -Wait -NoNewWindow -ArgumentList $("-cp C:/ProgramData/Jenkins/agent.jar hudson.remoting.jnlp.Main -headless$Tunnel$Url$WorkDir$WebSocket$Secret $Name")
}

0 comments on commit fdf9ed8

Please sign in to comment.