如何使用 Bitbucket 管道在 AWS ElasticBeanstalk 上部署基于 Java Maven 的应用程序?
How to deploy Java Maven based application on AWS ElasticBeanstalk using Bitbucket pipelines?
如何使用 Bitbucket 管道在 AWS ElasticBeanstalk 上部署 Java 基于 Maven 的应用程序?我找到了 there 示例如何使用 python 脚本来完成。我根据需要修改 yml 和 py 脚本。所有步骤都正常,机器人在最后一个(执行 python 脚本它抛出错误。出了什么问题?谢谢。
YML 文件
image: openkbs/jre-mvn-py3
pipelines:
branches:
master:
- step:
caches:
- maven
- node
script:
- mvn clean install
- apt-get update
- apt-get -y install python-pip
- pip install boto3
- export AWS_SECRET_ACCESS_KEY=KEY
- export AWS_ACCESS_KEY_ID=KEY
- export AWS_DEFAULT_REGION=REGION
- python beanstalk_deploy.py
Python 脚本
Source
Traceback (most recent call last):
File "beanstalk_deploy.py", line 107, in
main()
File "beanstalk_deploy.py", line 99, in main
if not create_new_version():
File "beanstalk_deploy.py", line 56, in create_new_version
Process=True
File "/usr/local/lib/python2.7/dist-packages/botocore/client.py", line 314, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/usr/local/lib/python2.7/dist-packages/botocore/client.py", line 586, in _make_api_call
api_params, operation_model, context=request_context)
File "/usr/local/lib/python2.7/dist-packages/botocore/client.py", line 641, in _convert_to_request_dict
api_params, operation_model)
File "/usr/local/lib/python2.7/dist-packages/botocore/validate.py", line 291, in serialize_to_request
raise ParamValidationError(report=report.generate_report())
botocore.exceptions.ParamValidationError: Parameter validation failed:
Invalid type for parameter SourceBundle.S3Bucket, value: None, type: , valid types:
已经找到解决办法。首先,我们创建一个包含所有工具的新图像。
修改yml管道文件
image: hardtyz/jre-mvn-py3
pipelines:
branches:
master:
- step:
caches:
- maven
- node
script:
- mvn clean install
- export AWS_SECRET_ACCESS_KEY=AWS_SECRET_ACCESS_KEY
- export AWS_ACCESS_KEY_ID=AWS_ACCESS_KEY_ID
- export AWS_DEFAULT_REGION=AWS_DEFAULT_REGION
- python3 beanstalk_deploy.py
并修改python脚本。只需使用您的值更新脚本头部的变量。
from __future__ import print_function
import sys
from time import strftime, sleep
import boto3
from botocore.exceptions import ClientError
VERSION_LABEL = strftime("%Y%m%d%H%M%S")
BUCKET_KEY = VERSION_LABEL + '-bitbucket_builds.jar'
BUCKET_NAME = 'your_bucket_name'
APP_NAME='AppName'
ENV_NAME='AppName-env'
ARTIFACT_PATH='./core/target/core-0.0.1-SNAPSHOT.jar'
def upload_to_s3(artifact):
"""
Uploads an artifact 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(artifact, 'rb'),
Bucket=BUCKET_NAME,
Key=BUCKET_KEY
)
except ClientError as err:
print("Failed to upload artifact to S3.\n" + str(err))
return False
except IOError as err:
print("Failed to access artifact in this directory.\n" + str(err))
return False
return True
def deploy_new_version():
"""
Deploy a new version to AWS Elastic Beanstalk
"""
try:
client = boto3.client('elasticbeanstalk')
except ClientError as err:
print("Failed to create boto3 client.\n" + str(err))
return False
try:
response = client.update_environment(
ApplicationName=APP_NAME,
EnvironmentName=ENV_NAME,
VersionLabel=VERSION_LABEL,
)
except ClientError as err:
print("Failed to update environment.\n" + str(err))
return False
print(response)
return True
def main():
if not upload_to_s3(ARTIFACT_PATH):
sys.exit(1)
if not create_new_version():
sys.exit(1)
# Wait for the new version to be consistent before deploying
sleep(5)
if not deploy_new_version():
sys.exit(1)
if __name__ == "__main__":
main()
如何使用 Bitbucket 管道在 AWS ElasticBeanstalk 上部署 Java 基于 Maven 的应用程序?我找到了 there 示例如何使用 python 脚本来完成。我根据需要修改 yml 和 py 脚本。所有步骤都正常,机器人在最后一个(执行 python 脚本它抛出错误。出了什么问题?谢谢。
YML 文件
image: openkbs/jre-mvn-py3
pipelines:
branches:
master:
- step:
caches:
- maven
- node
script:
- mvn clean install
- apt-get update
- apt-get -y install python-pip
- pip install boto3
- export AWS_SECRET_ACCESS_KEY=KEY
- export AWS_ACCESS_KEY_ID=KEY
- export AWS_DEFAULT_REGION=REGION
- python beanstalk_deploy.py
Python 脚本 Source
Traceback (most recent call last): File "beanstalk_deploy.py", line 107, in main()
File "beanstalk_deploy.py", line 99, in main if not create_new_version():
File "beanstalk_deploy.py", line 56, in create_new_version Process=True
File "/usr/local/lib/python2.7/dist-packages/botocore/client.py", line 314, in _api_call return self._make_api_call(operation_name, kwargs)
File "/usr/local/lib/python2.7/dist-packages/botocore/client.py", line 586, in _make_api_call api_params, operation_model, context=request_context)
File "/usr/local/lib/python2.7/dist-packages/botocore/client.py", line 641, in _convert_to_request_dict api_params, operation_model)
File "/usr/local/lib/python2.7/dist-packages/botocore/validate.py", line 291, in serialize_to_request raise ParamValidationError(report=report.generate_report()) botocore.exceptions.ParamValidationError: Parameter validation failed:
Invalid type for parameter SourceBundle.S3Bucket, value: None, type: , valid types:
已经找到解决办法。首先,我们创建一个包含所有工具的新图像。
修改yml管道文件
image: hardtyz/jre-mvn-py3
pipelines:
branches:
master:
- step:
caches:
- maven
- node
script:
- mvn clean install
- export AWS_SECRET_ACCESS_KEY=AWS_SECRET_ACCESS_KEY
- export AWS_ACCESS_KEY_ID=AWS_ACCESS_KEY_ID
- export AWS_DEFAULT_REGION=AWS_DEFAULT_REGION
- python3 beanstalk_deploy.py
并修改python脚本。只需使用您的值更新脚本头部的变量。
from __future__ import print_function
import sys
from time import strftime, sleep
import boto3
from botocore.exceptions import ClientError
VERSION_LABEL = strftime("%Y%m%d%H%M%S")
BUCKET_KEY = VERSION_LABEL + '-bitbucket_builds.jar'
BUCKET_NAME = 'your_bucket_name'
APP_NAME='AppName'
ENV_NAME='AppName-env'
ARTIFACT_PATH='./core/target/core-0.0.1-SNAPSHOT.jar'
def upload_to_s3(artifact):
"""
Uploads an artifact 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(artifact, 'rb'),
Bucket=BUCKET_NAME,
Key=BUCKET_KEY
)
except ClientError as err:
print("Failed to upload artifact to S3.\n" + str(err))
return False
except IOError as err:
print("Failed to access artifact in this directory.\n" + str(err))
return False
return True
def deploy_new_version():
"""
Deploy a new version to AWS Elastic Beanstalk
"""
try:
client = boto3.client('elasticbeanstalk')
except ClientError as err:
print("Failed to create boto3 client.\n" + str(err))
return False
try:
response = client.update_environment(
ApplicationName=APP_NAME,
EnvironmentName=ENV_NAME,
VersionLabel=VERSION_LABEL,
)
except ClientError as err:
print("Failed to update environment.\n" + str(err))
return False
print(response)
return True
def main():
if not upload_to_s3(ARTIFACT_PATH):
sys.exit(1)
if not create_new_version():
sys.exit(1)
# Wait for the new version to be consistent before deploying
sleep(5)
if not deploy_new_version():
sys.exit(1)
if __name__ == "__main__":
main()