From 2f9453115b39cf8f67039d433b94d5d8ff692db3 Mon Sep 17 00:00:00 2001 From: Autumn Na Date: Fri, 22 Mar 2024 03:33:18 +0900 Subject: [PATCH] =?UTF-8?q?CSS-55=20[DevOps][=EC=8B=A0=EA=B7=9C]=20Google?= =?UTF-8?q?=20Drive=20=EC=97=85=EB=A1=9C=EB=93=9C=20Google=20Drive=20?= =?UTF-8?q?=EC=97=85=EB=A1=9C=EB=93=9C=20=EB=A1=9C=EC=A7=81=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Jenkins/build.ps1 | 80 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 78 insertions(+), 2 deletions(-) diff --git a/Jenkins/build.ps1 b/Jenkins/build.ps1 index c01bed3314..7a3f0f497b 100644 --- a/Jenkins/build.ps1 +++ b/Jenkins/build.ps1 @@ -1,8 +1,23 @@ -param([string]$Version="version.value.error") +param( + [string]$Version="version.value.error", + [string]$RefreshToken="refresh.token.error", + [string]$ClientID="client.id.error", + [string]$ClientSecret="client.secret.error", + [string]$FolderID="folder.id.error" +) + +[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 # Move to the build directory Set-Location "C:\\Work\\Career\\2_KMU\\CapstoneProject\\Builds" +$VersionPath = Resolve-Path($Version) + +# If the build already exists, delete it +if (Test-Path $VersionPath) { + Remove-Item -Recurse -Force $VersionPath +} + # Create a new folder for the build New-Item -ItemType Directory -Name $Version @@ -16,4 +31,65 @@ Start-Process -FilePath $RunUATPath -ArgumentList $UATOptions -NoNewWindow -Wait # Make a zip file of the build $ZipPath = $ArchivePath.ToString() + ".zip" -Compress-Archive -Path $ArchivePath -DestinationPath $ZipPath -Force \ No newline at end of file +Compress-Archive -Path $ArchivePath -DestinationPath $ZipPath -Force + +# Set the Google Auth parameters. Fill in your RefreshToken, ClientID, and ClientSecret +$Params = @{ + Uri = 'https://accounts.google.com/o/oauth2/token' + Body = @( + "refresh_token=$RefreshToken", # Replace $RefreshToken with your refresh token + "client_id=$ClientID", # Replace $ClientID with your client ID + "client_secret=$ClientSecret", # Replace $ClientSecret with your client secret + "grant_type=refresh_token" + ) -join '&' + Method = 'Post' + ContentType = 'application/x-www-form-urlencoded' +} +$AccessToken = (Invoke-RestMethod @Params).access_token + +# Change this to the file you want to upload +$SourceFile = $ZipPath + +# Get the source file contents and details, encode in base64 +$SourceItem = Get-Item $SourceFile +$SourceBase64 = [Convert]::ToBase64String([IO.File]::ReadAllBytes($SourceItem.FullName)) +$SourceMime = [System.Web.MimeMapping]::GetMimeMapping($SourceItem.FullName) + +# If uploading to a Team Drive, set this to 'true' +$SupportsTeamDrives = 'false' + +# Set the file metadata +$UploadMetadata = @{ + originalFilename = $sourceItem.Name + name = $sourceItem.Name + description = $sourceItem.VersionInfo.FileDescription + parents = @($FolderID) # Include to upload to a specific folder + #teamDriveId = ‘teamDriveId’ # Include to upload to a specific teamdrive +} + +# Set the upload body +$UploadBody = @" +--boundary +Content-Type: application/json; charset=UTF-8 + +$($UploadMetadata | ConvertTo-Json) +--boundary +Content-Transfer-Encoding: base64 +Content-Type: $SourceMime + +$SourceBase64 +--boundary-- +"@ + +# Set the upload headers +$UploadHeaders = @{ + "Authorization" = "Bearer $AccessToken" + "Content-Type" = 'multipart/related; boundary=boundary' + "Content-Length" = $UploadBody.Length +} + +# Perform the upload +$Response = Invoke-RestMethod -Uri "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&SupportsTeamDrives=$SupportsTeamDrives" -Method Post -Headers $UploadHeaders -Body $UploadBody + +# Print the response +$Response \ No newline at end of file