[1011] 서치라이트 12일차

한별·2024년 11월 27일
0

서치라이트

목록 보기
14/40

Today's Tasks

Learnings and Questions

  • Github Action : 소프트웨어 workflow를 자동화할 수 있도록 도와주는 도구

    • 생성

      • .github/workflows 경로에 .yml 파일로 작성
    • 구성 요소 (workflow > job > step)

      • workflow : 자동화된 프로세스. 여러 job으로 구성된 최상위 개념.

      • job : 여러 step으로 구성

      • step : 여러 명령어로 구성

        • 한 줄이면 그냥 옆에 적고
        • 두 줄이면 옆에 |를 적고 아래에 여러 줄 쓴다.
      • event

        • workflow를 실행하는 조건
        # workflow - Git Action Test
        name: Github-Action-Test
        
        # event
        on:
          push:
            branches: ["main"] # main 브랜치에 push 될 때마다 실행
          workflow_dispatch: # 수동으로 실행
        
        ## job 1 - lint
        jobs:
          lint:
            runs-on: ubuntu-latest # job을 어떤 OS에서 실행할 것인지 명시
            steps:
                # step 1
              - uses: actions/checkout@v4 # uses: 해당 step에서 사용할 액션
                # step 2 - Use Node.js
              - name: Use Node.js
                uses: actions/setup-node@v2
                with:
                  node-version: 20.x
                  cache: "yarn"
              # step 3 - Install dependencies
              - name: Install dependencies
                run: yarn install --frozen-lockfile
              # step 4 - Run lint
              - name: Run lint
                run: yarn lint
        
        ## job 2 - build
          build:
            needs: lint # lint(job) 실행 후에 실행가능한 workflow
            runs-on: ubuntu-latest
            steps:
              - uses: actions/checkout@v4
              - name: Generate Environment Variables File
                run: |
                  echo "VITE_TEST=${{ secrets.VITE_TEST }}" >> .env.local
                  echo "NEXT_PUBLIC_HOSTNAME=${{ secrets.NEXT_PUBLIC_HOSTNAME }}" >> .env.local
              - name: Install Dependencies
                run: yarn install --frozen-lockfile
              - name: Build
                run: yarn build
              # SSH + SCP - ubuntu 서버에 접속하여 빌드 파일을 전송하고 배포된 파일을 교체
              - name: Execute Remote SSH & Deploy
                uses: appleboy/scp-action@master
                with:
                  host: ${{ secrets.REMOTE_SSH_HOST }}
                  username: ${{ secrets.REMOTE_SSH_USERNAME }}
                  key: ${{ secrets.REMOTE_SSH_KEY }}
                  source: ./out
                  target: /home/${{ secrets.REMOTE_SSH_USERNAME }}
        
        ## job 3 - notify slack success
          notify-slack-success:
            runs-on: ubuntu-latest
            needs: [build, lint] # lint, build(jobs) 실행 후에 실행가능한 workflow
            if: success() # 앞의 모든 workflow 실행 결과가 success인 경우에만 실행
            steps:
              - name: Notify Slack on Success
                uses: rtCamp/action-slack-notify@v2
                # env: 해당 job에서 사용할 환경 변수
                env:
                  SLACK_COLOR: "good"
                  SLACK_TITLE: " ✅ [ FE ] 배포 성공"
                  SLACK_MESSAGE: "새로운 버전이 배포되었습니다"
                  SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
        
        ## job 4 - notify slack failure
          notify-slack-failure:
            runs-on: ubuntu-latest
            needs: [build, lint]
            if: failure() # 앞의 모든 workflow 실행 결과가 failure인 경우에만 실행
            steps:
              - name: Notify Slack on Failure
                uses: rtCamp/action-slack-notify@v2
                env:
                  SLACK_COLOR: "danger"
                  SLACK_TITLE: "❌ [ FE ] 배포 실패"
                  SLACK_MESSAGE: "배포에 실패하였습니다"
                  SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}

workflow dispatch

workflow dispatch를 적어주면, Actions 탭에서 실행할 action을 클릭했을 때 다음과 같이 Run workflow 버튼이 생긴다.

이를 이용하면 event가 발생하지 않아도 수동으로 action을 실행할 수 있다.

secrets

Settings > Secrets and variables > Actions 에 들어가면 Repository secrets 를 볼 수 있다.

이것은 Git Actions에서 사용할 환경변수 값을 넣어줄 수 있다. New Repository Secret을 누르고 Name과 값(Secret)을 입력하여 추가하면 등록된다.

ACTIONS_SECRETS 로 추가한 경우, 다음과 같이 값을 사용할 수 있다.

${{ secrets.ACTIONS_SECRETS }}

참고 자료

Github Action에 대한 소개와 사용법 | ggong | velog

Plan for Tomorrow

  • KAN-125 - 무료리스트 신청 페이지 시작
profile
누구나 이해하기 쉽게 글 쓰고 싶은 FE 개발자

0개의 댓글

관련 채용 정보