node {
    stage('Checkout') {
        checkout scm
    }
    if ("${env.BRANCH_NAME}".startsWith('my-')) {
        print "è un branch da non compilare"
        currentBuild.result = 'SUCCESS'
        return
    }
    stage('Extract Git Info') {
        commitId = sh(returnStdout: true, script: 'git --no-pager log -1 --pretty=format:"%H"')
        commitDate = sh(returnStdout: true, script: 'git --no-pager log -1 --pretty=format:"%ai"')
        refs = sh(returnStdout: true, script: 'git --no-pager log -1 --pretty=format:"%d"')
    }
    stage('Build docker') {
        withEnv(['DOCKER_BUILDKIT=1']) {
            customImage = docker.build("briq/sangue:${env.BUILD_ID}", "--build-arg GIT_COMMIT_SHA=\"${commitId}\" --build-arg GIT_COMMIT_DATE=\"${commitDate}\" --build-arg GIT_COMMIT_REF=\"${refs}\" .")
        }
    }
    stage('Push to registry') {
        catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
            docker.withRegistry('https://docker.briq.it', 'briq-docker-cred') {
                customImage.push("${env.BRANCH_NAME}")
            }
        }
    }
}