github.com/devtron-labs/ci-runner@v0.0.0-20240518055909-b2672f3349d7/executor/util/envUtils.go (about)

     1  /*
     2   *  Copyright 2020 Devtron Labs
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   *
    16   */
    17  
    18  package util
    19  
    20  import (
    21  	"encoding/json"
    22  	"fmt"
    23  	"github.com/caarlos0/env"
    24  	"github.com/devtron-labs/ci-runner/helper"
    25  	"github.com/devtron-labs/ci-runner/pubsub"
    26  	"github.com/devtron-labs/ci-runner/util"
    27  	"os"
    28  	"strconv"
    29  	"strings"
    30  )
    31  
    32  func GetGlobalEnvVariables(cicdRequest *helper.CiCdTriggerEvent) (map[string]string, error) {
    33  	envs := make(map[string]string)
    34  	envs["WORKING_DIRECTORY"] = util.WORKINGDIR
    35  	cfg := &pubsub.PubSubConfig{}
    36  	err := env.Parse(cfg)
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  	if helper.IsCIOrJobTypeEvent(cicdRequest.Type) {
    41  		image, err := helper.BuildDockerImagePath(cicdRequest.CommonWorkflowRequest)
    42  		if err != nil {
    43  			return nil, err
    44  		}
    45  
    46  		envs["DOCKER_IMAGE_TAG"] = cicdRequest.CommonWorkflowRequest.DockerImageTag
    47  		envs["DOCKER_REPOSITORY"] = cicdRequest.CommonWorkflowRequest.DockerRepository
    48  		envs["DOCKER_REGISTRY_URL"] = cicdRequest.CommonWorkflowRequest.DockerRegistryURL
    49  		envs["TRIGGER_BY_AUTHOR"] = cicdRequest.CommonWorkflowRequest.TriggerByAuthor
    50  		envs["DOCKER_IMAGE"] = image
    51  
    52  		if cicdRequest.Type == util.JOBEVENT {
    53  			envs["JOB_NAME"] = cicdRequest.CommonWorkflowRequest.AppName
    54  		} else {
    55  			envs["APP_NAME"] = cicdRequest.CommonWorkflowRequest.AppName
    56  		}
    57  		//adding GIT_MATERIAL_REQUEST in env for semgrep plugin
    58  		CiMaterialRequestArr := ""
    59  		if cicdRequest.CommonWorkflowRequest.CiProjectDetails != nil {
    60  			for _, ciProjectDetail := range cicdRequest.CommonWorkflowRequest.CiProjectDetails {
    61  				GitRepoSplit := strings.Split(ciProjectDetail.GitRepository, "/")
    62  				GitRepoName := ""
    63  				if len(GitRepoSplit) > 0 {
    64  					GitRepoName = strings.Split(GitRepoSplit[len(GitRepoSplit)-1], ".")[0]
    65  				}
    66  				CiMaterialRequestArr = CiMaterialRequestArr +
    67  					fmt.Sprintf("%s,%s,%s,%s|", GitRepoName, ciProjectDetail.CheckoutPath, ciProjectDetail.SourceValue, ciProjectDetail.CommitHash)
    68  			}
    69  		}
    70  		envs["GIT_MATERIAL_REQUEST"] = CiMaterialRequestArr // GIT_MATERIAL_REQUEST will be of form "<repoName>/<checkoutPath>/<BranchName>/<CommitHash>"
    71  		fmt.Println(envs["GIT_MATERIAL_REQUEST"])
    72  
    73  		// adding ACCESS_KEY,SECRET_KEY, AWS_REGION, LAST_FETCHED_TIME for polling-plugin
    74  		envs["ACCESS_KEY"] = cicdRequest.CommonWorkflowRequest.AccessKey
    75  		envs["SECRET_KEY"] = cicdRequest.CommonWorkflowRequest.SecretKey
    76  		envs["AWS_REGION"] = cicdRequest.CommonWorkflowRequest.AwsRegion
    77  		envs["LAST_FETCHED_TIME"] = cicdRequest.CommonWorkflowRequest.CiArtifactLastFetch.String()
    78  
    79  		//adding some envs for Image scanning plugin
    80  		envs["PIPELINE_ID"] = strconv.Itoa(cicdRequest.CommonWorkflowRequest.PipelineId)
    81  		envs["TRIGGERED_BY"] = strconv.Itoa(cicdRequest.CommonWorkflowRequest.TriggeredBy)
    82  		envs["DOCKER_REGISTRY_ID"] = cicdRequest.CommonWorkflowRequest.DockerRegistryId
    83  		envs["IMAGE_SCANNER_ENDPOINT"] = cfg.ImageScannerEndpoint
    84  		envs["IMAGE_SCAN_MAX_RETRIES"] = strconv.Itoa(cicdRequest.CommonWorkflowRequest.ImageScanMaxRetries)
    85  		envs["IMAGE_SCAN_RETRY_DELAY"] = strconv.Itoa(cicdRequest.CommonWorkflowRequest.ImageScanRetryDelay)
    86  
    87  		// setting extraEnvironmentVariables
    88  		for k, v := range cicdRequest.CommonWorkflowRequest.ExtraEnvironmentVariables {
    89  			envs[k] = v
    90  		}
    91  		// for skopeo plugin, list of destination images againt registry name eg: <registry_name>: [<i1>,<i2>]
    92  		RegistryDestinationImage, _ := json.Marshal(cicdRequest.CommonWorkflowRequest.RegistryDestinationImageMap)
    93  		RegistryCredentials, _ := json.Marshal(cicdRequest.CommonWorkflowRequest.RegistryCredentialMap)
    94  		envs["REGISTRY_DESTINATION_IMAGE_MAP"] = string(RegistryDestinationImage)
    95  		envs["REGISTRY_CREDENTIALS"] = string(RegistryCredentials)
    96  	} else {
    97  		envs["DOCKER_IMAGE"] = cicdRequest.CommonWorkflowRequest.CiArtifactDTO.Image
    98  		envs["DEPLOYMENT_RELEASE_ID"] = strconv.Itoa(cicdRequest.CommonWorkflowRequest.DeploymentReleaseCounter)
    99  		envs["DEPLOYMENT_UNIQUE_ID"] = strconv.Itoa(cicdRequest.CommonWorkflowRequest.WorkflowRunnerId)
   100  		envs["CD_TRIGGERED_BY"] = cicdRequest.CommonWorkflowRequest.DeploymentTriggeredBy
   101  		envs["CD_TRIGGER_TIME"] = cicdRequest.CommonWorkflowRequest.DeploymentTriggerTime.String()
   102  
   103  		// to support legacy yaml based script trigger
   104  		envs["DEVTRON_CD_TRIGGERED_BY"] = cicdRequest.CommonWorkflowRequest.DeploymentTriggeredBy
   105  		envs["DEVTRON_CD_TRIGGER_TIME"] = cicdRequest.CommonWorkflowRequest.DeploymentTriggerTime.String()
   106  
   107  		//adding some envs for Image scanning plugin
   108  		envs["TRIGGERED_BY"] = strconv.Itoa(cicdRequest.CommonWorkflowRequest.TriggeredBy)
   109  		envs["DOCKER_REGISTRY_ID"] = cicdRequest.CommonWorkflowRequest.DockerRegistryId
   110  		envs["IMAGE_SCANNER_ENDPOINT"] = cfg.ImageScannerEndpoint
   111  		envs["IMAGE_SCAN_MAX_RETRIES"] = strconv.Itoa(cicdRequest.CommonWorkflowRequest.ImageScanMaxRetries)
   112  		envs["IMAGE_SCAN_RETRY_DELAY"] = strconv.Itoa(cicdRequest.CommonWorkflowRequest.ImageScanRetryDelay)
   113  
   114  		for k, v := range cicdRequest.CommonWorkflowRequest.ExtraEnvironmentVariables {
   115  			envs[k] = v
   116  		}
   117  		// for skopeo plugin, list of destination images againt registry name eg: <registry_name>: [<i1>,<i2>]
   118  		RegistryDestinationImage, _ := json.Marshal(cicdRequest.CommonWorkflowRequest.RegistryDestinationImageMap)
   119  		RegistryCredentials, _ := json.Marshal(cicdRequest.CommonWorkflowRequest.RegistryCredentialMap)
   120  		envs["REGISTRY_DESTINATION_IMAGE_MAP"] = string(RegistryDestinationImage)
   121  		envs["REGISTRY_CREDENTIALS"] = string(RegistryCredentials)
   122  	}
   123  	return envs, nil
   124  }
   125  
   126  func GetSystemEnvVariables() map[string]string {
   127  	envs := make(map[string]string)
   128  	//get all environment variables
   129  	envVars := os.Environ()
   130  	for _, envVar := range envVars {
   131  		subs := strings.SplitN(envVar, "=", 2)
   132  		envs[subs[0]] = subs[1]
   133  	}
   134  	return envs
   135  }