Home > database >  SocketTimeoutException while trying to reach Gmail API from Jenkins
SocketTimeoutException while trying to reach Gmail API from Jenkins

Time:09-13

I'm running automated tests that call the Gmail API.

I'm going to say it (are you ready?): It works on my machine.

But when I run the same tests via Jenkins, I get a SocketTimeoutException: connect timed out error.

Some addresses have been added to the proxy's whitelist (but with no improvement):

  • accounts.google.com
  • gmail.googleapis.com

Where it fails:

JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
String APPLICATION_NAME = "TEST";

final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
// This one runs ok
Gmail service = new Gmail.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
    .setApplicationName(APPLICATION_NAME)
    .build();
// It fails here:
ListMessagesResponse response = service.
    users().
    messages().
    list("me").
    setQ("subject:"   messageTitle).
    execute();

Any help will be very appreciated :)

CodePudding user response:

We finally found how to fix the problem. It was, as expected, a proxy issue. My virtual machine was running behind a certain corporate proxy, and the Jenkins instance was running behind another one.

Instead of final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();

I put a custom final NetHttpTransport HTTP_TRANSPORT = (NetHttpTransport) newProxyTransport();

Which is defined here:

protected static Boolean isJenkinsMode = Boolean.getBoolean(System.getProperty("jenkinsMode"));

static HttpTransport newProxyTransport() throws GeneralSecurityException, IOException {
    NetHttpTransport.Builder builder = new NetHttpTransport.Builder();
    builder.trustCertificates(GoogleUtils.getCertificateTrustStore());
    String proxy = "default-proxy-address-which-works-on-my-machine.com";
    if(isJenkinsMode){
        proxy = "proxy-address-for-jenkins.com";
    }
    builder.setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxy, 3128)));
    return builder.build();
}

Then, from Jenkins, I run the Maven command with -DjenkinsMode=true.

  • Related