LoginSignup
24
4

More than 3 years have passed since last update.

CloudFormationの変更セットを見やすい表にしてGitHubにコメントする

Last updated at Posted at 2020-12-05

概要

タイトルの件をGitHub Actionsとシェルスクリプトで実現します。▼
0.png
2.png

本文

AWS CloudFormation(以下 CFn)のマネジメントコンソールでは、変更セットの内容を表で確認することができます。▼
console.png
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=$1
changeset_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 body
  id: make-cfn-comment
  run: |
    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-output
    echo ::set-output name=header::changeset-comment # 後述
    echo ::set-output name=result::${comment_body_file} # 出力先ファイル


- name: Comment on pull request
  uses: marocchino/sticky-pull-request-comment@v1
  with:
    GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    header: ${{ steps.make-cfn-comment.outputs.header }} # 同じheaderのコメントがすでにある場合は上書きされる
    path: ${{ steps.make-cfn-comment.outputs.result }} # 出力したファイルのパス

完成したもの

通常はスタック名の一覧だけにすることでコメントの表示サイズが大きくなることを抑えつつ... ▼
1.png

スタック名をクリックして表を見られるようにしました。▼
2.png

▶ view json をクリックして元のJSONを見ることも可能です。▼
3.png

まとめ

GitHub上でCFnの変更セットを確認できるようになり、大変便利になりました😊

24
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
24
4