概要
タイトルの件をGitHub Actionsとシェルスクリプトで実現します。▼
本文
AWS CloudFormation(以下 CFn)のマネジメントコンソールでは、変更セットの内容を表で確認することができます。▼
AWS CLIでも変更セットの内容は取得できます。ただし返ってくるJSONは人間が読める感じではありません。▼
$ aws cloudformation describe-change-set --change-set-name$changeset_id{とても長いJSONが返ってくる}
そこでこのJSONをjq
コマンドでゴニョゴニョし、Markdownの表にします。さらにそれをGitHubのプルリクエストにコメントします。
jqでいい感じにする
シェルスクリプトを作ります。これをCI/CDで使います。▼
output_changeset_as_markdown.sh
# 前略changeset_id=$1changeset_json=$(aws cloudformation describe-change-set --change-set-name$changeset_id)stack_name=$(echo"$changeset_json" | jq -r .StackName)changes=$(echo"$changeset_json" | jq -r .Changes)changes_length=$(echo"$changes" | jq length)echo"<details><summary><code>$stack_name ($changes_length changes)</code></summary>"# クリックで展開できるやつecho
if[$changes_length-gt 0 ];then
echo'|Action|論理ID|物理ID|リソースタイプ|置換|'# 少しでも横幅を減らすためにActionだけ英語echo'|---|---|---|---|---|'for i in$(seq 0 $(($changes_length-1)));do
row=$(echo"$changes" | jq -r .[$i].ResourceChange)col_1=$(echo"$row" | jq -r .Action)col_2=$(echo"$row" | jq -r .LogicalResourceId)col_3=$(echo"$row" | jq -r .PhysicalResourceId | sed-e's/null/-/')# nullの場合'-'を表示col_4=$(echo"$row" | jq -r .ResourceType | sed-e's/AWS:://')# リソースタイプの'AWS::'は省略col_5=$(echo"$row" | jq -r .Replacement | sed-e's/null/-/' | sed-e's/True/\*\*True\*\*/')# nullの場合'-'を表示。Trueなら太字にするecho"|$col_1|$col_2|$col_3|$col_4|$col_5|"done
fi
echo'<ul><li><details><summary>view json</summary>'# インデントを付ける目的でリストにしているecho
echo'```json'echo"$changeset_json"echo'```'echo'</details></li></ul></details>'
CI/CDでプルリクエストにコメントする
以下はGitHub Actionsでの例です。
今回はsticky-pull-request-commentを使い、プルリクエストのコメントをUpsert(なければInsert,あればUpdate)します。
【参考】GitHub ActionsでPRに同一種類のコメントをUpsertで追加する方法
ワークフローは以下のようにします。▼
workflow.yml
# 前略(以下はjobのsteps)-name:Make comment bodyid:make-cfn-commentrun:|comment_body_file="/tmp/pr_comment_body.txt" # 出力先ファイル# コメント本文の作成($changeset_id_listは変更セットのID一覧)for id in $(echo "$changeset_id_list"); do./scripts/output_changeset_as_markdown.sh $id >> ${comment_body_file}done# タイトルの追加comment_title="### CFn change sets"sed -i "1i ${comment_title}" ${comment_body_file}# 次のステップで使うためにset-outputecho ::set-output name=header::changeset-comment # 後述echo ::set-output name=result::${comment_body_file} # 出力先ファイル-name:Comment on pull requestuses:marocchino/sticky-pull-request-comment@v1with:GITHUB_TOKEN:${{ secrets.GITHUB_TOKEN }}header:${{ steps.make-cfn-comment.outputs.header }}# 同じheaderのコメントがすでにある場合は上書きされるpath:${{ steps.make-cfn-comment.outputs.result }}# 出力したファイルのパス
完成したもの
通常はスタック名の一覧だけにすることでコメントの表示サイズが大きくなることを抑えつつ... ▼
▶ view json
をクリックして元のJSONを見ることも可能です。▼
まとめ
GitHub上でCFnの変更セットを確認できるようになり、大変便利になりました😊