diff --git a/README.md b/README.md index 902df9f..4395b01 100644 --- a/README.md +++ b/README.md @@ -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). diff --git a/cleanup.ps1 b/cleanup.ps1 index 96a7821..bd39510 100644 --- a/cleanup.ps1 +++ b/cleanup.ps1 @@ -2,4 +2,21 @@ param ( [string]$RabbitMQName ) -$ignore = az container delete --resource-group GitHubActions-RG --name $RabbitMQName --yes \ No newline at end of file +$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 +} \ No newline at end of file diff --git a/setup.ps1 b/setup.ps1 index 5ffe964..a3fe774 100644 --- a/setup.ps1 +++ b/setup.ps1 @@ -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 { @@ -68,4 +93,5 @@ do { if ($response.status -ne "ok") { Write-Output "Failed to connect after 50 attempts"; exit 1 -} \ No newline at end of file +} +Write-Output "::endgroup::" \ No newline at end of file