簡體   English   中英

檢查代碼構建中的步驟 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 時繼續進行。 我想有一個條件如下:

  1. 如果步驟 function 正在運行 state,則等待它完成。
  2. 如果步驟 function 成功 state 則繼續執行下一個命令。

解決這個問題的推薦方法是什么。

通過使用 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM