-
Notifications
You must be signed in to change notification settings - Fork 435
feat: upload zip after deploy #7573
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
c80113e
feat: add flag to deploy command to upload zip
e2307fe
fix: improvements
denar90 98cd15f
fix: merge main
denar90 8e63ff7
fix: use filename from server
denar90 c41a581
fix: tests
denar90 707a287
fix: tests
denar90 b966c08
fix: list
denar90 68e6301
fix: tests
denar90 60f3621
fix: list
denar90 0d2e7ee
fix: test
denar90 50bc155
Merge branch 'main' into feat-upload-zip-after-deploy
kodiakhq[bot] 388d13e
Merge branch 'main' into feat-upload-zip-after-deploy
kodiakhq[bot] afe729e
Merge branch 'main' into feat-upload-zip-after-deploy
kodiakhq[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| import { execFile } from 'child_process' | ||
| import { readFile } from 'fs/promises' | ||
| import { join } from 'path' | ||
| import { promisify } from 'util' | ||
| import type { PathLike } from 'fs' | ||
| import { platform } from 'os' | ||
|
|
||
| import fetch from 'node-fetch' | ||
|
|
||
| import { log, warn } from '../command-helpers.js' | ||
| import { temporaryDirectory } from '../temporary-file.js' | ||
| import type { DeployEvent } from './status-cb.js' | ||
|
|
||
| const execFileAsync = promisify(execFile) | ||
|
|
||
| interface UploadSourceZipOptions { | ||
| sourceDir: string | ||
| uploadUrl: string | ||
| filename: string | ||
| statusCb?: (status: DeployEvent) => void | ||
| } | ||
|
|
||
| const DEFAULT_IGNORE_PATTERNS = [ | ||
| 'node_modules', | ||
| '.git', | ||
| '.netlify', | ||
| '.next', | ||
| 'dist', | ||
| 'build', | ||
| '.nuxt', | ||
| '.output', | ||
| '.vercel', | ||
| '__pycache__', | ||
| '.venv', | ||
| '.env', | ||
| '.DS_Store', | ||
| 'Thumbs.db', | ||
| '*.log', | ||
| '.nyc_output', | ||
| 'coverage', | ||
| '.cache', | ||
| '.tmp', | ||
| '.temp', | ||
| ] | ||
|
|
||
| const createSourceZip = async ({ | ||
| sourceDir, | ||
| filename, | ||
| statusCb, | ||
| }: { | ||
| sourceDir: string | ||
| filename: string | ||
| statusCb: (status: DeployEvent) => void | ||
| }) => { | ||
| // Check for Windows - this feature is not supported on Windows | ||
| if (platform() === 'win32') { | ||
| throw new Error('Source zip upload is not supported on Windows') | ||
| } | ||
|
|
||
| const tmpDir = temporaryDirectory() | ||
| const zipPath = join(tmpDir, filename) | ||
|
|
||
| statusCb({ | ||
| type: 'source-zip-upload', | ||
| msg: `Creating source zip...`, | ||
| phase: 'start', | ||
| }) | ||
|
|
||
| // Create exclusion list for zip command | ||
| const excludeArgs = DEFAULT_IGNORE_PATTERNS.flatMap((pattern) => ['-x', pattern]) | ||
|
|
||
| // Use system zip command to create the archive | ||
| await execFileAsync('zip', ['-r', zipPath, '.', ...excludeArgs], { | ||
| cwd: sourceDir, | ||
| maxBuffer: 1024 * 1024 * 100, // 100MB buffer | ||
| }) | ||
|
|
||
| return zipPath | ||
| } | ||
|
|
||
| const uploadZipToS3 = async ( | ||
| zipPath: string, | ||
| uploadUrl: string, | ||
| statusCb: (status: DeployEvent) => void, | ||
| ): Promise<void> => { | ||
| const zipBuffer = await readFile(zipPath) | ||
| const sizeMB = (zipBuffer.length / 1024 / 1024).toFixed(2) | ||
|
|
||
| statusCb({ | ||
| type: 'source-zip-upload', | ||
| msg: `Uploading source zip (${sizeMB} MB)...`, | ||
| phase: 'progress', | ||
| }) | ||
|
|
||
| const response = await fetch(uploadUrl, { | ||
| method: 'PUT', | ||
| body: zipBuffer, | ||
| headers: { | ||
| 'Content-Type': 'application/zip', | ||
| 'Content-Length': zipBuffer.length.toString(), | ||
| }, | ||
| }) | ||
|
|
||
| if (!response.ok) { | ||
| throw new Error(`Failed to upload zip: ${response.statusText}`) | ||
| } | ||
| } | ||
|
|
||
| export const uploadSourceZip = async ({ | ||
| sourceDir, | ||
| uploadUrl, | ||
| filename, | ||
| statusCb = () => {}, | ||
| }: UploadSourceZipOptions): Promise<void> => { | ||
| let zipPath: PathLike | undefined | ||
|
|
||
| try { | ||
| // Create zip from source directory | ||
| try { | ||
| zipPath = await createSourceZip({ sourceDir, filename, statusCb }) | ||
| } catch (error) { | ||
| const errorMsg = error instanceof Error ? error.message : String(error) | ||
| statusCb({ | ||
| type: 'source-zip-upload', | ||
| msg: `Failed to create source zip: ${errorMsg}`, | ||
| phase: 'error', | ||
| }) | ||
| warn(`Failed to create source zip: ${errorMsg}`) | ||
| throw error | ||
| } | ||
|
|
||
| // Upload to S3 | ||
| try { | ||
| await uploadZipToS3(zipPath, uploadUrl, statusCb) | ||
| } catch (error) { | ||
| const errorMsg = error instanceof Error ? error.message : String(error) | ||
| statusCb({ | ||
| type: 'source-zip-upload', | ||
| msg: `Failed to upload source zip: ${errorMsg}`, | ||
| phase: 'error', | ||
| }) | ||
| warn(`Failed to upload source zip: ${errorMsg}`) | ||
| throw error | ||
| } | ||
|
|
||
| statusCb({ | ||
| type: 'source-zip-upload', | ||
| msg: `Source zip uploaded successfully`, | ||
| phase: 'stop', | ||
| }) | ||
|
|
||
| log(`β Source code uploaded`) | ||
| } finally { | ||
| // Clean up temporary zip file | ||
| if (zipPath) { | ||
| try { | ||
| await import('fs/promises').then((fs) => fs.unlink(zipPath as unknown as PathLike)) | ||
| } catch { | ||
| // Ignore cleanup errors | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@denar90 I think it would be good to add something here that shows a warning and then quits for windows users if they are using this new flag, since I think they are not able to use the zip functionality
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good idea, I moved on command level and default falg to false