1

我尝试将 AWS Code Deploy 与自动缩放组一起使用。过去我使用章鱼进行部署,我想迁移到 aws。

所以,我有一个包含多个步骤的八达通项目。第一步部署一个用于构建基础架构的 cloudformation 模板:vpc、子网、部署组、自动缩放组、ELB、S3 存储桶等......

其他步骤应该替换章鱼包的变量,并从第一步中检索 S3 存储桶名称以将其上传到 S3。

我怎样才能用章鱼做到这一点?

谢谢。

4

1 回答 1

2

这是我如何做到的:

  1. 在您的流程中添加一个步骤以部署包。我使用了一个“Jumpbox”来部署,它只需要去某个地方。
  2. 在部署包的步骤中单击 配置功能

  3. 选择 自定义部署脚本

  4. 将此脚本添加为部署脚本。替换变量后,它将上传到 AWS S3 存储桶。

    $sourcePath = $OctopusParameters["Octopus.Action.Package.InstallationDirectoryPath"]
    $destinationPath = $OctopusParameters["Your parameter to the full path where you want the Zip file stored"]
    $destinationPathRootDirectory = $OctopusParameters["Your parameter to the folder"]
    
    Write-Host "Cleaning up previous zip files"
    Remove-Item "$destinationPathRootDirectory\*.zip"
    
    Write-Host "Compressing $sourcePath to $destinationPath"
    Compress-Archive -Path "$sourcePath\*"  -DestinationPath $destinationPath -Force
    
    $params = @{}
    
    #Initialises the S3 Credentials based on the Access Key and Secret Key provided, so that we can invoke the APIs further down
    Set-AWSCredentials -AccessKey $AwsAccessKey -SecretKey $AwsAccessSecret -StoreAs S3Creds
    
    #Initialises the Default AWS Region based on the region provided
    Set-DefaultAWSRegion -Region $AwsRegion
    
    #Gets all relevant files and uploads them
    function Upload($item) 
    {
        #Gets all files and child folders within the given directory
        foreach ($i in Get-ChildItem $item) {
                #Inserts file to AWS
                Write-S3Object -ProfileName S3Creds -BucketName $S3BucketName -Key $($i.Name) -File $i.FullName @params
        }
    }
    
    Upload($destinationPath)
    
  5. 添加一个步骤以运行将执行部署的 AWS 命令​​行命令。

这是我在该步骤中的示例脚本:

    # All these parameters are parameters you set in the variables screen in Octopus Deploy

    Write-Host "ApplicationName $AwsApplicationName"
    Write-Host "DeploymentConfigName $AwsCodeDeployConfigName"
    Write-Host "CodeDeployName $AwsCodeDeployGroupName"
    Write-Host "BucketLocation $S3BucketName"
    Write-Host "KeyName $ZipFileName"

    # This is the AWS name of the location where you want to put the bucket, such as us-west-1
    Write-Host "BucketLocation $bucketLocation"

    $fullyQualifiedBucket = "$S3BucketName-$bucketLocation"
    Write-Host "Fully Qualified Bucket $fullyQualifiedBucket"

    $deployment = aws deploy create-deployment --application-name $AwsApplicationName --deployment-config-name $AwsCodeDeployConfigName --deployment-group-name $AwsCodeDeployGroupName --s3-location bucket=$S3BucketName,bundleType=zip,key=$TradingWebSiteZipFileName
    Write-Host "Deployment info $deployment"

它确实在 S3 存储桶中为每个环境创建了一个唯一的包。所以你必须考虑到这一点。这不是世界上最完美的解决方案,如果您遍历它并找到更好的解决方案,请告诉我!

于 2018-05-14T21:24:05.647 回答