Skip to content

Commit

Permalink
Use docker on linux to speed up builds (#38)
Browse files Browse the repository at this point in the history
* Use docker on linux to speed up builds

* Improve cleanup
  • Loading branch information
danielmarbach authored Aug 10, 2024
1 parent 81ec394 commit 1b1345c
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 35 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ npm install

When changing `index.js`, run `npm run prepare` afterwards to update the output in the `dist` folder.


## License

The scripts and documentation in this project are released under the [MIT License](LICENSE).
19 changes: 18 additions & 1 deletion cleanup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,21 @@ param (
[string]$RabbitMQName
)

$ignore = az container delete --resource-group GitHubActions-RG --name $RabbitMQName --yes
$resourceGroup = $Env:RESOURCE_GROUP_OVERRIDE ?? "GitHubActions-RG"
$runnerOs = $Env:RUNNER_OS ?? "Linux"

if ($runnerOs -eq "Linux") {
Write-Output "Killing Docker container $RabbitMQName"
docker kill $RabbitMQName

Write-Output "Removing Docker container $RabbitMQName"
docker rm $RabbitMQName
}
elseif ($runnerOs -eq "Windows") {
Write-Output "Deleting Azure container $RabbitMQName"
az container delete --resource-group $resourceGroup --name $RabbitMQName --yes | Out-Null
}
else {
Write-Output "$runnerOs not supported"
exit 1
}
92 changes: 59 additions & 33 deletions setup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,51 +9,76 @@ param (
[string]$registryPass
)

$hostInfo = curl -H Metadata:true "169.254.169.254/metadata/instance?api-version=2017-08-01" | ConvertFrom-Json
$region = $hostInfo.compute.location
$runnerOsTag = "RunnerOS=$($Env:RUNNER_OS)"
$packageTag = "Package=$tagName"
$dockerImage = "rabbitmq:$imageTag"
$runnerOs = $Env:RUNNER_OS ?? "Linux"
$resourceGroup = $Env:RESOURCE_GROUP_OVERRIDE ?? "GitHubActions-RG"
$ipAddress = "127.0.0.1"

echo "hostname=$hostname" | Out-File -FilePath $Env:GITHUB_OUTPUT -Encoding utf-8 -Append
echo "Creating RabbitMQ container $hostname in $region (This can take a while.)"
if ($runnerOs -eq "Linux") {
Write-Output "Running Rabbit in container $($containerName) using Docker"

$azureContainerCreate = "az container create --image rabbitmq:$imageTag --name $hostname --location $region --dns-name-label $hostname --resource-group GitHubActions-RG --cpu 4 --memory 16 --ports 5672 15672 --ip-address public"

if ($registryUser -and $registryPass) {
echo "Creating container with login to $registryLoginServer"
$azureContainerCreate = "$azureContainerCreate --registry-login-server $registryLoginServer --registry-username $registryUser --registry-password $registryPass"
} else {
echo "Creating container with anonymous credentials"
docker run --name "$($hostname)" -d -p "5672:5672" -p "15672:15672" $dockerImage
}
elseif ($runnerOs -eq "Windows") {

$jsonResult = Invoke-Expression $azureContainerCreate
if (!$jsonResult) {
Write-Output "Failed to create RabbitMQ container"
exit 1;
}
if ($Env:REGION_OVERRIDE) {
$region = $Env:REGION_OVERRIDE
}
else {
$hostInfo = curl -H Metadata:true "169.254.169.254/metadata/instance?api-version=2017-08-01" | ConvertFrom-Json
$region = $hostInfo.compute.location
}

$details = $jsonResult | ConvertFrom-Json
$runnerOsTag = "RunnerOS=$($Env:RUNNER_OS)"
$packageTag = "Package=$tagName"
$dateTag = "Created=$(Get-Date -Format "yyyy-MM-dd")"

if (!$details.ipAddress) {
Write-Output "Failed to create RabbitMQ container $hostname in $region"
Write-Output $jsonResult
exit 1;
}
$azureContainerCreate = "az container create --image $dockerImage --name $hostname --location $region --dns-name-label $hostname --resource-group $resourceGroup --cpu 4 --memory 16 --ports 5672 15672 --ip-address public"

if ($registryUser -and $registryPass) {
Write-Output "Creating container with login to $registryLoginServer"
$azureContainerCreate = "$azureContainerCreate --registry-login-server $registryLoginServer --registry-username $registryUser --registry-password $registryPass"
} else {
Write-Output "Creating container with anonymous credentials"
}

Write-Output "Creating RabbitMQ container $hostname in $region (This can take a while.)"

$jsonResult = Invoke-Expression $azureContainerCreate
if (!$jsonResult) {
Write-Output "Failed to create RabbitMQ container"
exit 1;
}

$details = $jsonResult | ConvertFrom-Json

$ip=$details.ipAddress.ip
if (!$details.ipAddress) {
Write-Output "Failed to create RabbitMQ container $hostname in $region"
Write-Output $jsonResult
exit 1;
}

echo "::add-mask::$ip"
echo "Tagging container image"
$ipAddress=$details.ipAddress.ip

$dateTag = "Created=$(Get-Date -Format "yyyy-MM-dd")"
$ignore = az tag create --resource-id $details.id --tags $packageTag $runnerOsTag $dateTag
Write-Output "::add-mask::$ipAddress"
Write-Output "Tagging container image"
az tag create --resource-id $details.id --tags $packageTag $runnerOsTag $dateTag | Out-Null

echo "$connectionStringName=host=$ip" | Out-File -FilePath $Env:GITHUB_ENV -Encoding utf-8 -Append
}
else {
Write-Output "$runnerOs not supported"
exit 1
}

Write-Output "hostname=$hostname" | Out-File -FilePath $Env:GITHUB_OUTPUT -Encoding utf-8 -Append
Write-Output "$connectionStringName=host=$ipAddress" | Out-File -FilePath $Env:GITHUB_ENV -Encoding utf-8 -Append
if (-not [string]::IsNullOrWhiteSpace($hostEnvVarName)) {
echo "$hostEnvVarName=$ip" | Out-File -FilePath $Env:GITHUB_ENV -Encoding utf-8 -Append
Write-Output "$hostEnvVarName=$ipAddress" | Out-File -FilePath $Env:GITHUB_ENV -Encoding utf-8 -Append
}

$uri = "http://" + $ip + ":15672/api/health/checks/virtual-hosts"
Write-Output "::group::Testing connection"

$uri = "http://" + $ipAddress + ":15672/api/health/checks/virtual-hosts"
$tries = 0

do {
Expand All @@ -68,4 +93,5 @@ do {
if ($response.status -ne "ok") {
Write-Output "Failed to connect after 50 attempts";
exit 1
}
}
Write-Output "::endgroup::"

0 comments on commit 1b1345c

Please sign in to comment.