Skip to content

Commit

Permalink
chore: Simplify CI implementations
Browse files Browse the repository at this point in the history
rradczewski committed May 24, 2024
1 parent 4c79ae7 commit f73b431
Showing 6 changed files with 46 additions and 60 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
trunkver
24 changes: 6 additions & 18 deletions internal/ci/circleci.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
package ci

import (
"os"
)

type Circleci struct{}

func (g *Circleci) IsInUse() bool {
return os.Getenv("CIRCLECI") == "true"
}

func (g *Circleci) Get() CIData {
return CIData{
SourceRef: "g" + os.Getenv("CIRCLE_SHA1")[:7],
BuildRef: os.Getenv("CIRCLE_WORKFLOW_JOB_ID"),
}
}

func init() {
RegisterCi(&Circleci{})
RegisterCi(&SimpleEnvBased{
Name: "CircleCI",
DetectKey: "CIRCLECI",
SourceRefKey: "CIRCLE_SHA1",
BuildRefKey: "CIRCLE_WORKFLOW_JOB_ID",
})
}
24 changes: 6 additions & 18 deletions internal/ci/github.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
package ci

import (
"os"
)

type Github struct{}

func (g *Github) IsInUse() bool {
return os.Getenv("GITHUB_SHA") != "" && os.Getenv("GITHUB_RUN_ID") != ""
}

func (g *Github) Get() CIData {
return CIData{
SourceRef: "g" + os.Getenv("GITHUB_SHA")[:7],
BuildRef: os.Getenv("GITHUB_RUN_ID"),
}
}

func init() {
RegisterCi(&Github{})
RegisterCi(&SimpleEnvBased{
Name: "Github",
DetectKey: "GITHUB_SHA",
SourceRefKey: "GITHUB_SHA",
BuildRefKey: "GITHUB_RUN_ID",
})
}
24 changes: 6 additions & 18 deletions internal/ci/gitlab.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
package ci

import (
"os"
)

type Gitlab struct{}

func (g *Gitlab) IsInUse() bool {
return os.Getenv("GITLAB_CI") == "true"
}

func (g *Gitlab) Get() CIData {
return CIData{
SourceRef: "g" + os.Getenv("CI_COMMIT_SHA")[:7],
BuildRef: os.Getenv("CI_JOB_ID"),
}
}

func init() {
RegisterCi(&Gitlab{})
RegisterCi(&SimpleEnvBased{
Name: "Gitlab",
DetectKey: "GITLAB_CI",
SourceRefKey: "CI_COMMIT_SHA",
BuildRefKey: "CI_JOB_ID",
})
}
21 changes: 21 additions & 0 deletions internal/ci/simpleEnvBased.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ci

import "os"

type SimpleEnvBased struct {
Name string
DetectKey string
SourceRefKey string
BuildRefKey string
}

func (g *SimpleEnvBased) IsInUse() bool {
return os.Getenv(g.DetectKey) != ""
}

func (g *SimpleEnvBased) Get() CIData {
return CIData{
SourceRef: "g" + os.Getenv(g.SourceRefKey)[:7],
BuildRef: os.Getenv(g.BuildRefKey),
}
}
12 changes: 6 additions & 6 deletions smoke.yaml
Original file line number Diff line number Diff line change
@@ -16,25 +16,25 @@ tests:
- name: ci/github
command: |
export GITHUB_SHA=1234567890abcdef1234567890abcdef12345678
export GITHUB_RUN_ID=R4242
export GITHUB_RUN_ID=GITHUBRUN
trunkver --timestamp "2024-05-22T16:25:48+02:00"
stdout: |
20240522142548.0.0-g1234567-R4242
20240522142548.0.0-g1234567-GITHUBRUN
- name: ci/gitlab
command: |
export GITLAB_CI=true
export CI_COMMIT_SHA=1234567890abcdef1234567890abcdef12345678
export CI_JOB_ID=R4242
export CI_JOB_ID=GITLABRUN
trunkver --timestamp "2024-05-22T16:25:48+02:00"
stdout: |
20240522142548.0.0-g1234567-R4242
20240522142548.0.0-g1234567-GITLABRUN
- name: ci/circleci
command: |
export CIRCLECI=true
export CIRCLE_SHA1=1234567890abcdef1234567890abcdef12345678
export CIRCLE_WORKFLOW_JOB_ID=R4242
export CIRCLE_WORKFLOW_JOB_ID=CIRCLECIJOB
trunkver --timestamp "2024-05-22T16:25:48+02:00"
stdout: |
20240522142548.0.0-g1234567-R4242
20240522142548.0.0-g1234567-CIRCLECIJOB

0 comments on commit f73b431

Please sign in to comment.