From f73b4318ea8f118eeb1ffb5afea2d60f3c9358c4 Mon Sep 17 00:00:00 2001 From: Raimo Radczewski Date: Sat, 25 May 2024 00:13:41 +0200 Subject: [PATCH] chore: Simplify CI implementations --- .gitignore | 1 + internal/ci/circleci.go | 24 ++++++------------------ internal/ci/github.go | 24 ++++++------------------ internal/ci/gitlab.go | 24 ++++++------------------ internal/ci/simpleEnvBased.go | 21 +++++++++++++++++++++ smoke.yaml | 12 ++++++------ 6 files changed, 46 insertions(+), 60 deletions(-) create mode 100644 .gitignore create mode 100644 internal/ci/simpleEnvBased.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f22f87a --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +trunkver diff --git a/internal/ci/circleci.go b/internal/ci/circleci.go index 387e784..a0d0c7e 100644 --- a/internal/ci/circleci.go +++ b/internal/ci/circleci.go @@ -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", + }) } diff --git a/internal/ci/github.go b/internal/ci/github.go index 655fb5a..533fd72 100644 --- a/internal/ci/github.go +++ b/internal/ci/github.go @@ -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", + }) } diff --git a/internal/ci/gitlab.go b/internal/ci/gitlab.go index 43efa42..7751939 100644 --- a/internal/ci/gitlab.go +++ b/internal/ci/gitlab.go @@ -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", + }) } diff --git a/internal/ci/simpleEnvBased.go b/internal/ci/simpleEnvBased.go new file mode 100644 index 0000000..4c533ba --- /dev/null +++ b/internal/ci/simpleEnvBased.go @@ -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), + } +} diff --git a/smoke.yaml b/smoke.yaml index 0322fc8..83fa591 100644 --- a/smoke.yaml +++ b/smoke.yaml @@ -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