Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Start and restart Windows self-hosted runner automatically

fernando_arias October 11, 2022

Hi

I've successfully set up a self-hosted runner on a Windows Server 2019 VM. How would you set it up so that it starts on boot and restarts on failure?

 

2 answers

1 accepted

0 votes
Answer accepted
fernando_arias October 13, 2022

Ok, this is what I did. Assuming we have the agent in d:\bb...

 


Download for windows: commons-daemon-1.3.2-bin-windows.zip from: https://downloads.apache.org/commons/daemon/binaries/windows/

Then copy these files (I use the x64 version):
Copy-Item amd64/prunsrv.exe d:\bb\bin\
Copy-Item prunmgr.exe d:\bb\bin\
New-Item -ItemType Directory -Path d:\bb\logs

Create an install.ps1 file in d:\bb\bin
param (
    [string]$accountUuid = $(throw "-accountUuid is required."),
    [string]$repositoryUuid = "",
    [string]$runnerUuid = $(throw "-runnerUuid is required."),
    [string]$OAuthClientId = $(throw "-OAuthClientId is required."),
    [string]$OAuthClientSecret = $(throw "-OAuthClientSecret is required."),
    [string]$workingDirectory = $(throw "-workingDirectory is required."),
    [string]$serviceUser = $(throw "-serviceUser is required."),
    [string]$servicePassword = $(throw "-servicePassword is required."),
    [string]$runnerEnvironment = "PRODUCTION",
    [string]$scheduledStateUpdateInitialDelaySeconds = "0",    
    [string]$scheduledStateUpdatePeriodSeconds = "30",
    [string]$cleanupPreviousFolders = "false",
    [string]$logPath = "../logs"
)

if
(-not (Get-Command -name "java" -CommandType Application -errorAction SilentlyContinue))
{
    Write-Error "Java not installed"
    break
}

$argList = @("install",
            "BitBucketRunner",
            "--Install=D:\bb\bin\prunsrv.exe",
            "--Startup=auto",
            "--Classpath=runner.jar",
            "--StartClass=com.atlassian.pipelines.runner.core.ApplicationImpl",
            "--StartMethod=main",
            "--StopClass=com.atlassian.pipelines.runner.core.ApplicationImpl",
            "--StopMethod=main",
            '--DisplayName="Bitbucket Runner"',
            '--Description="Bitbucket Runner Service"',
            "--StartMode=jvm",
            "--StopMode=jvm",
            "--Jvm=auto",
            "--ServiceUser=$serviceUser",
            "--ServicePassword=$servicePassword",
            "++JvmOptions=-Dbitbucket.pipelines.runner.account.uuid=$accountUuid",
            "++JvmOptions=-Dbitbucket.pipelines.runner.repository.uuid=$repositoryUuid",
            "++JvmOptions=-Dbitbucket.pipelines.runner.uuid=$runnerUuid",
            "++JvmOptions=-Dbitbucket.pipelines.runner.environment=$runnerEnvironment",
            "++JvmOptions=-Dbitbucket.pipelines.runner.oauth.client.id=$OAuthClientId",
            "++JvmOptions=-Dbitbucket.pipelines.runner.oauth.client.secret=$OAuthClientSecret",
            "++JvmOptions=-Dbitbucket.pipelines.runner.directory.working=$workingDirectory",
            "++JvmOptions=-Dbitbucket.pipelines.runner.runtime=windows-powershell",
            "++JvmOptions=-Dbitbucket.pipelines.runner.scheduled.state.update.initial.delay.seconds=$scheduledStateUpdateInitialDelaySeconds",
            "++JvmOptions=-Dbitbucket.pipelines.runner.scheduled.state.update.period.seconds=$scheduledStateUpdatePeriodSeconds",
            "++JvmOptions=-Dbitbucket.pipelines.runner.cleanup.previous.folders=$cleanupPreviousFolders"
            "++JvmOptions=-Dfile.encoding=UTF-8",
            "++JvmOptions=-Dsun.jnu.encoding=UTF-8",
            "--JvmMs=256",
            "--JvmMx=1024",
            "--JvmSs=4000",
            "--LogPath=$logPath",
            "--LogPrefix=bitbucket-agent",
            "--StdOutput=$logPath\stdout.log",
            "--StdError=$logPath\stderr.log")

$params
= @{
    FilePath = "prunsrv"
    ArgumentList = $argList
}

$p = Start-Process @params -NoNewWindow -Wait

Create uninstall.ps1 in D:\bb\bin
$argList = @("delete",
            "BitBucketRunner")

$params = @{
    FilePath = "prunsrv"
    ArgumentList = $argList
}

$p = Start-Process @params -NoNewWindow -Wait

CD into D:\bb\bin and run
.\install.ps1 -accountUuid '{*****}' -runnerUuid '{*****}' -OAuthClientId ***** -OAuthClientSecret ***** -workingDirectory '..\temp' -serviceUser DOMAIN\user -servicePassword ***
sebastien_ezfocus November 7, 2022

I can't get my service to start after following this installation procedure. procmon requires an Oracle JDK properly installed in the registry but Bitbucket runner only works with OpenJDK. How did you do it? Thanks.

fernando_arias November 8, 2022

I used the Eclipse Adoptium Java version and then

1. Added this JAVA_HOME to environment variables: 

C:\Program Files\Eclipse Adoptium\jdk-11.0.16.101-hotspot

 

2. Added to PATH: 

C:\Program Files\Eclipse Adoptium\jdk-11.0.16.101-hotspot\bin

Like sebastien_ezfocus likes this
sebastien_ezfocus November 9, 2022

Thank you very much!

0 votes
Fabio Racobaldo _Herzum_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 11, 2022

Hi @fernando_arias ,

welcome to the Atlassian community!

You need to install a service. Please take a look to the following article  https://confluence.atlassian.com/bitbucketserver/start-and-stop-bitbucket-776640144.html

Hope this helps,

Fabio

fernando_arias October 13, 2022

Thanks, Fabio

Looks like the answer is for running Bitbucket as a service, but I'm talking about a Bitbucket Windows runner.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PERMISSIONS LEVEL
Site Admin
TAGS
AUG Leaders

Atlassian Community Events