This is how:

import com.atlassian.confluence.setup.settings.SettingsManager
import com.atlassian.sal.api.component.ComponentLocator

SettingsManager settingsManager = (SettingsManager) ComponentLocator.getComponent(SettingsManager)
String baseUrl = settingsManager.getGlobalSettings().getBaseUrl()


I often use it like:

if (baseUrl.contains("test"))
{
	//We are in test environment
}
else
{
	//We are in prod environment
}