Skip to content

Commit

Permalink
Deploy the snapshot of generic files under 1GB file size (#207)
Browse files Browse the repository at this point in the history
* Deploy the snapshot of generic files under 1GB file size

* Code cleanup

* Log snapshot deployment

* Update uploaders/fileuploader.go

Co-authored-by: Olivér Falvai <[email protected]>

---------

Co-authored-by: Olivér Falvai <[email protected]>
  • Loading branch information
godrei and ofalvai authored Aug 2, 2024
1 parent d3b1275 commit 2242bfb
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 5 deletions.
2 changes: 0 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,8 +544,6 @@ func deploySingleItem(item deployment.DeployableItem, config Config, androidArti

return uploaders.DeployXcarchive(item, config.BuildURL, config.APIToken)
default:
logger.Printf("Deploying file: %s", pth)

return uploaders.DeployFile(item, config.BuildURL, config.APIToken)
}
}
Expand Down
75 changes: 72 additions & 3 deletions uploaders/fileuploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,54 @@ package uploaders

import (
"fmt"
"io"
"os"
"path/filepath"

"github.com/bitrise-io/go-utils/log"
"github.com/bitrise-io/go-utils/pathutil"
"github.com/bitrise-steplib/steps-deploy-to-bitrise-io/deployment"
)

const snapshotFileSizeLimitInBytes = 1024 * 1024 * 1024

// DeployFile ...
func DeployFile(item deployment.DeployableItem, buildURL, token string) (ArtifactURLs, error) {
pth := item.Path
fileSize, err := fileSizeInBytes(item.Path)
if err != nil {
return ArtifactURLs{}, fmt.Errorf("get file size: %s", err)
return ArtifactURLs{}, fmt.Errorf("get file size: %w", err)
}

// TODO: This is a workaround to avoid uploading a file that is being modified during the upload process,
// which can cause an issue like: request body larger than specified content length at file upload.
deploySnapshot := false
if fileSize <= snapshotFileSizeLimitInBytes {
snapshotPth, err := createSnapshot(pth)
if err != nil {
log.Warnf("failed to create snapshot of %s: %s", pth, err)
} else {
defer func() {
if err := os.Remove(snapshotPth); err != nil {
log.Warnf("Failed to remove snapshot file: %s", err)
}
}()
pth = snapshotPth
deploySnapshot = true
}
}

if deploySnapshot {
log.Printf("Deploying snapshot of original file: %s", pth)
} else {
log.Printf("Deploying file: %s", pth)
}
artifact := ArtifactArgs {
Path: item.Path,

artifact := ArtifactArgs{
Path: pth,
FileSize: fileSize,
}

uploadURL, artifactID, err := createArtifact(buildURL, token, artifact, "file", "")
if err != nil {
return ArtifactURLs{}, fmt.Errorf("create file artifact: %s %w", artifact.Path, err)
Expand All @@ -31,3 +65,38 @@ func DeployFile(item deployment.DeployableItem, buildURL, token string) (Artifac
}
return artifactURLs, nil
}

// createSnapshot copies a file to a temporary directory with the same file name.
func createSnapshot(originalPath string) (string, error) {
originalFile, err := os.Open(originalPath)
if err != nil {
return "", fmt.Errorf("failed to open original file: %w", err)
}
defer func() {
if err := originalFile.Close(); err != nil {
log.Warnf("Failed to close original file: %s", err)
}
}()

tmpDir, err := pathutil.NormalizedOSTempDirPath("snapshot")
if err != nil {
return "", fmt.Errorf("failed to create temp directory: %w", err)
}

tmpFilePath := filepath.Join(tmpDir, filepath.Base(originalPath))
tmpFile, err := os.Create(tmpFilePath)
if err != nil {
return "", fmt.Errorf("failed to create temp file: %w", err)
}
defer func() {
if err := tmpFile.Close(); err != nil {
log.Warnf("Failed to close temp file: %s", err)
}
}()

if _, err := io.Copy(tmpFile, originalFile); err != nil {
return "", fmt.Errorf("failed to copy contents: %w", err)
}

return tmpFilePath, nil
}

0 comments on commit 2242bfb

Please sign in to comment.