3
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AWS CodeCommitを使ってソースコード管理

Last updated at Posted at 2018-08-12

やりたいこと

AWS CodeCommit でソースコード管理したい。
または
AWS CodePipleine のトリガーにCodeCommitを使いたい。 ← 私の場合はこちら

前提

  • AWSアカウントがある。
  • AWS CLIがローカルにインストールされている。

大まかな流れ

  1. AWS CodeCommitにリポジトリと、アクセスするためのIAM User作成
  2. AccessKey設定
  3. gitにCodeCommit認証設定
  4. CodeCommitにpush

手順

CodeCommitにリポジトリとアクセスするIAM User作成する。

今回は、以下の内容で設定する。

CodeCommitリポジトリ名:MyRepoName
IAM User:

  • ユーザ名:codecommit-user-test
  • グループ名:codecommit-group-test
  • 権限ポリシー:AWSCodeCommitPowerUser

CloudFormation yamlファイル

AWSTemplateFormatVersion: 2010-09-09
Description: CodeCommit Test
Resources:
  MyRepo:
    Type: AWS::CodeCommit::Repository
    Properties:
      RepositoryName: MyRepoName
      RepositoryDescription: My test repository
  IamUserCodeCommit:
    Type: AWS::IAM::User
    Properties:
      UserName: codecommit-user-test
      Groups:
      - !Ref 'IamGroupCodeCommit'
  IamGroupCodeCommit:
    Type: AWS::IAM::Group
    Properties:
      GroupName: codecommit-group-test
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/AWSCodeCommitPowerUser

上記 yamlをCloudFormationでstackを作成して実行

IAM Userのアクセスキー発行

  • AWS マネージメントコンソール → [IAM] → [Users] → 今回対象のユーザ[codecommit-user-test] → [Security credentials] → [Create access key]

  • 上記で [Access key ID] と、[Secret access key] が発行されるので、メモるかcsvでダウンロードする

  • aws設定ファイルにプロファイルを追加する。

  • 複数プロファイルを扱う場合は、下記のようにプロファイルに名前をつける。今回は、CodeCommitTest という名前をつけた。

~/.aws/config

[profile CodeCommitTest]
output = json
region = ap-northeast-1  ← 今回使うリージョンを指定

  • 先ほど発行した[Access key ID] と、[Secret access key] をcredentialsファイルに設定
~/.aws/credentials
[CodeCommitTest] ← 上記で設定したプロファイル名
aws_access_key_id = AAAAAAAAAAAAAAAAAAAAAAAA
aws_secret_access_key = SSSSSSSSSSSSSSSSSSSSSSSSSSS

gitコマンドにCodeCommit認証情報を設定

  • 複数リポジトリを扱う場合、credential の横に対象のリポジトリURLを指定する。
    (リポジトリURLはコンソールのCodeCommitのリポジトリの画面で [Clone URL] ボタンを押すと出てくる。)
~/.gitconfig

     :

[credential "https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/MyRepoName"]
        helper = !aws codecommit --profile CodeCommitTest credential-helper $@
        UseHttpPath = true

CodeCommitから git cloneし、ファイルを作成してcommit, pushする

$ git clone https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/MyRepoName
$ cd MyRepoName/
$ echo "# My test repo" > README.md
$ git add .
$ git commit -m "My first commit."
$ git push origin master

あとはいつものGit操作で引き続き・・

もし、すでにリモートリポジトリがあるGit管理のソースの場合、以下のように git push --mirror すれば一度で全部 pushできる。

$ git push --mirror https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/MyRepoName


3
5
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
3
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?