groovygitlabgithooksgitblit

GitBlit add a hook


I have a GitBlit instance on a windows server, and i want to set a hook on post receive callback to start a gitlab ci pipeline on another server.

I already have set a GitlabCi trigger who works well, but my hook doesn't. Here is build-gitlab-ci.groovy file :

import com.gitblit.GitBlit
import com.gitblit.Keys
import com.gitblit.models.RepositoryModel
import com.gitblit.models.UserModel
import com.gitblit.utils.JGitUtils
import org.eclipse.jgit.lib.Repository
import org.eclipse.jgit.revwalk.RevCommit
import org.eclipse.jgit.transport.ReceiveCommand
import org.eclipse.jgit.transport.ReceiveCommand.Result
import org.slf4j.Logger

logger.info("Gitlab-CI hook triggered by ${user.username} for ${repository.name}")

// POST :
def sendPostRequest(urlString, paramString) {
    def url = new URL(urlString)
    def conn = url.openConnection()
    conn.setDoOutput(true)
    def writer = new OutputStreamWriter(conn.getOutputStream())

    writer.write(paramString)
    writer.flush()
    String line
    def reader = new BufferedReader(new     InputStreamReader(conn.getInputStream()))
    while ((line = reader.readLine()) != null) {
        println line
    }
    writer.close()
    reader.close()
}
sendPostRequest("https://xxxxx/api/v4/projects/1/trigger/pipeline", "token=xxxxxxxx&ref=master")

The project configuration :

enter image description here

Moreover, i don't know where logger.info write the log, so i don't know if my script was executed well. Thanks for help


Solution

  • I found my problem, it was a SSL self-certificate problem. I added this code to ignore it :

    import com.gitblit.GitBlit
    import com.gitblit.Keys
    import com.gitblit.models.RepositoryModel
    import com.gitblit.models.UserModel
    import com.gitblit.utils.JGitUtils
    import org.eclipse.jgit.lib.Repository
    import org.eclipse.jgit.revwalk.RevCommit
    import org.eclipse.jgit.transport.ReceiveCommand
    import org.eclipse.jgit.transport.ReceiveCommand.Result
    import org.slf4j.Logger
    
    logger.info("Gitlab-CI hook triggered by ${user.username} for ${repository.name}")
    
    def nullTrustManager = [
        checkClientTrusted: { chain, authType ->  },
        checkServerTrusted: { chain, authType ->  },
        getAcceptedIssuers: { null }
    ]
    
    def nullHostnameVerifier = [
        verify: { hostname, session -> hostname.startsWith('yuml.me')}
    ]
    
    javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext.getInstance("SSL")
    sc.init(null, [nullTrustManager as  javax.net.ssl.X509TrustManager] as  javax.net.ssl.X509TrustManager[], null)
    javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory())
    javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(nullHostnameVerifier as javax.net.ssl.HostnameVerifier)
    
    def url = new URL("https://xxxx/api/v4/projects/{idProject}/trigger/pipeline")
    def conn = url.openConnection()
    conn.setDoOutput(true)
    def writer = new OutputStreamWriter(conn.getOutputStream())
    
    writer.write("token={token}&ref={branch}")
    writer.flush()
    String line
    def reader = new BufferedReader(new InputStreamReader(conn.getInputStream()))
    while ((line = reader.readLine()) != null) {
        println line
    }
    writer.close()
    reader.close()
    

    And I identified the error checking the logs in E:\gitblit-1.7.1\logs\gitblit-stdout.{date}.log.

    NB : stdout file date can be very old. Gitblit doesn't create a file per day. Mine had a name expired 4 months ago.