Giter VIP home page Giter VIP logo

code-commit's Introduction

AWS CodeCommit

완전 관리형 형상관리 또는 버전관리 서비스로 고가용성, 확장성 및 안정성을 내재한 Git Repository를 제공합니다. 모든 소스코드는 암호화되어 전송 및 저장이 되고 Repository 크기는 무한대로 확장이 가능하며 어떤한 파일도 저장이 가능합니다. 다른 AWS 서비스들과 (AWS CodeBuild, CodeDeploy, CodePipeline, etc) 연계를 통해서 보다 나은 CI/CD 파이프라인을 구축할수 있습니다.

Lab Overview

  1. Git 101 - 기본적인 Git 환경 구성 및 Commands 소개

  2. AWs CodeCommit 연동 방식 실습 (HTTP, SSH)

  3. Git 201 - Branching, Merging, Pull Request

  4. CloudWatch Events, Lambda, CodeBuild로 테스트 자동화 구성

시작하기전에

  1. 본 Hands-on lab은 AWS Seoul region 기준으로 작성되었습니다. Region을 Seoul (ap-northeast-2)로 변경 후 진행 부탁드립니다.
  2. AWS Credit 추가하기
  3. Lab 환경 구축

Repository에 연결

HTTPS

  1. CloudFormation으로 생성된 IAM user로 AWS Management Console 로그인

    IAM user name: lead
    Password: Asdf!234
    
  2. AWS Management Console에서 좌측 상단에 있는 [Services] 를 선택하고 검색창에서 IAM를 검색하거나 [Security, Identity, & Compliance] 바로 밑에 있는 [IAM] 를 선택

  3. IAM Dashboard에서 [Users] 클릭 → lead를 선택 → [Security credentials] → HTTPS Git credentials for AWS CodeCommit 밑에 있는 [Generate credentials] 를 클릭 후 User name과 Password를 메모

  4. AWS Management Console 좌측 상단에 있는 [Services] 를 선택하고 검색창에서 ssm를 검색하고 [Systems Manager] 를 선택

  5. Systems Manager Dashboard 왼쪽 패널 Instances & Nodes 섹션 아래에 있는 [Session Manager] 선택

  6. [Start Session] → Instance Name: lead 선택 → [Start Session] 클릭

    • Home Directory로 이동

      cd
    • Git 설치

      sudo yum install git -y
    • Git 유저 설정

      git config --global user.email "[email protected]"
      git config --global user.name "Your Name"
    • Clone CodeCommit Repository

      git clone https://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/guess
    • 위에서 만든 Git credentials 입력

      Username for 'https://git-codecommit.ap-northeast-2.amazonaws.com':
      Password for 'https://[email protected]':
    • Clone 해당 Github Repository

      git clone https://github.com/fitcloud/code-commit.git
    • 샘플 애플리케이션 구동

      python3 main.py
    • 소스코드 복사

      cp code-commit/*.py guess/
    • Git credentials 저장을 원할경우 (Optional)

      git config credential.helper store
    • CodeCommit Repository로 소스코드를 push

      cd guess
      git add main.py test.py
      git commit -m "initial commit"
      git push
  7. IAM Dashboard에서 [Users] 클릭 → lead를 선택 → [Permissions] 섹션 오른쪽에 있는 [:heavy_plus_sign: Add inline policy] 클릭후, Service = CodeCommit, Actions = GitPush, Resources 탭에 있는 [Add ARN] 클릭 → Region = ap-northeast-2, Repository name = guess → [Add]

  8. [Review Policy]Name = codecommit-lead-access → [Create Policy]

  9. git push 재시도

SSH

  1. AWS Systems Manager Session Manager를 통해서 dev 인스턴스로 연결

    • RSA Key Pair 생성

      ssh-keygen
    • RSA Public Key를 클립보드로 복사

      cat ~/.ssh/id_rsa.pub
  2. IAM Dashboard에서 [Users] 클릭 → dev를 선택 → [Security credentials] → HTTPS Git credentials for AWS CodeCommit 밑에 있는 [Upload SSH public key] 클릭 → 클립보드 내용 붙여넣기 → [Upload SSH public key] → SSH key ID를 메모

  3. 다시 EC2 세션으로 돌아와서

    • Home Directory로 이동

      cd
    • Git 설치

      sudo yum install git -y
    • Git 유저 설정

      git config --global user.email "[email protected]"
      git config --global user.name "Your Name"
    • 로컬 SSH 연결 설정

      vi ~/.ssh/config
      Host git-codecommit.*.amazonaws.com
      User Your-IAM-SSH-Key-ID-Here
      IdentityFile ~/.ssh/id_rsa
      
      chmod 600 ~/.ssh/config
    • Clone CodeCommit Repository

      git clone ssh://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/guess
  4. 어플리케이션을 1 ~ 20까지의 숫자를 맞추는걸로 수정하고 Git Repository에 변경사항 반영

  5. 권한 부족으로 리포지토리에 변경사항을 반영할수 없으므로 다음 단계로 진행

Branching

  1. Git Branch 생성 후 Origin으로 Push

    git checkout -b feature-max20
    git push -u origin feature-max20
  2. 권한 부족으로 리포지토리에 변경사항을 반영할수 없으므로 다음 단계로 진행

  3. IAM Dashboard에서 [Users] 클릭 → dev를 선택 → [Permissions] 섹션 아래 [Add permissions][Attach existing policies directly] → ✅ AWSCodeCommitPowerUser 선택 → [Next: Review][Add permissions]

  4. [:heavy_plus_sign: Add inline policy] 클릭 → JSON 선택 후 아래 내용 붙여넣고 [Review policy]

    {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Effect": "Deny",
          "Action": [
            "codecommit:GitPush",
            "codecommit:DeleteBranch",
            "codecommit:PutFile",
            "codecommit:MergePullRequestByFastForward"
          ],
          "Resource": "arn:aws:codecommit:ap-northeast-2:*:guess",
          "Condition": {
            "StringEqualsIfExists": {
              "codecommit:References": ["refs/heads/master"]
            },
            "Null": {
              "codecommit:References": false
            }
          }
        }
      ]
    }
  5. Name = DenyChangesToMaster → [Create policy]

  6. 다시 EC2 세션으로 돌아와서 Git Branch를 Origin으로 Push 재시도

    git push -u origin feature-max20

Pull Request & Merging branch

  1. dev IAM user로 AWS Management Console 로그인

    IAM user name: dev
    Password: Asdf!234
    
  2. AWS Management Console에서 좌측 상단에 있는 [Services] 를 선택하고 검색창에서 CodeCommit을 검색하거나 [Developer Tools] 밑에 있는 [CodeCommit] 를 선택

  3. guess Repository를 선택 → 왼쪽 패널에서 [Branches][Create pull request] 클릭 → Destination = master, Source = feature-max20 → [Compare]

  4. Title = NEW-01 최댓값 변경, Description = 사용자 XXX의 요청으로 최댓값을 20으로 변경 → [Create pull request]

  5. lead IAM user로 AWS Management Console 로그인하고 AWSCodeCommitPowerUser 권한 부여

  6. CodeCommit Dashboard에서 Pull requests 섹션으로 이동

  7. NEW-01 최댓값 변경 Pull request를 선택 → [Approve][Merge][Merge pull request]

  8. Master branch의 소스코드에 변경사항이 적용 됬는지 확인

Test Automation

Test Pipeline

  1. 개발자가 Pull Requeust 생성

  2. CloudWatch Events에서 Pull Request에 대한 이벤트를 인지하고 Lambda 실행

  3. Lambda function을 통해서 CodeBuild에 있는 Test 빌드 실행

  4. CloudWatch Events에서 Test 빌드의 상태변화를 인지하고 Lambda 실행

  5. Test 빌드의 상태를 Pull Request 댓글에 기록

CodeBuild

  1. AWS Management Console에서 좌측 상단에 있는 [Services] 를 선택하고 검색창에서 CodeBuild를 검색하거나 [Developer Tools] 밑에 있는 [CodeBuild] 를 선택

  2. [Create build proejct]Project name = guess-unittest, Source provider = AWS CodeCommit, Repository = guess, Reference type = Branch, Environment image = Managed Image, Operating system = Amazon Linux 2, Runtime(s) = Standard, Image = aws/codebuild/amazonlinux2-x86_64-standard:3.0, Service role = New service role, Build specifications = Insert build commands → [Switch to editor] → 아래 커맨드블록을 Build commands에 붙여놓고 [Create build project]

    version: 0.2
    
    phases:
      install:
        runtime-versions:
          python: 3.8
      build:
        commands:
          - python3 -m unittest

Lambda - CodeBuild 실행

  1. AWS Management Console에서 좌측 상단에 있는 [Services] 를 선택하고 검색창에서 Lambda를 검색하거나 [Compute] 밑에 있는 [Lambda] 를 선택

  2. Lambda Dashboard에서 [Create function] 클릭후, Function name = run_testbuild, Runtime = Python 3.8, [Create function] 클릭

  3. [Configuration][Permission]Execution role 에서 run_testbuild-role-xxxx 를 선택

  4. [Permissions] 섹션 오른쪽에 있는 [:heavy_plus_sign: Add inline policy] 클릭후, Service = CodeBuild, Actions = StartBuild, Resources 탭에 있는 [Add ARN] 클릭 → Region = ap-northeast-2, Project name = guess-unittest → [Add]

  5. [Review Policy]Name = allow-lambda-run-codebuild → [Create Policy]

  6. 아래 코드블록을 Lambda에 복사 후, [Save] 클릭

    import json
    import boto3
    
    def lambda_handler(event, context):
    
        codebuild = boto3.client('codebuild')
        response = codebuild.start_build(
            projectName = 'guess-unittest',
            sourceVersion= event['detail']['sourceCommit'],
            environmentVariablesOverride=[
                {
                    'name': 'pullRequestId',
                    'value': event['detail']['pullRequestId'],
                    'type': 'PLAINTEXT'
                },
                {
                    'name': 'repositoryName',
                    'value': 'guess',
                    'type': 'PLAINTEXT'
                },
                {
                    'name': 'destinationCommit',
                    'value': event['detail']['destinationCommit'],
                    'type': 'PLAINTEXT'
                }
            ]
        )

CloudWatch Events - Pull Request 변경 탐지

  1. AWS Management Console에서 좌측 상단에 있는 [Services] 를 선택하고 검색창에서 CloudWatch를 검색하거나 [Management & Governance] 밑에 있는 [CloudWatch] 를 선택

  2. CloudWatch Dashboard에서 [Events] 섹션 아래에 있는 [Rules][Create rule]

  3. 왼쪽 Event Source에서 ✅ Event Pattern 선택 , Service Name = CodeCommit, Event Type = CodeCommit Pull Request State Change, ✅ Specific resource(s) by ARN = CodeCommit Repository ARN 입력

  4. 오른쪽 Targets에서 [:heavy_plus_sign: Add target]Lambda functionFunction = run_testbuild → [Configure details]

  5. Name = pull_request_made, State = ✅ Enabled → [Create rule]

Lambda - CodeBuild 결과 전송

  1. AWS Management Console에서 좌측 상단에 있는 [Services] 를 선택하고 검색창에서 Lambda를 검색하거나 [Compute] 밑에 있는 [Lambda] 를 선택

  2. Lambda Dashboard에서 [Create function] 클릭후, Function name = post_test_result, Runtime = Python 3.8, [Create function] 클릭

  3. 좌측 하단에 있는 [Execution role] 에서 View the post_test_result-role-xxxx on the IAM console 를 선택

  4. [Permissions] 섹션 오른쪽에 있는 [:heavy_plus_sign: Add inline policy] 클릭후, Service = CodeCommit, Actions = PostCommentForPullRequest, Resources 탭에 있는 [Add ARN] 클릭 → Region = ap-northeast-2, Repository name = guess → [Add]

  5. [Review Policy]Name = allow-lambda-post-comment-on-pr → [Create Policy]

  6. 아래 코드블록을 Lambda에 복사 후, [Save] 클릭

    import json
    import boto3
    import re
    
    def lambda_handler(event, context):
        envs = event['detail']['additional-information']['environment']['environment-variables']
        repositoryName = [var['value'] for var in envs if var['name'] == 'repositoryName'][0]
        pullRequestId = [var['value'] for var in envs if var['name'] == 'pullRequestId'][0]
        destinationCommit = [var['value'] for var in envs if var['name'] == 'destinationCommit'][0]
        buildStatus = event['detail']['build-status']
        buildArn = event['detail']['build-id']
        buildId = re.findall(r'(?<=\/)(.*)', buildArn)[0]
        sourceVersion = envs = event['detail']['additional-information']['source-version']
    
        codecommit = boto3.client('codecommit')
        response = codecommit.post_comment_for_pull_request(
            pullRequestId = pullRequestId,
            repositoryName = repositoryName,
            content = f'[BUILD](https://ap-northeast-2.console.aws.amazon.com/codesuite/codebuild/projects/guess-unittest/build/{buildId}): {buildStatus}',
            afterCommitId = sourceVersion,
            beforeCommitId = destinationCommit
        )

CloudWatch Events - CodeBuild 상태 탐지

  1. AWS Management Console에서 좌측 상단에 있는 [Services] 를 선택하고 검색창에서 CloudWatch를 검색하거나 [Management & Governance] 밑에 있는 [CloudWatch] 를 선택

  2. CloudWatch Dashboard에서 [Events] 섹션 아래에 있는 [Rules][Create rule]

  3. 왼쪽 Event Source에서 ✅ Event Pattern 선택 후 Dropdown 리스트에서 Custom event pattern 선택 후 아래 블록을 붙여넣기

    {
      "source": ["aws.codebuild"],
      "detail-type": ["CodeBuild Build State Change"],
      "detail": {
        "build-status": ["IN_PROGRESS", "SUCCEEDED", "FAILED", "STOPPED"],
        "project-name": ["guess-unittest"]
      }
    }
  4. 오른쪽 Targets에서 [:heavy_plus_sign: Add target]Lambda functionFunction = post_test_result → [Configure details]

  5. Name = test_build_run, State = ✅ Enabled → [Create rule]

테스트

  1. 새로운 Git Branch를 생성 후, 어플리케이션을 1 ~ 30까지의 숫자를 맞추는걸로 수정하고 해당 Branch를 CodeCommit에 Push하고 Pull Request 생성

  2. CodeCommit Dashboard에서 방금 생성한 Pull request를 선택하고, Activity나 Changes 탭 밑에서 CodeBuild 테스트 결과 확인

  3. CodeBuild 테스트 빌드가 성공하도록 소스코드 수정

Cleanup

  1. IAM 유저에 수동으로 추가된 인라인 정책, AWSCodeCommitPowerUser 정책 및 Git 자격증명 삭제
  2. Lambda 함수 삭제 - post_test_result, run_testbuild
  3. CloudWatch Events 삭제 - pull_request_made, test_build_run
  4. AWS CodeBuild 프로젝트 삭제 - guess-unittest
  5. CloudFormation 스택 삭제

code-commit's People

Contributors

youngwjung avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.