Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Amazon web services 詹金斯:检查AWS S3存储桶是否存在,或者创建它_Amazon Web Services_Jenkins_Amazon S3 - Fatal编程技术网

Amazon web services 詹金斯:检查AWS S3存储桶是否存在,或者创建它

Amazon web services 詹金斯:检查AWS S3存储桶是否存在,或者创建它,amazon-web-services,jenkins,amazon-s3,Amazon Web Services,Jenkins,Amazon S3,我试图使用一个Jenkins文件为一个项目创建一个AmazonS3 Bucket,但是我想首先检查Bucket是否存在,否则使用给定的参数创建Bucket 我在Jenkins文件的创建Bucket阶段使用以下命令实现此功能: script { if ("aws s3api bucket-exists --bucket ${params.BUCKET}") { // Check if the bucket exists echo 'Bucket already e

我试图使用一个Jenkins文件为一个项目创建一个AmazonS3 Bucket,但是我想首先检查Bucket是否存在,否则使用给定的参数创建Bucket

我在Jenkins文件的创建Bucket阶段使用以下命令实现此功能:

script {
  if ("aws s3api bucket-exists --bucket ${params.BUCKET}") { // Check if the bucket exists
     echo 'Bucket already exists'
  } 
  else ("aws s3api create-bucket --bucket ${params.BUCKET} --region ${params.REGION} --create-bucket-configuration LocationConstraint=${params.REGION}") { // Create the bucket if it does not exist
     echo 'Bucket created'
  }
}
这是我的詹金斯档案

pipeline {  
  agent any
  parameters {
    string(name: 'BUCKET', defaultValue: 's3-pipeline', description: 'Amazon S3 Bucket Name')
    string(name: 'PREFIX', defaultValue: 'my-website', description: 'Amazon S3 Bucket Application Directory')
    string(name: 'REGION', defaultValue: 'eu-west-1', description: 'Amazon S3 Bucket Region')
    string(name: 'BUILD', defaultValue: 'public/', description: 'Application Build Directory')
    string(name: 'INDEX', defaultValue: 'index.html', description: 'Application Index File')
  }  
  stages {  
    stage('Build project') {  
      steps {
        echo 'Running build project phase'
        sh 'npm install' // Install packages
        sh 'npm run clean' // Clean project 
        sh 'npm run build' // Build project 
        sh 'ls' // List project files
      }  
    //}
    stage('Create Bucket') {  
      steps {  
        echo 'Running create bucket phase'
        withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', accessKeyVariable: 'AWS_ACCESS_KEY_ID', credentialsId: 'AWSCredentials', secretKeyVariable: 'AWS_SECRET_ACCESS_KEY']]) { 
          script {
            if ("aws s3api bucket-exists --bucket ${params.BUCKET}") { // Check if the bucket exists
              echo 'Bucket already exists'
            } 
            else ("aws s3api create-bucket --bucket ${params.BUCKET} --region ${params.REGION} --create-bucket-configuration LocationConstraint=${params.REGION}") { // Create the bucket if it does not exist
              echo 'Bucket created'
            }
          }
        }  
      }   
    }  
    stage('Deploy') {  
      steps {  
        echo 'Running deploy phase'
        withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', accessKeyVariable: 'AWS_ACCESS_KEY_ID', credentialsId: 'AWSCredentials', secretKeyVariable: 'AWS_SECRET_ACCESS_KEY']]) { 
          sh 'aws s3 ls' // List AWS S3 buckets
          sh "aws s3 sync ${params.BUILD} s3://${params.BUCKET}/${params.PREFIX} --delete" // Sync project files with AWS S3 Bucket project path
        }  
      }   
    }
  }
  post { 
    success {  
      echo "Deployment to Amazon S3 suceeded for JOB - ${env.JOB_NAME} with BUILD NUMBER - ${env.BUILD_NUMBER}"
      echo "The Jenkins BUILD URL of this project is: ${env.BUILD_URL}" 
      echo "The Amazon S3 BUILD URL of this project is: https://${params.BUCKET}.s3-${params.REGION}.amazonaws.com/${params.PREFIX}/${params.INDEX}" 
      // mail to: team@example.com, subject: "Deployment to Amazon S3 suceeded for JOB - ${env.JOB_NAME} with BUILD NUMBER - ${env.BUILD_NUMBER}"
    }  
    failure {  
      echo "Deployment to Amazon S3 failed for JOB - ${env.JOB_NAME} with BUILD NUMBER - ${env.BUILD_NUMBER}"
      echo "The Jenkins BUILD URL of this project is: ${env.BUILD_URL}"
      // mail to: team@example.com, subject: "Deployment to Amazon S3 failed for JOB - ${env.JOB_NAME} with BUILD NUMBER - ${env.BUILD_NUMBER}"
    }  
  }  
}
然而,我又意识到这个桶已经存在了,即使它不在那里

如何检查bucket是否存在,然后根据返回值进行决策,如果bucket不存在,则创建bucket

方法文档并没有为我提供这一点,相反,他们只有以下内容:

使用head bucket进行轮询时,等待收到200响应。信息技术 将每5秒轮询一次,直到达到成功状态。 这将在20次检查失败后退出,返回代码为255


我似乎不明白该怎么做。我们将非常感谢任何形式的帮助。谢谢。

以下代码是Groovy代码:

script {
  if ("aws s3api head-bucket --bucket ${params.BUCKET}") { // Check if the bucket exists
     echo 'Bucket already exists'
  } 
  else ("aws s3api create-bucket --bucket ${params.BUCKET} --region ${params.REGION} --create-bucket-configuration LocationConstraint=${params.REGION}") { // Create the bucket if it does not exist
     echo 'Bucket created'
  }
}
在Groovy中,条件
if(“某个随机字符串”){…}
将始终被求值为
true
,除非给定的字符串被求值为falsy(有关详细信息,请参阅)

更重要的是,给定的命令没有执行!它只是一个计算为
true
的字符串

如果您想在Jenkins管道中执行shell命令,您需要使用步骤,而这不是您在这里要做的

你可以试试这个:

script {
  def status = sh(script: "aws s3api head-bucket --bucket ${params.BUCKET}", returnStatus: true)
  if (status == 0) {
    echo 'Bucket already exists'
  } else {
    echo 'Bucket created'
  }
}
或者,更具体地说:

script {
  def status = sh(script: "aws s3api head-bucket --bucket ${params.BUCKET}", returnStatus: true)
  if (status != 0) {
    sh "aws s3api create-bucket --bucket ${params.BUCKET} --region ${params.REGION} --create-bucket-configuration LocationConstraint=${params.REGION}"
  }
}
要实现try-and-catch,可以执行以下操作:

stage('Create Bucket') {  
  steps {  
    echo 'Running create bucket phase'
    withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', accessKeyVariable: 'AWS_ACCESS_KEY_ID', credentialsId: 'AWSCredentials', secretKeyVariable: 'AWS_SECRET_ACCESS_KEY']]) { 
      script {
        def status = sh(script: "aws s3api head-bucket --bucket ${params.BUCKET}", returnStatus: true)
        def create = sh(script: "aws s3api create-bucket --bucket ${params.BUCKET} --region ${params.REGION} --create-bucket-configuration LocationConstraint=${params.REGION}", returnStatus: true)
        try {
          if (status == 0) { // Check if the bucket exists
            echo 'Bucket already exists'
          } 
          else create() {  // Create the bucket if it does not exist
            echo 'Bucket created'
          }
        } catch (err) {
          echo "Caught: ${err}"
          currentBuild.result = 'SUCCESS'
        }
      }
    }  
  }   
}
就这些


我希望这有帮助

哇。谢谢你。现在让我试试。你能补充一下,如果bucket不存在,我如何调用一个方法来创建它吗。我这样做了,但它不起作用:
def create=sh(脚本:“aws s3api创建bucket--bucket${params.bucket}--region${params.region}--create bucket configuration LocationConstraint=${params.region}”,returnStatus:true)
。然后调用create方法
else(create){//create bucket(如果bucket不存在)
。但是它抛出了一个错误。
else(create)
不是有效语法。您的意思是
else{create()}
?就像你的问题一样,从一个
回音开始,看看它是否有效。好的。现在让我试试。我编辑了我的答案,以便更具体地针对你的用例。