Skip to content
/ go-ci Public

A Go package that tells you whether the current environment is within a CI/CD pipeline.

License

Notifications You must be signed in to change notification settings

kitabisa/go-ci

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-ci

GoDoc tests

A Go package that provides utilities for detecting and handling CI/CD environments. It also has testing helper functions to conditionally skip or fail tests/benchmarks/fuzzing based on the CI/CD status.

Usage

Use IsCI or IsNotCI to check if the current environment is within a CI/CD pipeline.

go get github.com/kitabisa/go-ci@latest

Note

See CICDEnvVars for currently supported CI/CD pipelines.

Testing helper function

Import the go-ci package into your *_test.go files.

import (
	"testing"

	"github.com/kitabisa/go-ci"
	"github.com/stretchr/testify/assert"
)

func shouldSkip(t *testing.T) {
	assert.True(t, t.Skipped())
	t.Logf("Skipping '%s' test in CI/CD environment", t.Name())
}

func TestFuncShoulNotInCI(t *testing.T) {
	defer shouldSkip(t)

	t.Setenv("CI", "true")
	ci.SkipTestIfCI(t)

	// do your test...
}

Important

Please note that the checking mechanism uses a cache, so do NOT simulating CI/CD environment variables in actual test cases.

Available testing helper functions

func FailBenchmarkIfCI(b *testing.B)
func FailBenchmarkIfNotCI(b *testing.B)
func FailFuzzIfCI(f *testing.F)
func FailFuzzIfNotCI(f *testing.F)
func FailTestIfCI(t *testing.T)
func FailTestIfNotCI(t *testing.T)
func SkipBenchmarkIfCI(b *testing.B)
func SkipBenchmarkIfNotCI(b *testing.B)
func SkipFuzzIfCI(f *testing.F)
func SkipFuzzIfNotCI(f *testing.F)
func SkipTestIfCI(t *testing.T)
func SkipTestIfNotCI(t *testing.T)

Benchmark

$ go test -race -run="^$" -bench .
goos: linux
goarch: amd64
pkg: github.com/kitabisa/go-ci
cpu: 11th Gen Intel(R) Core(TM) i9-11900H @ 2.50GHz
BenchmarkIsCI/true-16         	39761295	        28.22 ns/op	       0 B/op	       0 allocs/op
BenchmarkIsCI/false-16        	42846458	        34.67 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/kitabisa/go-ci	3.680s

License

go-ci is released by @dwisiswant0 under the Apache 2.0 license. See LICENSE.

About

A Go package that tells you whether the current environment is within a CI/CD pipeline.

Resources

License

Security policy

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Languages