[英]How to use getExecutionHistory from aws java sdk to check status of a step function
[英]Check for step function status in code build and control the pipeline behaviour
我有一個代碼構建管道,它在命令部分執行步驟 function。 下面是代碼構建管道的片段
version: "0.2"
env:
git-credential-helper: "yes"
shell: bash
variables:
PROFILEFLAGS: ""
phases:
install:
commands:
- npm install jsonlint -g
pre_build:
commands:
- echo "pre_build"
build:
on-failure: ABORT
commands:
- aws stepfunctions start-execution --state-machine-arn arn:aws:states:${AWS_REGION}:${TARGET_ACCOUNT}:stateMachine:${STEP_FUNCTION} --name Ab-sf-$CODEBUILD_BUILD_NUMBER --input "xyz.json"
post_build:
commands:
- echo "Display Last Changes"
artifacts:
files:
- xyz.json
當前的問題是,構建在執行步驟 function 時繼續進行。 我想有一個條件如下:
解決這個問題的推薦方法是什么。
通過使用 AWS CLI 命令aws stepfunctions describe-execution創建一個帶有監控循環的腳本來更新 state 機器狀態,這很容易實現
下面是一個示例代碼。 隨意添加更復雜的錯誤和異常處理和執行邏輯。
#!/bin/bash
main() {
start_execution # Start state machine execution
monitor_execution # Start monitoring loop
echo "{\"status\":\"$EXECUTION_STATUS\"}" # Print script output as JSON string.
if [ $EXECUTION_STATUS != "SUCCEEDED" ]
then
exit 1 # Set exit code to 1 (error) if buid was not successful
else
exit 0 # Set exits code to 0 (success) if build was successful
fi
}
# Starts Step Functions state machine
start_execution() {
EXECUTION_ARN=$(aws stepfunctions start-execution --state-machine-arn [arn] --input {} --region [region] | jq -r '.executionArn | tostring')
EXECUTION_STATUS="RUNNING" # Initialize executions status for the first iteration of monitoring loop.
}
# Monitors execution status. When status changes from RUNNING to some other state, function calls error handling function
monitor_execution () {
until [ $EXECUTION_STATUS != "RUNNING" ]
do
sleep [value] # Monitoring loop sleep time
update_execution_status # Update status for the next comparison
done
}
update_execution_status() {
EXECUTION_STATUS=$(aws stepfunctions describe-execution --execution-arn ${EXECUTION_ARN} --region [region] | jq -r .status)
}
if [ "$0" == "${BASH_SOURCE[0]}" ] ; then
main "$@"
fi
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.