静的Webサイトホスティング二重化してみる(その3)

2020年中に完了させたかったのですが、結局年をまたいでしまいました。静的Webサイトホスティング冗長化の件、設定作業自体は前回の内容で完了しているのですが、コンテンツの更新で2ヶ所に更新するというのもナンセンスだし、今時FTPでファイルアップとかいう時代遅れなことをする気もないのでGitHubmainブランチpushしたタイミングでS3GCSの両方にコンテンツを同期するという仕組みを準備することで更新漏れを防止するという仕組みを準備します。こうしておけばいつCloudFrontが落ちても安心ですね。(オイ

後は2021年8月13日にGitHubでパスワード運用が廃止になるらしいのでトークンを発行してパスワード運用からの切替えもしました。

GitHubにpushすれば本番リリースというのはとても楽でいいですね。確認終わったやつをmergeしてからpushすりゃ本番リリース完了サーバ再起動も発生しない。もはやWebのためにサーバという発想がなくなってきています。後はこいつをIaC化できればいいなぁ。


最終的にこんな感じになりました。







◆シリーズ

その1:AWSでCloudFront+S3構成を作る

その2:GoocleCloudでLoadBaranser+GCS構成を作りDNSでフェイルオーバ

その3:S3とGCSにGitHubActions使ってコンテンツ同期

その4:TerraformでIaC化


◆手順概要

1.AWSのIAM設定

2.GCPのサービスアカウント発行

3.GitHubの諸設定

 3-1.トークン設定

 3-2.Secretの設定

4.GitHubActionsの設定


◆手順

1.AWSのIAM設定

・コンソールでIAMから[グループ]を選択して[新しいグループの作成]をクリックする。







・[グループ名]を選択して[次のステップ]をクリックする。








・ポリシーで[S3FullAccess]と[CloudFrontFullAccess]を付与して[グループの作成]をクリックする。








・続いてユーザの作成を行う[ユーザ]を選択して[ユーザを追加]をクリックする。








・[ユーザ名]を入力し、アクセスの種類で[プログラムによるアクセス]を選択する。[次のステップ]をクリックする。








・ユーザをグループに追加セクションで先に作成したグループ名を選択して[次のステップ]をクリックする。








・タグの追加は特に何もせずに「次のステップ」をクリックする。








・確認となるので[ユーザの作成]をクリックする。








・ユーザ追加が完了するので[CSVのダウンロード]にて[アクセスキーID]と[シークレットアクセスキー]を保存しておいて[閉じる]をクリックする。









2.GCPのサービスアカウント発行

・サービスアカウント作成

$ gcloud iam service-accounts create SA-NAME \
    --display-name="SA-NAME"
Created service account [SA-NAME].

・ロール付与

$ gcloud projects add-iam-policy-binding PROJECT-ID \
  --member="serviceAccount:SA-NAME@PROJECT-ID.iam.gserviceaccount.com" \
  --role roles/storage.admin
$ gcloud iam service-accounts list

・サービスアカウントキー発行

$ cd
$ mkdir credentials
$ gcloud iam service-accounts keys create ~/credentials/static_sitekey.json \
  --iam-account SA-NAME@PROJECT-ID.iam.gserviceaccount.com

・GitHub登録用Base64エンコードのキー作成

$ cat ~/credentials/static_sitekey.json | base64

※出力されたBase64のテキストをGitHubのSecretのGCS_SA_KEYに貼り付ける


3.GitHubの諸設定

 3-1.トークン設定

・アカウントをクリックして[Setings]を選択










・[Developer Settings]を選択する










・[Personal access tokens]をクリックし[Generate new token]を選択する。




・[Note]に説明を入力し[repo]をクリックする。






・[GenerateToken]をクリックする。





・トークンが表示するのでコピーしておく。(この画面から遷移したら二度とトークンを確認するすべがないので再作成が必要となる。)






 3-2.Secretの設定

・新しくリポジトリを作成する。コンソールの右側の[New]をクリックする。




・一応、[Private]としておいた方がいい。その他はチェックを付けておくに越したことはないが任意で問題ない、以上を確認し[Create repository]をクリックする。










・該当リポジトリを選択して右上の[Settings]を選択する。




・[Secrets]を選択し[New repository secret]をクリックするとsecretの入力画面が表示されるので[Value]の箇所に設定する。






※以下を設定する。

AWS_ACCESS_KEY_ID(CSVファイルの「Access key ID」)
AWS_SECRET_ACCESS_KEY(CSVファイルの「Secret access key」)
S3_UPLOAD_BUCKET(AmazonS3で公開設定しているバケットの「バケット名」)
GCS_SA_KEY(2.GCPのサービスアカウント発行の「GitHub登録用Base64エンコードのキー」を貼付)
GCS_UPLOAD_BUCKET(GoogleCloudStorageで公開設定しているバケットの「バケット名」)


4.GitHubActionsの設定

・Git認証設定(認証確認されない様に設定)

$ git config --global user.email "youremail@example.com"
$ git config --global user.name "username"
$ vi ~/.netrc
-----------------
machine github.com
login username
password YourToken
----------------

・ディレクトリ類準備

$ git clone [Your Git Repo] html
$ cd html
$ mkdir dist
$ mkdir .github
$ mkdir .github/workflows
※distディレクトリにindex.htmlと404.htmlをアップロード
$ cd .github/workflows
$ tree -a --charset=C
.
|-- .git
|-- .github
|   `-- workflows
|-- .gitignore
`-- dist
    |-- 404.html
    `-- index.html

・WorkFlowファイル作成

$ vi staticsite_deploy.yaml
----------------------------
name: Website - Production
on:
  push:
    branches:
      - main
jobs:
  build:
    runs-on: ubuntu-latest
    timeout-minutes: 5
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: ap-northeast-1

      - name: Sync to S3
        env:
          S3_UPLOAD_BUCKET: ${{ secrets.S3_UPLOAD_BUCKET }}
        run: |
          aws s3 sync --delete dist s3://$S3_UPLOAD_BUCKET/

      - name: Configure GCP credentials
        uses: google-github-actions/setup-gcloud@master
        with:
          service_account_key: ${{ secrets.GCS_SA_KEY }}
          project_id: ${{ secrets.GCS_PROJECT }}
          export_default_credentials: true

      - name: Sync to GCS
        env:
          GCS_UPLOAD_BUCKET: ${{ secrets.GCS_UPLOAD_BUCKET }}
        run: |
          gsutil -m rsync -R dist gs://$GCS_UPLOAD_BUCKET
----------------------------

・GitHubにpush

$ cd ../..
$ git add .
$ git commit -m '2021-01-01 GitHubActions'
$ git push origin main


・該当リポジトリの[Actions]をクリックするとジョブの状況を確認できる。




◆参考サイト

・Git→S3連携

https://dev.classmethod.jp/articles/s3-file-up-down-from-github-actions/

https://blog.honai.me/post/github-actions-aws-s3

https://qiita.com/tippy/items/488e52411a9a5765330f

https://tech.yutaka0m.com/2020/08/01/hugo-github-actions/


・Git→GCS連携

https://sha.ws/automatic-upload-to-google-cloud-storage-with-github-actions.html

https://qiita.com/shogomuranushi/items/63682604d69f46cf1083

https://www.mickaelvieira.com/blog/2020/01/29/deploying-a-static-website-to-google-cloud-storage-with-github-actions.html

https://github.com/google-github-actions/setup-gcloud


・GitHubトークン設定

https://docs.github.com/ja/free-pro-team@latest/github/authenticating-to-github/creating-a-personal-access-token

https://blog.katsubemakito.net/git/git-config-1st

https://qiita.com/azusanakano/items/8dc1d7e384b00239d4d9


・GitHubActions

https://qiita.com/bigwheel/items/2ab7deb237122db2fb8d


・サービスアカウントキー作成(GCP)

https://cloud.google.com/iam/docs/service-accounts?hl=JA

https://messefor.hatenablog.com/entry/2020/10/08/080414

https://www.apps-gcp.com/cloudiam-role-management/

https://qiita.com/bendevs/items/066194b37e3753d0c201



以下は色々と試行錯誤してた時の参考ですが結局はGitHubActionsに落ち着きました。

AWS単体での場合、CodePipeLineはスグレモノです。GUIですべて完結できるので非常にラクです。CloudBuildもGitHubをトリガーにできますがファイルでGCP側の制御の定義書いてやらないとだめです。CodePipeLineでディレクトリ直下のhtml等静的サイトを同期するのは簡単にできますが、CloudBuildのファイルを残さないといけなくなり、外部からCloudBuildのトリガーファイルが見えるのもよろしくないので、その辺りの制御が柔軟なGitHubActionsにしました。


・CodePipeLine(AWS)

https://www.kabegiwablog.com/entry/2019/02/19/100000

https://s8a.jp/angular-github-aws-s3-auto-deploy#angular%E3%81%A7web%E3%82%A2%E3%83%97%E3%83%AA%E3%82%B1%E3%83%BC%E3%82%B7%E3%83%A7%E3%83%B3%E3%82%92%E4%BD%9C%E6%88%90%E3%81%99%E3%82%8B

https://www.ketancho.net/entry/2017/12/09/033436

https://kotamat.com/post/codepipeline-github-actions/


・CloudBuild(GCP)

https://cloud.google.com/cloud-build/docs/automating-builds/run-builds-on-github

https://cloud.google.com/cloud-build/docs/automating-builds/create-github-app-triggers

https://medium.com/google-cloud-jp/try-github-cloudbuild-integration-5149175105fb

https://qiita.com/butterv/items/39382f02b7354dcd8fe1

https://qiita.com/anyanco/items/2800fac3da2432ee7f6f

https://qiita.com/overgoro56/items/888bf128e0d5d33f4798

https://www.apps-gcp.com/cloud-build-getting-started/#Cloud_Build-2

https://qiita.com/kyhei_0727/items/79482fd5340d5e6e4fb6


コメント

このブログの人気の投稿

証券外務員1種勉強(計算式暗記用メモ)

GASでGoogleDriveのサブフォルダとファイル一覧を出力する

マクロ経済学(IS-LM分析)