Jenkins 管道 - 检查 ECS 服务是否存在
Jenkins pipeline - check if an ECS service exist
我在 Jenkins 管道中使用 AWS ECS CLI 来自动化我的 CICD。我想做的是,如果服务尚不存在,我想根据任务定义创建服务,如果服务已经存在,我只想更新它。这是 create-service
cli 命令:
aws ecs create-service \
--cluster $stg_cluster \
--task-definition $task_def \
--service-name $ecs_service \
--desired-count 1 \
--launch-type EC2 \
--scheduling-strategy REPLICA \
--load-balancers \"targetGroupArn=$target_group_arn,containerName=$container_name,containerPort=80\" \
--deployment-configuration \"maximumPercent=200,minimumHealthyPercent=100\"
第一次运行正常,但在后续部署中会因为这个错误而失败:
An error occurred (InvalidParameterException) when calling the CreateService operation: Creation of service was not idempotent.
我想我必须改用命令 update-service
但不确定如何编写 ECS CLI 命令来检查 ECS 服务是否已经存在。我能想到的一种方法是我可以检查 create-service
cli 命令返回的代码,看看它是否等于 0,但同样不确定如何从管道中检索它。感谢您的帮助。
首先您可以 运行 aws ecs describe-services
检查该服务是否已经存在并且状态为 ACTIVE
。如果它处于活动状态,那么您可以 运行 aws ecs update-service
更新现有服务。如果不是 ACTIVE
那么您可以 运行 aws ecs create-service
创建服务。
这是一个示例代码,您可以使用它来检查服务是否已创建并处于活动状态:
aws ecs describe-services --cluster CLUSTER_NAME --services SERVIVE_NAME | jq --raw-output 'select(.services[].status != null ) | .services[].status'
然后使用 if 条件 运行 update-service
或 create-service
.
这是我的代码,以防有人感兴趣。
#Jenkinsfile 管道
stage('Deploy')
{
steps {
....
script {
int count = sh(script: """
aws ecs describe-services --cluster $ecs_cluster --services $ecs_service | jq '.services | length'
""",
returnStdout: true).trim()
echo "service count: $count"
if (count > 0) {
//ECS service exists: update
echo "Updating ECS service $ecs_service..."
sh(script: """
aws ecs update-service \
--cluster $ecs_cluster \
--service $ecs_service \
--task-definition $task_def \
""")
}
else {
//ECS service does not exist: create new
echo "Creating new ECS service $ecs_service..."
sh(script: """
aws ecs create-service \
--cluster $ecs_cluster \
--task-definition $task_def \
--service-name $ecs_service \
--desired-count 1 \
--launch-type EC2 \
--scheduling-strategy REPLICA \
--load-balancers \"targetGroupArn=${target_group_arn},containerName=$app_name,containerPort=80\" \
--deployment-configuration \"maximumPercent=200,minimumHealthyPercent=100\"
""")
}
}
}
}
在 aws cli 中使用查询参数:
#!/bin/bash
CLUSTER="test-cluster-name"
SERVICE="test-service-name"
echo "check ECS service exists"
status=$(aws ecs describe-services --cluster ${CLUSTER} --services ${SERVICE} --query 'failures[0].reason' --output text)
if [[ "${status}" == "MISSING" ]]; then
echo "ecs service ${SERVICE} missing in ${CLUSTER}"
exit 1
fi
在詹金斯管道中相同:
stage('Deploy')
{
steps {
....
script {
string status = sh(script: """
aws ecs describe-services --cluster $ecs_cluster --services $ecs_service --query 'failures[0].reason' --output text
""",
returnStdout: true).trim()
echo "service status: $status"
if (status == 'MISSING') {
echo "ecs service: $ecs_service does not exists in $ecs_cluster"
// put create service logic
}
}}}
我在 Jenkins 管道中使用 AWS ECS CLI 来自动化我的 CICD。我想做的是,如果服务尚不存在,我想根据任务定义创建服务,如果服务已经存在,我只想更新它。这是 create-service
cli 命令:
aws ecs create-service \
--cluster $stg_cluster \
--task-definition $task_def \
--service-name $ecs_service \
--desired-count 1 \
--launch-type EC2 \
--scheduling-strategy REPLICA \
--load-balancers \"targetGroupArn=$target_group_arn,containerName=$container_name,containerPort=80\" \
--deployment-configuration \"maximumPercent=200,minimumHealthyPercent=100\"
第一次运行正常,但在后续部署中会因为这个错误而失败:
An error occurred (InvalidParameterException) when calling the CreateService operation: Creation of service was not idempotent.
我想我必须改用命令 update-service
但不确定如何编写 ECS CLI 命令来检查 ECS 服务是否已经存在。我能想到的一种方法是我可以检查 create-service
cli 命令返回的代码,看看它是否等于 0,但同样不确定如何从管道中检索它。感谢您的帮助。
首先您可以 运行 aws ecs describe-services
检查该服务是否已经存在并且状态为 ACTIVE
。如果它处于活动状态,那么您可以 运行 aws ecs update-service
更新现有服务。如果不是 ACTIVE
那么您可以 运行 aws ecs create-service
创建服务。
这是一个示例代码,您可以使用它来检查服务是否已创建并处于活动状态:
aws ecs describe-services --cluster CLUSTER_NAME --services SERVIVE_NAME | jq --raw-output 'select(.services[].status != null ) | .services[].status'
然后使用 if 条件 运行 update-service
或 create-service
.
这是我的代码,以防有人感兴趣。 #Jenkinsfile 管道
stage('Deploy')
{
steps {
....
script {
int count = sh(script: """
aws ecs describe-services --cluster $ecs_cluster --services $ecs_service | jq '.services | length'
""",
returnStdout: true).trim()
echo "service count: $count"
if (count > 0) {
//ECS service exists: update
echo "Updating ECS service $ecs_service..."
sh(script: """
aws ecs update-service \
--cluster $ecs_cluster \
--service $ecs_service \
--task-definition $task_def \
""")
}
else {
//ECS service does not exist: create new
echo "Creating new ECS service $ecs_service..."
sh(script: """
aws ecs create-service \
--cluster $ecs_cluster \
--task-definition $task_def \
--service-name $ecs_service \
--desired-count 1 \
--launch-type EC2 \
--scheduling-strategy REPLICA \
--load-balancers \"targetGroupArn=${target_group_arn},containerName=$app_name,containerPort=80\" \
--deployment-configuration \"maximumPercent=200,minimumHealthyPercent=100\"
""")
}
}
}
}
在 aws cli 中使用查询参数:
#!/bin/bash
CLUSTER="test-cluster-name"
SERVICE="test-service-name"
echo "check ECS service exists"
status=$(aws ecs describe-services --cluster ${CLUSTER} --services ${SERVICE} --query 'failures[0].reason' --output text)
if [[ "${status}" == "MISSING" ]]; then
echo "ecs service ${SERVICE} missing in ${CLUSTER}"
exit 1
fi
在詹金斯管道中相同:
stage('Deploy')
{
steps {
....
script {
string status = sh(script: """
aws ecs describe-services --cluster $ecs_cluster --services $ecs_service --query 'failures[0].reason' --output text
""",
returnStdout: true).trim()
echo "service status: $status"
if (status == 'MISSING') {
echo "ecs service: $ecs_service does not exists in $ecs_cluster"
// put create service logic
}
}}}