Bitbucket Pipelineを使用して、Bitbucketリポジトリ全体をS3にアップロードします

スコットデッカー

BitbucketsPipelineを使用しています。リポジトリのコンテンツ全体(非常に小さい)をS3にプッシュしたいと思います。私はそれを圧縮し、S3にプッシュしてから解凍する必要はありません。Bitbucketリポジトリの既存のファイル/フォルダー構造を取得してS3にプッシュしたいだけです。

これを実現するには、yamlファイルと.pyファイルはどのように表示されますか?

現在のyamlファイルは次のとおりです。

image: python:3.5.1

pipelines:
  branches:
    master:
      - step:
          script:
            # - apt-get update # required to install zip
            # - apt-get install -y zip # required if you want to zip repository objects
            - pip install boto3==1.3.0 # required for s3_upload.py
            # the first argument is the name of the existing S3 bucket to upload the artefact to
            # the second argument is the artefact to be uploaded
            # the third argument is the the bucket key
            # html files
            - python s3_upload.py my-bucket-name html/index_template.html html/index_template.html # run the deployment script
            # Example command line parameters. Replace with your values
            #- python s3_upload.py bb-s3-upload SampleApp_Linux.zip SampleApp_Linux # run the deployment script

そしてこれが私の現在のPythonです:

from __future__ import print_function
import os
import sys
import argparse
import boto3
from botocore.exceptions import ClientError

def upload_to_s3(bucket, artefact, bucket_key):
    """
    Uploads an artefact to Amazon S3
    """
    try:
        client = boto3.client('s3')
    except ClientError as err:
        print("Failed to create boto3 client.\n" + str(err))
        return False
    try:
        client.put_object(
            Body=open(artefact, 'rb'),
            Bucket=bucket,
            Key=bucket_key
        )
    except ClientError as err:
        print("Failed to upload artefact to S3.\n" + str(err))
        return False
    except IOError as err:
        print("Failed to access artefact in this directory.\n" + str(err))
        return False
    return True


def main():

    parser = argparse.ArgumentParser()
    parser.add_argument("bucket", help="Name of the existing S3 bucket")
    parser.add_argument("artefact", help="Name of the artefact to be uploaded to S3")
    parser.add_argument("bucket_key", help="Name of the S3 Bucket key")
    args = parser.parse_args()

    if not upload_to_s3(args.bucket, args.artefact, args.bucket_key):
        sys.exit(1)

if __name__ == "__main__":
    main()

これには、別のコマンドとしてyamlファイルのリポジトリ内のすべてのファイルを一覧表示する必要があります。すべてを取得してS3にアップロードしたいだけです。

スコットデッカー

自分で考え出した。これがPythonファイル「s3_upload.py」です

from __future__ import print_function
import os
import sys
import argparse
import boto3
#import zipfile
from botocore.exceptions import ClientError

def upload_to_s3(bucket, artefact, is_folder, bucket_key):
    try:
        client = boto3.client('s3')
    except ClientError as err:
        print("Failed to create boto3 client.\n" + str(err))
        return False
    if is_folder == 'true':
        for root, dirs, files in os.walk(artefact, topdown=False):
            print('Walking it')
            for file in files:
                #add a check like this if you just want certain file types uploaded
                #if file.endswith('.js'):
                try:
                    print(file)
                    client.upload_file(os.path.join(root, file), bucket, os.path.join(root, file))
                except ClientError as err:
                    print("Failed to upload artefact to S3.\n" + str(err))
                    return False
                except IOError as err:
                    print("Failed to access artefact in this directory.\n" + str(err))
                    return False
                #else:
                #    print('Skipping file:' + file)
    else:
        print('Uploading file ' + artefact)
        client.upload_file(artefact, bucket, bucket_key)
    return True


def main():

    parser = argparse.ArgumentParser()
    parser.add_argument("bucket", help="Name of the existing S3 bucket")
    parser.add_argument("artefact", help="Name of the artefact to be uploaded to S3")
    parser.add_argument("is_folder", help="True if its the name of a folder")
    parser.add_argument("bucket_key", help="Name of file in bucket")
    args = parser.parse_args()

    if not upload_to_s3(args.bucket, args.artefact, args.is_folder, args.bucket_key):
        sys.exit(1)

if __name__ == "__main__":
    main()

そしてここにそれらはbitbucket-pipelines.ymlファイルです:

---
image: python:3.5.1

pipelines:
  branches:
    dev:
      - step:
          script:
            - pip install boto3==1.4.1 # required for s3_upload.py
            - pip install requests
            # the first argument is the name of the existing S3 bucket to upload the artefact to
            # the second argument is the artefact to be uploaded
            # the third argument is if the artefact is a folder
            # the fourth argument is the bucket_key to use
            - python s3_emptyBucket.py dev-slz-processor-repo
            - python s3_upload.py dev-slz-processor-repo lambda true lambda
            - python s3_upload.py dev-slz-processor-repo node_modules true node_modules
            - python s3_upload.py dev-slz-processor-repo config.dev.json false config.json
    stage:
      - step:
          script:
            - pip install boto3==1.3.0 # required for s3_upload.py
            - python s3_emptyBucket.py staging-slz-processor-repo
            - python s3_upload.py staging-slz-processor-repo lambda true lambda
            - python s3_upload.py staging-slz-processor-repo node_modules true node_modules
            - python s3_upload.py staging-slz-processor-repo config.staging.json false config.json
    master:
      - step:
          script:
            - pip install boto3==1.3.0 # required for s3_upload.py
            - python s3_emptyBucket.py prod-slz-processor-repo
            - python s3_upload.py prod-slz-processor-repo lambda true lambda
            - python s3_upload.py prod-slz-processor-repo node_modules true node_modules
            - python s3_upload.py prod-slz-processor-repo config.prod.json false config.json

devブランチの例として、「lambda」フォルダー内のすべてを取得し、そのフォルダーの構造全体をウォークし、見つかったアイテムごとに、それをdev-slz-processor-repoバケットにアップロードします。

最後に、新しいオブジェクトをアップロードする前にバケットからすべてのオブジェクトを削除するための、少し便利な関数「s3_emptyBucket」を次に示します。

from __future__ import print_function
import os
import sys
import argparse
import boto3
#import zipfile
from botocore.exceptions import ClientError

def empty_bucket(bucket):
    try:
        resource = boto3.resource('s3')
    except ClientError as err:
        print("Failed to create boto3 resource.\n" + str(err))
        return False
    print("Removing all objects from bucket: " + bucket)
    resource.Bucket(bucket).objects.delete()
    return True


def main():

    parser = argparse.ArgumentParser()
    parser.add_argument("bucket", help="Name of the existing S3 bucket to empty")
    args = parser.parse_args()

    if not empty_bucket(args.bucket):
        sys.exit(1)

if __name__ == "__main__":
    main()

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

APIを使用してBitBucketチームリポジトリを作成します

分類Dev

capistranoを使用してRailsアプリをデプロイするためのbitbucketリポジトリの問題

分類Dev

capistranoを使用してRailsアプリをデプロイするためのbitbucketリポジトリの問題

分類Dev

最初にbitbucketにリポジトリを作成せずに、SourceTreeを使用してローカルリポジトリをBitbucketにプッシュするにはどうすればよいですか?

分類Dev

IntelliJでリポジトリとしてBitbucketを使用する方法

分類Dev

リポジトリを作成し、サーバー/ cpanelからbitbucketにコードをプッシュする方法

分類Dev

CodeDeployを使用してリポジトリをbitbucketでAWSにデプロイする方法は?

分類Dev

jqを使用して、プロジェクトからすべてのBitBucketリポジトリのクローンを作成します

分類Dev

ローカルリポジトリをbitbucketにプッシュする方法は?

分類Dev

bitbucketサーバーはリポジトリをどこに保存しますか?

分類Dev

Bitbucketはリモートリポジトリから最新のコードを自動プルします

分類Dev

Ansibleを使用してBitbucketからgitリポジトリのクローンを作成する-パスワードを2、3回要求された

分類Dev

既存のBitbucketリポジトリを使用した継続的デプロイBluemix

分類Dev

エンタープライズアプリケーションプロジェクトをコミットしてBitBucketにプッシュする

分類Dev

C#ファイルをAzure App ServiceからBitbucketリポジトリにプッシュしますか?

分類Dev

BitBucket-リポジトリとサブモジュールを作成します

分類Dev

Bitbucketパイプライン。NodeJSスクリプトでENVVARSを使用して、S3Deployにデプロイします。

分類Dev

非標準のSVNリポジトリをBitBucketに移動します

分類Dev

curl / RESTとトークンを使用して、SSH公開鍵をbitbucketクラウドにアップロードします

分類Dev

Bitbucketプルリクエストを使用したGitフロー

分類Dev

drools .drlファイルをbitbucketリポジトリに保存し、実行時にSpringBootを使用してそれらにアクセスできますか?

分類Dev

Spring Cloud Configは、sshキーを使用してプライベートbitbucketリポジトリのクローンを作成できません

分類Dev

Spring Cloud Configは、sshキーを使用してプライベートbitbucketリポジトリのクローンを作成できません

分類Dev

AWS CodePipeLineがBitBucketアカウントのすべてのリポジトリを取得していません

分類Dev

APIを使用してすべての課題をBitbucketリポジトリに取得する方法

分類Dev

チームメイトがGitを使用して共有Bitbucketリポジトリにコミットをプッシュできない

分類Dev

コンポーザーを介してプライベートBitbucketリポジトリからパッケージをインポートする

分類Dev

GitHubとBitBucketは、ホストしているgitリポジトリをどのように解析しますか?

分類Dev

AzureWebアプリからBitbucketデプロイメントを削除します

Related 関連記事

  1. 1

    APIを使用してBitBucketチームリポジトリを作成します

  2. 2

    capistranoを使用してRailsアプリをデプロイするためのbitbucketリポジトリの問題

  3. 3

    capistranoを使用してRailsアプリをデプロイするためのbitbucketリポジトリの問題

  4. 4

    最初にbitbucketにリポジトリを作成せずに、SourceTreeを使用してローカルリポジトリをBitbucketにプッシュするにはどうすればよいですか?

  5. 5

    IntelliJでリポジトリとしてBitbucketを使用する方法

  6. 6

    リポジトリを作成し、サーバー/ cpanelからbitbucketにコードをプッシュする方法

  7. 7

    CodeDeployを使用してリポジトリをbitbucketでAWSにデプロイする方法は?

  8. 8

    jqを使用して、プロジェクトからすべてのBitBucketリポジトリのクローンを作成します

  9. 9

    ローカルリポジトリをbitbucketにプッシュする方法は?

  10. 10

    bitbucketサーバーはリポジトリをどこに保存しますか?

  11. 11

    Bitbucketはリモートリポジトリから最新のコードを自動プルします

  12. 12

    Ansibleを使用してBitbucketからgitリポジトリのクローンを作成する-パスワードを2、3回要求された

  13. 13

    既存のBitbucketリポジトリを使用した継続的デプロイBluemix

  14. 14

    エンタープライズアプリケーションプロジェクトをコミットしてBitBucketにプッシュする

  15. 15

    C#ファイルをAzure App ServiceからBitbucketリポジトリにプッシュしますか?

  16. 16

    BitBucket-リポジトリとサブモジュールを作成します

  17. 17

    Bitbucketパイプライン。NodeJSスクリプトでENVVARSを使用して、S3Deployにデプロイします。

  18. 18

    非標準のSVNリポジトリをBitBucketに移動します

  19. 19

    curl / RESTとトークンを使用して、SSH公開鍵をbitbucketクラウドにアップロードします

  20. 20

    Bitbucketプルリクエストを使用したGitフロー

  21. 21

    drools .drlファイルをbitbucketリポジトリに保存し、実行時にSpringBootを使用してそれらにアクセスできますか?

  22. 22

    Spring Cloud Configは、sshキーを使用してプライベートbitbucketリポジトリのクローンを作成できません

  23. 23

    Spring Cloud Configは、sshキーを使用してプライベートbitbucketリポジトリのクローンを作成できません

  24. 24

    AWS CodePipeLineがBitBucketアカウントのすべてのリポジトリを取得していません

  25. 25

    APIを使用してすべての課題をBitbucketリポジトリに取得する方法

  26. 26

    チームメイトがGitを使用して共有Bitbucketリポジトリにコミットをプッシュできない

  27. 27

    コンポーザーを介してプライベートBitbucketリポジトリからパッケージをインポートする

  28. 28

    GitHubとBitBucketは、ホストしているgitリポジトリをどのように解析しますか?

  29. 29

    AzureWebアプリからBitbucketデプロイメントを削除します

ホットタグ

アーカイブ