Skip to content

Commit

Permalink
Merge pull request #57 from bitrise-io/config-cache
Browse files Browse the repository at this point in the history
feat: ACI-2926 Generate config cache compatible config
  • Loading branch information
zsolt-marta-bitrise authored Sep 30, 2024
2 parents f600e2a + dc313a4 commit 92a7f62
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 39 deletions.
31 changes: 14 additions & 17 deletions internal/config/common/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,29 @@ func (cac CacheAuthConfig) TokenInGradleFormat() string {
}

// ReadAuthConfigFromEnvironments reads auth information from the environment variables
// - if BITRISEIO_BITRISE_SERVICES_ACCESS_TOKEN is provided, use that
// - otherwise, if BITRISE_BUILD_CACHE_AUTH_TOKEN and BITRISE_BUILD_CACHE_WORKSPACE_ID is provided, use that
// - otherwise return error
func ReadAuthConfigFromEnvironments(envProvider func(string) string) (CacheAuthConfig, error) {
authTokenEnv := envProvider("BITRISE_BUILD_CACHE_AUTH_TOKEN")
workspaceIDEnv := envProvider("BITRISE_BUILD_CACHE_WORKSPACE_ID")

if len(authTokenEnv) > 0 && len(workspaceIDEnv) > 0 {
return CacheAuthConfig{
AuthToken: authTokenEnv,
WorkspaceID: workspaceIDEnv,
}, nil
}

// Try to fall back to JWT which is always available on Bitrise.
// It's a JWT token which already includes the workspace ID.
if serviceToken := envProvider("BITRISEIO_BITRISE_SERVICES_ACCESS_TOKEN"); len(serviceToken) > 0 {
// Bitrise service access token specified, use it for auth.
// It's a JWT token which already includes the workspace ID.
return CacheAuthConfig{
AuthToken: serviceToken,
}, nil
}

// No Bitrise Service Access Token specified.
// In this case both AuthToken and Workspace ID required,
authTokenEnv := envProvider("BITRISE_BUILD_CACHE_AUTH_TOKEN")
// Write specific errors for each case.
if len(authTokenEnv) < 1 {
return CacheAuthConfig{}, errAuthTokenNotProvided
}

workspaceIDEnv := envProvider("BITRISE_BUILD_CACHE_WORKSPACE_ID")
if len(workspaceIDEnv) < 1 {
return CacheAuthConfig{}, errWorkspaceIDNotProvided
}

return CacheAuthConfig{
AuthToken: authTokenEnv,
WorkspaceID: workspaceIDEnv,
}, nil
return CacheAuthConfig{}, errWorkspaceIDNotProvided
}
4 changes: 2 additions & 2 deletions internal/config/common/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func Test_ReadAuthConfigFromEnvironments(t *testing.T) {
"BITRISE_BUILD_CACHE_WORKSPACE_ID": "WorkspaceIDValue",
}))
require.NoError(t, err)
// BITRISEIO_BITRISE_SERVICES_ACCESS_TOKEN wins
assert.Equal(t, CacheAuthConfig{AuthToken: "ServiceAccessTokenValue"}, authToken)
// BITRISE_BUILD_CACHE_AUTH_TOKEN wins
assert.Equal(t, CacheAuthConfig{AuthToken: "AuthTokenValue", WorkspaceID: "WorkspaceIDValue"}, authToken)
})
}

Expand Down
22 changes: 9 additions & 13 deletions internal/config/gradle/gradleconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ func TestGenerateInitGradle(t *testing.T) {
CIProvider: "BestCI",
RepoURL: "https://github.com/some/repo",
// Bitrise CI specific
BitriseAppID: "BitriseAppID1",
BitriseBuildID: "BitriseBuildID1",
BitriseWorkflowName: "BitriseWorkflowName1",
BitriseAppID: "BitriseAppID1",
},
},
want: expectedInitScriptWithoutMetrics,
Expand All @@ -76,9 +74,7 @@ func TestGenerateInitGradle(t *testing.T) {
CIProvider: "BestCI",
RepoURL: "https://github.com/some/repo",
// Bitrise CI specific
BitriseAppID: "BitriseAppID1",
BitriseBuildID: "BitriseBuildID1",
BitriseWorkflowName: "BitriseWorkflowName1",
BitriseAppID: "BitriseAppID1",
},
},
want: expectedInitScriptWithMetrics,
Expand Down Expand Up @@ -155,7 +151,7 @@ initscript {
maven(url="https://jitpack.io")
}
dependencies {
classpath("io.bitrise.gradle:remote-cache:1.2.8")
classpath("io.bitrise.gradle:remote-cache:1.2.9")
classpath("io.bitrise.gradle:gradle-analytics:2.1.7")
}
}
Expand All @@ -174,6 +170,7 @@ settingsEvaluated {
isPush = true
debug = true
blobValidationLevel = "warning"
collectMetadata = false
}
}
}
Expand All @@ -193,8 +190,6 @@ rootProject {
bitrise {
appSlug.set("BitriseAppID1")
buildSlug.set("BitriseBuildID1")
workflowName.set("BitriseWorkflowName1")
}
}
}
Expand All @@ -211,7 +206,7 @@ initscript {
maven(url="https://jitpack.io")
}
dependencies {
classpath("io.bitrise.gradle:remote-cache:1.2.8")
classpath("io.bitrise.gradle:remote-cache:1.2.9")
classpath("io.bitrise.gradle:gradle-analytics:2.1.7")
}
}
Expand All @@ -230,6 +225,7 @@ settingsEvaluated {
isPush = true
debug = true
blobValidationLevel = "warning"
collectMetadata = false
}
}
}
Expand Down Expand Up @@ -264,7 +260,7 @@ initscript {
maven(url="https://jitpack.io")
}
dependencies {
classpath("io.bitrise.gradle:remote-cache:1.2.8")
classpath("io.bitrise.gradle:remote-cache:1.2.9")
}
}
Expand Down Expand Up @@ -298,7 +294,7 @@ initscript {
maven(url="https://jitpack.io")
}
dependencies {
classpath("io.bitrise.gradle:remote-cache:1.2.8")
classpath("io.bitrise.gradle:remote-cache:1.2.9")
}
}
Expand Down Expand Up @@ -332,7 +328,7 @@ initscript {
maven(url="https://jitpack.io")
}
dependencies {
classpath("io.bitrise.gradle:remote-cache:1.2.8")
classpath("io.bitrise.gradle:remote-cache:1.2.9")
}
}
Expand Down
9 changes: 3 additions & 6 deletions internal/config/gradle/initd.gradle.kts.gotemplate
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ settingsEvaluated {
isPush = {{ .IsPushEnabled }}
debug = {{ .IsDebugEnabled }}
blobValidationLevel = "{{ .ValidationLevel }}"
{{- if .IsAnalyticsEnabled }}
collectMetadata = false
{{- end }}
}
}
}
Expand All @@ -53,12 +56,6 @@ rootProject {
{{- if .CacheConfigMetadata.BitriseAppID }}
appSlug.set("{{ .CacheConfigMetadata.BitriseAppID }}")
{{- end }}
{{- if .CacheConfigMetadata.BitriseBuildID }}
buildSlug.set("{{ .CacheConfigMetadata.BitriseBuildID }}")
{{- end }}
{{- if .CacheConfigMetadata.BitriseWorkflowName }}
workflowName.set("{{ .CacheConfigMetadata.BitriseWorkflowName }}")
{{- end }}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/consts/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const (
AnalyticsServiceEndpoint = "https://xcode-analytics.services.bitrise.io"

// Gradle Remote Build Cache related consts
GradleRemoteBuildCachePluginDepVersion = "1.2.8"
GradleRemoteBuildCachePluginDepVersion = "1.2.9"

// Gradle Analytics related consts
GradleAnalyticsPluginDepVersion = "2.1.7"
Expand Down

0 comments on commit 92a7f62

Please sign in to comment.