It seems the problem was it didn’t use proxy. The build server needs a proxy to access the outside world.
But if it could not access https://services.gradle.org, should there not be a different error message?
I tried to configure the proxy under /etc/sysconfig/proxy, but it still would not work. Using wget works after I configure the proxy there.
I had to configure proxy as java properties with gradle for it to work.
./gradlew -Dhttps.proxyHost=proxy.company.no -Dhttps.proxyPort=3128 --version