Skip to content

Instantly share code, notes, and snippets.

@ljlm0402
Last active June 26, 2023 23:40
Show Gist options
  • Save ljlm0402/b0e96d2d7b206080308c623ea9ecf2df to your computer and use it in GitHub Desktop.
Save ljlm0402/b0e96d2d7b206080308c623ea9ecf2df to your computer and use it in GitHub Desktop.
Github Actions Workflow / 2020. 02. 28

Workflow 실행

 name: my workflow                           # Workflow 이름
 on: [push]                                  # Event 감지

 jobs:                                       # Job 설정
   build:                                    # Job ID
     name: hello github action               # Job 이름
     runs-on: ubuntu-latest                  # Job 인스턴스 환경
     steps:                                  # Steps
       - name: checkout source code          # Step - 1
         uses: actions/checkout@master       # Uses를 통한 외부 설정 가져오기 - 레포지토리 소스 참조

       - name: echo Hello                    # Step - 2
         run: echo "Hello"                   # Run을 통한 스크립트 실행 - Hello 출력


Checkout을 이용하여 레포 소스 가져오기

name: checkout Build                        # Workflow 이름
on:                                         # Event 감지
    push:                                   # Push 이벤트
    branches:                               # Branch 지정 - master
        - master

jobs:                                       # Job 설정
    build:                                  # Job ID
    runs-on: ubuntu-latest                  # Job 인스턴스 환경
    steps:                                  # Steps
        - name: Checkout source code.       # Step - 1
        uses: actions/checkout@master       # Uses를 통한 외부 설정 가져오기 - 레포지토리 소스 참조
        
        - name: Install Dependencies        # Step - 2
        run: npm install                    # Run을 통한 스크립트 실행 - 노드 모듈 설치
            
        - name: Build                       # Step - 3
        run: npm run build                  # Run을 통한 스크립트 실행 - 패키지 빌드


Cache을 이용하여 의존성 설치 시간 감소

name: cache Build                           # Workflow 이름
on:                                         # Event 감지
  push:                                     # Push 이벤트
    branches:                               # Branch 지정 - master
      - master

jobs:                                       # Job 설정
  build:                                    # Job ID
    runs-on: ubuntu-latest                  # Job 인스턴스 환경
    steps:                                  # Steps
      - name: Checkout source code.         # Step - 1
        uses: actions/checkout@master       # Uses를 통한 외부 설정 가져오기 - 레포지토리 소스 참조

      - name: Cache node modules            # Step - 2
        uses: actions/cache@v1              # Uses를 통한 외부 설정 가져오기 - 캐싱
        with:                               # With를 통한 상세 기능 설정
          path: node_modules                # Path를 통한 캐시 폴더 지정
          key: ${{ runner.OS }}-build-${{ hashFiles('**/package-lock.json') }} # Key를 통한 해시 파일로 키 생성
          restore-keys: |                   # Restore-keys를 통한 해시 파일로 생성된 키 보관
            ${{ runner.OS }}-build-
            ${{ runner.OS }}-

      - name: Install Dependencies          # Step - 3
        run: npm install                    # Run을 통한 스크립트 실행 - 노드 모듈 설치

      - name: Build                         # Step - 4
        run: npm run build                  # Run을 통한 스크립트 실행 - 패키지 빌드


AWS S3 빌드

name: S3 Build                              # Workflow 이름
on:                                         # Event 감지
  push:                                     # Push 이벤트
    branches:                               # Branche 지정 - master
      - master

jobs:                                       # Job 설정
  build:                                    # Job ID
    runs-on: ubuntu-latest                  # Job 인스턴스 환경
    steps:                                  # Steps
      - name: Checkout source code.         # Step - 1
        uses: actions/checkout@master       # Uses를 통한 외부 설정 가져오기 - 레포지토리 소스 참조

      - name: Cache node modules            # Step - 2
        uses: actions/cache@v1              # Uses를 통한 외부 설정 가져오기 - 캐싱
        with:                               # With를 통한 상세 기능 설정
          path: node_modules                # Path를 통한 캐시 폴더 지정
          key: ${{ runner.OS }}-build-${{ hashFiles('**/package-lock.json') }} # Key를 통한 해시 파일로 키 생성
          restore-keys: |                   # Restore-keys를 통한 해시 파일로 생성된 키 보관
            ${{ runner.OS }}-build-
            ${{ runner.OS }}-

      - name: Install Dependencies          # Step - 3
        run: npm install                    # Run을 통한 스크립트 실행 - 노드 모듈 설치

      - name: Build                         # Step - 4
        run: npm run build                  # Run을 통한 스크립트 실행 - 패키지 빌드

      - name: Deploy                        # Step - 5
        env:                                # Env를 통한 환경변수 지정
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} # { key : value } 환경변수 생성 - AWS ACCESS KEY ID 생성 (AWS IAM 발급)
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} # { key : value } 환경변수 생성 - AWS SECRET ACCESS KEY ID 생성 (AWS IAM 발급)
        run: |                              # Run을 통한 스크립트 실행 - AWS S3 패키지 배포 - build dist 폴더 / S3 Bucket name 설정
          aws s3 cp \
            --recursive \
            --region ap-northeast-2 \
            [build folder] s3://[s3 bucket name] 


AWS S3 빌드 + Slack

name: S3 Build                              # Workflow 이름
on:                                         # Event 감지
  push:                                     # Push 이벤트
    branches:                               # Branche 지정 - master
      - master

jobs:                                       # Job 설정
  build:                                    # Job ID
    runs-on: ubuntu-latest                  # Job 인스턴스 환경
    steps:                                  # Steps
      - name: Checkout source code.         # Step - 1
        uses: actions/checkout@master       # Uses를 통한 외부 설정 가져오기 - 레포지토리 소스 참조

      - name: Cache node modules            # Step - 2
        uses: actions/cache@v1              # Uses를 통한 외부 설정 가져오기 - 캐싱
        with:                               # With를 통한 상세 기능 설정
          path: node_modules                # Path를 통한 캐시 폴더 지정
          key: ${{ runner.OS }}-build-${{ hashFiles('**/package-lock.json') }} # Key를 통한 해시 파일로 키 생성
          restore-keys: |                   # Restore-keys를 통한 해시 파일로 생성된 키 보관
            ${{ runner.OS }}-build-
            ${{ runner.OS }}-

      - name: Install Dependencies          # Step - 3
        run: npm install                    # Run을 통한 스크립트 실행 - 노드 모듈 설치

      - name: Build                         # Step - 4
        run: npm run build                  # Run을 통한 스크립트 실행 - 패키지 빌드

      - name: Deploy                        # Step - 5
        env:                                # Env를 통한 환경변수 지정
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} # { key : value } 환경변수 생성 - AWS ACCESS KEY ID 생성 (AWS IAM 발급)
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} # { key : value } 환경변수 생성 - AWS SECRET ACCESS KEY ID 생성 (AWS IAM 발급)
        run: |                              # Run을 통한 스크립트 실행 - AWS S3 패키지 배포 - build dist 폴더 / S3 Bucket name 설정
          aws s3 cp \
            --recursive \
            --region ap-northeast-2 \
            [build folder] s3://[s3 bucket name] 
            
      - name: Notification                  # Step - 6
        uses: 8398a7/action-slack@v2.6.0    # Uses를 통한 외부 설정 가져오기 - 슬랙 알림 기능 참조
        with:                               # With를 통한 상세 기능 설정
          status: ${{ job.status }}         # Status를 통한 Job 상태 확인
          author_name: [slack chatbot name] # Author_name을 통한 슬랙 챗봇 이름 지정
        env:                                # Env를 통한 환경변수 지정
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # { key : value } 환경변수 생성 - GITHUB_TOKEN 생성 (GitHub 발급) 
          SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} # { key : value } 환경변수 생성 - SLACK_WEBHOOK_URL 생성 (Slack 발급) 


AWS ECS 배포

on:
  push:
    branches:
      - master

name: Deploy to Amazon ECS

jobs:
  deploy:
    name: Deploy
    runs-on: ubuntu-latest

    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: us-east-2

    - name: Login to Amazon ECR
      id: login-ecr
      uses: aws-actions/amazon-ecr-login@v1

    - name: Build, tag, and push image to Amazon ECR
      id: build-image
      env:
        ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
        ECR_REPOSITORY: api-server-boilerplate
        IMAGE_TAG: ${{ github.sha }}
      run: |
        # Build a docker container and
        # push it to ECR so that it can
        # be deployed to ECS.
        docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
        docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
        echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG"
    - name: Fill in the new image ID in the Amazon ECS task definition
      id: task-def
      uses: aws-actions/amazon-ecs-render-task-definition@v1
      with:
        task-definition: task-definition.json
        container-name: api-server-boilerplate
        image: ${{ steps.build-image.outputs.image }}

    - name: Deploy Amazon ECS task definition
      uses: aws-actions/amazon-ecs-deploy-task-definition@v1
      with:
        task-definition: ${{ steps.task-def.outputs.task-definition }}
        service: api-server-boilerplate
        cluster: your-cluster
        wait-for-service-stability: true

npm 패키지 배포

name: npm publish                           # Workflow 이름

on:
  push:                                     # Event 감지
    branches:                               # Push 이벤트
      - master                              # Branche 지정 - master

jobs:                                       # Job 설정
  build:                                    # Job ID
    runs-on: ubuntu-latest                  # Job 인슨터스 환경
    steps:                                  # Steps
      - name: Checkout source code          # Step - 1
        uses: actions/checkout@v2           # Uses를 통한 외부 설정 가져오기 - 레포지토리 소스 참조

      - name: Setup nodejs                  # Step - 2
        uses: actions/setup-node@v1.4.3     # Uses를 통한 외부 설정 가져오기 - 노드 설치
        with:                               # With를 통한 상세 기능 설정
          node-version: 12                  # Node-version 지정
          registry-url: https://registry.npmjs.org/ # Registy-url npm 레지스트리 연동

      - name: Publish                       # Step - 3
        run: npm publish --access public    # Run을 통한 스크립트 실행 - 배포 공개
        env:                                # Env를 통한 환경변수 지정
          NODE_AUTH_TOKEN: ${{secrets.NPM_AUTH_TOKEN}} # { key : value } 환경변수 생성 - NPM AUTH TOKEN 생성 (NPM 발급)
          
      - name: Notification                  # Step - 4
        uses: 8398a7/action-slack@v2.6.0    # Uses를 통한 외부 설정 가져오기 - 슬랙 알림 기능 참조
        with:                               # With를 통한 상세 기능 설정
          status: ${{ job.status }}         # Status를 통한 Job 상태 확인
          author_name: [slack chatbot name] # Author_name을 통한 슬랙 챗봇 이름 지정
        env:                                # Env를 통한 환경변수 지정
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # { key : value } 환경변수 생성 - GITHUB_TOKEN 생성 (GitHub 발급) 
          SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} # { key : value } 환경변수 생성 - SLACK_WEBHOOK_URL 생성 (Slack 발급) 

create tags

on:                                         # Event 감지
  create:                                   # Create 이벤트
    tags:                                   # Tag 지정 - v*
      - v*

release types

on:                                         # Event 감지
  release:                                  # Release 이벤트
    types:                                  # Types 지정 - created 생성
      - created

pull_request

on:                                         # Event 감지
  pull_request:                             # Pull request 이벤트
    branches:                               # Branch 설정 - master
      - master

현재 적용 중 2020. 11. 27

name: npm publish

on:
  pull_request:
    branches:
      - master

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout source code
        uses: actions/checkout@v2

      - name: Setup nodejs
        uses: actions/setup-node@v1
        with:
          node-version: 12
          registry-url: https://registry.npmjs.org/

      - name: Publish npm
        run: npm publish --access public
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}

      - name: Notification
        uses: 8398a7/action-slack@v2.6.0
        with:
          status: ${{ job.status }}
          author_name: Github Actions
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
@ljlm0402
Copy link
Author

ljlm0402 commented Feb 28, 2020

GitHub Token / Repo Secrets 생성

▾ GitHub - Settings - Developer settings - Personal access tokens
image

▾ GitHub - Your Profile - Repositories - Settings - Secrets
image

@ljlm0402
Copy link
Author

NPM Auth Toekns 생성

▾ npm - Auth Tokens
image

@ljlm0402
Copy link
Author

Slack Webhook URL 생성

▾ slack api - Create New App
image

▾ Incomming Webhooks
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment