@@ -80,6 +80,71 @@ func TestLoadEnvInheritance(t *testing.T) {
8080 }
8181}
8282
83+ func TestLoadBuildEnvCIVars (t * testing.T ) {
84+ appName := "test-app"
85+ mockedAWS := new (MockAWS )
86+ appConfigPrefix := fmt .Sprintf ("/apppack/apps/%s/config/" , appName )
87+ mockedAWS .On ("GetParametersByPath" , appConfigPrefix ).Return (map [string ]string {}, nil )
88+ mockedState := emptyState ()
89+ mockedState .On ("ReadEnvFile" ).Return (& map [string ]string {}, nil )
90+
91+ t .Setenv ("CODEBUILD_WEBHOOK_HEAD_REF" , "refs/heads/main" )
92+ t .Setenv ("CODEBUILD_RESOLVED_SOURCE_VERSION" , "deadbeef1234" )
93+ t .Setenv ("CODEBUILD_START_TIME" , "1234567890" )
94+ t .Setenv ("CODEBUILD_SOURCE_REPO_URL" , "https://github.com/org/repo" )
95+
96+ b := Build {
97+ Appname : appName ,
98+ CodebuildBuildId : CodebuildBuildId ,
99+ aws : mockedAWS ,
100+ state : mockedState ,
101+ Ctx : testContext ,
102+ }
103+ env , err := b .LoadBuildEnv ()
104+ if err != nil {
105+ t .Fatalf ("expected no error, got %s" , err )
106+ }
107+ if env ["CI_COMMIT_REF" ] != "refs/heads/main" {
108+ t .Errorf ("expected CI_COMMIT_REF=refs/heads/main, got %s" , env ["CI_COMMIT_REF" ])
109+ }
110+ if env ["CI_COMMIT_SHA" ] != "deadbeef1234" {
111+ t .Errorf ("expected CI_COMMIT_SHA=deadbeef1234, got %s" , env ["CI_COMMIT_SHA" ])
112+ }
113+ if env ["CI_BUILD_STARTED_AT" ] != "1234567890" {
114+ t .Errorf ("expected CI_BUILD_STARTED_AT=1234567890, got %s" , env ["CI_BUILD_STARTED_AT" ])
115+ }
116+ if env ["CI_REPOSITORY_URL" ] != "https://github.com/org/repo" {
117+ t .Errorf ("expected CI_REPOSITORY_URL=https://github.com/org/repo, got %s" , env ["CI_REPOSITORY_URL" ])
118+ }
119+ }
120+
121+ func TestLoadBuildEnvCIVarsFallback (t * testing.T ) {
122+ // When CODEBUILD_WEBHOOK_HEAD_REF is not set, CI_COMMIT_REF should fall back to CODEBUILD_SOURCE_VERSION
123+ appName := "test-app"
124+ mockedAWS := new (MockAWS )
125+ appConfigPrefix := fmt .Sprintf ("/apppack/apps/%s/config/" , appName )
126+ mockedAWS .On ("GetParametersByPath" , appConfigPrefix ).Return (map [string ]string {}, nil )
127+ mockedState := emptyState ()
128+ mockedState .On ("ReadEnvFile" ).Return (& map [string ]string {}, nil )
129+
130+ t .Setenv ("CODEBUILD_SOURCE_VERSION" , "refs/heads/feature-branch" )
131+
132+ b := Build {
133+ Appname : appName ,
134+ CodebuildBuildId : CodebuildBuildId ,
135+ aws : mockedAWS ,
136+ state : mockedState ,
137+ Ctx : testContext ,
138+ }
139+ env , err := b .LoadBuildEnv ()
140+ if err != nil {
141+ t .Fatalf ("expected no error, got %s" , err )
142+ }
143+ if env ["CI_COMMIT_REF" ] != "refs/heads/feature-branch" {
144+ t .Errorf ("expected CI_COMMIT_REF=refs/heads/feature-branch, got %s" , env ["CI_COMMIT_REF" ])
145+ }
146+ }
147+
83148func TestGenerateDockerEnvStrings (t * testing.T ) {
84149 env := map [string ]string {
85150 "FOO" : "bar" ,
0 commit comments