LoginSignup
12
4

More than 3 years have passed since last update.

AWS Copilot Pipelineを触ってみた

Posted at

きっかけ

前回AWS Copilotを触ってみましたが、その時はpipelineをうまく動かせませんでした。
しかし、その後うまく動いたので今回はcopilot pipelineの話を書こうと思います。

動かなかった原因

なぜ動くようになったのかは不明です...
とりあえずcopilotに新バージョンが出ていたのでバージョンを上げて色々試していたら動きました...

$ copilot --version
copilot version: v0.2.0

Copilotのセットアップ

まずはCopilotをセットアップします。セットアップに使うファイルややり方は前回記事同様です。

# 必要なファイル
.
├── Dockerfile
└── index.html

0 directories, 2 files
# Dockerfile
FROM nginx
EXPOSE 80
COPY index.html /usr/share/nginx/html

copilot initでCopilotのセットアップを開始します。
選択肢の詳細等の詳しいセットアップ方法は前回記事を確認してください。

$ copilot init

(省略)

Application name: qiita-copilot-pipeline-app        # 今回設定したアプリ名
Service type: Load Balanced Web Service
Service name: qiita-copilot-pipeline-service        # 今回設定したサービス名
Dockerfile: ./Dockerfile

(省略)
✔ Wrote the manifest for service qiita-copilot-pipeline-service at copilot/qiita-copilot-pipeline-service/manifest.yml
Your manifest contains configurations like your container size and port (:80).
(省略)

All right, you're all set for local development.
Deploy: Yes

(省略)

git管理しているフォルダでcopilot initを行うと、Copilotセットアップ中に自動で作成されたAmazon ECRのImageタグにcommit番号?(7桁の英数字)が入ります。
git管理していない場合は以下のメッセージが表示され、Imageタグを入力するように求められます。

fatal: not a git repository (or any of the parent directories): .git
Note: Failed to default tag, are you in a git repository?
Input an image tag value: 

設定がすべて終わるとアクセス先URLが表示されるのでブラウザで確認してみましょう。
index.htmlの内容が表示されているはずです。

✔ Deployed qiita-copilot-pipeline-service, you can access it at http://.....

Copilotのセットアップが終わったファイル構成は以下の通りです。

# ファイル構成
.
├── .git
│   └── (省略)
├── Dockerfile
├── README.md
├── copilot                               # 今回のcopilot initで作成されたフォルダ
│   ├── .workspace                        # 今回のcopilot initで作成された隠しファイル
│   └── qiita-copilot-pipeline-service    # 今回のcopilot initで作成されたフォルダ
│       └── manifest.yml                  # 今回のcopilot initで作成されたファイル
└── index.html

copilot initで作成されたファイルをGitHubにプッシュしましょう。
./copilot/.workspaceファイルも忘れずにGitHubプッシュをしてください。
.workspaceファイルがないとPipeline実行時にbuildでエラーが発生します。(気づかずにハマった...)

Copilot Pipelineのセットアップ

続いて今回のメインディッシュのCopilot Pipelineのセットアップを行います。

copilot pipeline initで対話形式のセットアップが始まります。

$ copilot pipeline init

  Would you like to add an environment to your pipeline? [? for help] (y/N) 

Would you like to add an environment to your pipeline?でNoにすると、後ほど聞かれるGitHubのPersonal Access Tokenの設定のみ行われ、Pipelineの設定は行われません。(下の環境に関する質問がスキップされる)
何に使うんだろ...:thinking:

  Which environment would you like to add to your pipeline?  [Use arrows to move, type to filter, ? for more help]
  > test

pipelineを追加する環境を選択します。
ここでは自動で作成されるテスト環境以外の環境は作っていないので、表示されているテスト環境を選択します。

  Which GitHub repository would you like to use for your service?  [Use arrows to move, type to filter, ? for more help]
  > git@github.com:『アカウント名』/『リポジトリ名』

  Please enter your GitHub Personal Access Token for your repository copilot-test: [? for help]

接続するGitHubリポジトリを選択し、GitHubのPersonal Access Tokenを入力します。
Personal Access TokenはGitHubサイトで以下の操作で生成します。
1. GitHubアカウントのSettingsをクリック
2. 左メニューの一番下のDeveloper settingsをクリック
3. 左メニューの一番下のPersonal access tokensをクリック
4. Personal access tokens画面のGenerate new tokenをクリック
詳しくはGitHub「Personal access tokens」の設定方法 - Qiitaが参考になります。

(省略)
✔ Wrote the pipeline manifest for qiita-copilot-pipeline at 'copilot/pipeline.yml'
The manifest contains configurations for your CodePipeline resources, such as your pipeline stages and build steps.
✔ Wrote the buildspec for the pipeline's build stage at 'copilot/buildspec.yml'
The buildspec contains the commands to build and push your container images to your ECR repositories.

セットアップが終わるとcopilot/pipeline.ymlcopilot/buildspec.ymlが作成されます。

# ファイル構成
.
├── .git
│   └── (省略)
├── Dockerfile
├── README.md
├── copilot
│   ├── .workspace
│   ├── buildspec.yml       # 今回のcopilot pipeline initで作成されたファイル
│   ├── pipeline.yml        # 今回のcopilot pipeline initで作成されたファイル
│   └── qiita-copilot-pipeline-service
│       └── manifest.yml
└── index.html

またセットアップ完了後に以下のように推奨手順が表示されます。

Recommended follow-up actions:
- Commit and push the generated buildspec and manifest file.
- Update the build phase of your buildspec to unit test your services before pushing the images.
- Update your pipeline manifest to add additional stages.
- Run `copilot pipeline update` to deploy your pipeline for the repository.

が、今回作成されたbuildspec(buildspec.yml)とPipelineマニフェスト(pipeline.yml)はPipelines · aws/copilot-cli WikiSetting up a Pipeline, step by stepでオプションになっており、以下の通り同ページのシンプルな手順でも触れられていないため、推奨手順に書かれている- Update the build phase of your buildspec to unit test your services before pushing the images.- Update your pipeline manifest to add additional stages.は無視することにします。

# Pipelines · aws/copilot-cli Wikiに書かれているシンプルな手順
$ copilot pipeline init
$ git add copilot/buildspec.yml && git commit -m "Adding Pipeline Buildspec" && git push
$ copilot pipeline update

ということで、Pipelines · aws/copilot-cli Wikiに書かれているシンプルな手順に従います。

$ git add copilot/buildspec.yml && git commit -m "Adding Pipeline Buildspec" && git push
$ copilot pipeline update

CodePipelineが作成されます。
AWSマネジメントコンソールで確認するとしばらくステータスが進行中なので成功しましたになるまで待ちます。
以下のコマンドでも確認できます。

$ copilot pipeline status
Pipeline Status

Stage                        Transition          Status
-----                        ----------          ------
Source                       ENABLED             Succeeded
└── SourceCodeFor-qiita-app                      Succeeded
Build                        ENABLED             InProgress
└── Build                                        InProgress
DeployTo-test                ENABLED               -

Last Deployment

  Updated At    8 minutes ago

ステータスが成功しましたになったらpipelineの設定が完了しているので試しにローカルのindex.htmlを編集してGitHubにプッシュしてみましょう。
5、6分待つと公開サイトに反映されているはずです :tada:

その他のCopilot Pipelineのコマンド

Pipelineの詳細情報表示

$ copilot pipeline show

Pipelineの削除

$ copilot pipeline delete

Pipelineマニフェスト(pipeline.yml)

このファイルは以下の3要素で構成されています。
このファイルを更新した場合はcopilot pipeline updateを実行する必要があります。

  • name: Pipelineの名前
  • source: 追跡するGitHubリポジトリとブランチ
  • stages: デプロイする環境

まとめ

奥が深そうなので今回はbuildspec.ymlには触れませんでした。
が、テストも関係してきてテスト担当者としては見逃せないので、日を改めて研究したいと思います。

We're hiring!

AIチャットボットを開発しています。
ご興味ある方は Wantedlyページ からお気軽にご連絡ください!

参考記事

Pipelines · aws/copilot-cli Wiki
AWS CopilotでAmazon ECSの環境とCI/CDの超簡単構築を試してみた - SMARTCAMP Engineer Blog
GitHub「Personal access tokens」の設定方法 - Qiita

12
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
12
4