github.com/konsorten/ktn-build-info@v1.0.11/ver/gitlab_ci.go (about)

     1  package ver
     2  
     3  import (
     4  	"os"
     5  
     6  	log "github.com/sirupsen/logrus"
     7  )
     8  
     9  func IsGitlabCIAvailable() bool {
    10  	return os.Getenv("GITLAB_CI") == "true"
    11  }
    12  
    13  func TryReadFromGitlabCI(ignoreRevision bool, ignoreProjectName bool) (*VersionInformation, error) {
    14  	if !IsGitlabCIAvailable() {
    15  		return nil, nil
    16  	}
    17  
    18  	vi := MakeVersionInformation()
    19  
    20  	// read project name
    21  	if !ignoreProjectName && vi.Name == "" {
    22  		bn := os.Getenv("CI_PROJECT_NAME")
    23  
    24  		if bn != "" {
    25  			vi.Name = bn
    26  
    27  			log.Debugf("Found Gitlab CI project name: %v", vi.Name)
    28  		}
    29  	}
    30  
    31  	// read revision
    32  	if !ignoreRevision && vi.Revision == "" {
    33  		bn := os.Getenv("CI_COMMIT_SHA")
    34  
    35  		if bn != "" {
    36  			vi.Revision = bn
    37  
    38  			log.Debugf("Found Gitlab CI VCS revision: %v", vi.Revision)
    39  		}
    40  	}
    41  
    42  	return vi, nil
    43  }