github.com/devtron-labs/ci-runner@v0.0.0-20240518055909-b2672f3349d7/helper/ArtefactUploadHelper.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 helper
    19  
    20  import (
    21  	"io"
    22  	"log"
    23  	"os"
    24  	"os/exec"
    25  	"path/filepath"
    26  
    27  	"github.com/devtron-labs/ci-runner/util"
    28  	"github.com/otiai10/copy"
    29  )
    30  
    31  //const BLOB_STORAGE_AZURE = "AZURE"
    32  //const BLOB_STORAGE_S3 = "S3"
    33  //const BLOB_STORAGE_GCP = "GCP"
    34  
    35  func UploadArtifact(cloudHelperBaseConfig *util.CloudHelperBaseConfig, artifactFiles map[string]string, artifactFileLocation string) error {
    36  	if len(artifactFiles) == 0 {
    37  		log.Println(util.DEVTRON, "no artifact to upload")
    38  		return nil
    39  	}
    40  	//collect in a dir
    41  	log.Println(util.DEVTRON, "artifact upload ", artifactFiles, artifactFileLocation)
    42  	err := os.Mkdir(util.TmpArtifactLocation, os.ModePerm)
    43  	if err != nil {
    44  		return err
    45  	}
    46  	for key, val := range artifactFiles {
    47  		loc := filepath.Join(util.TmpArtifactLocation, key)
    48  		err := os.Mkdir(loc, os.ModePerm)
    49  		if err != nil {
    50  			return err
    51  		}
    52  		err = copy.Copy(val, filepath.Join(loc, val))
    53  		if err != nil {
    54  			return err
    55  		}
    56  	}
    57  	_, err = ZipAndUpload(cloudHelperBaseConfig, artifactFileLocation)
    58  	return err
    59  }
    60  
    61  func ZipAndUpload(cloudHelperBaseConfig *util.CloudHelperBaseConfig, artifactFileName string) (bool, error) {
    62  	artifactUploaded := false
    63  	if !cloudHelperBaseConfig.StorageModuleConfigured {
    64  		log.Println(util.DEVTRON, "not going to upload artifact as storage module not configured...")
    65  		return artifactUploaded, nil
    66  	}
    67  	isEmpty, err := IsDirEmpty(util.TmpArtifactLocation)
    68  	if err != nil {
    69  		log.Println(util.DEVTRON, "artifact empty check error ")
    70  		return artifactUploaded, err
    71  	} else if isEmpty {
    72  		log.Println(util.DEVTRON, "no artifact to upload")
    73  		return artifactUploaded, nil
    74  	}
    75  	log.Println(util.DEVTRON, "artifact to upload")
    76  	zipFile := "job-artifact.zip"
    77  	zipCmd := exec.Command("zip", "-r", zipFile, util.TmpArtifactLocation)
    78  	err = util.RunCommand(zipCmd)
    79  	if err != nil {
    80  		return artifactUploaded, err
    81  	}
    82  	log.Println(util.DEVTRON, " artifact upload to ", zipFile, artifactFileName)
    83  	err = UploadFileToCloud(cloudHelperBaseConfig, zipFile, artifactFileName)
    84  	if err != nil {
    85  		return artifactUploaded, err
    86  	}
    87  	artifactUploaded = true
    88  	return artifactUploaded, err
    89  }
    90  
    91  func IsDirEmpty(name string) (bool, error) {
    92  	if _, err := os.Stat(name); os.IsNotExist(err) {
    93  		return true, nil
    94  	}
    95  	f, err := os.Open(name)
    96  	if err != nil {
    97  		return false, err
    98  	}
    99  	defer f.Close()
   100  
   101  	// read in ONLY one file
   102  	_, err = f.Readdir(1)
   103  
   104  	// and if the file is EOF... well, the dir is empty.
   105  	if err == io.EOF {
   106  		return true, nil
   107  	}
   108  	return false, err
   109  }