[OSSCA] Challengers 3주차 과제 Github Action 2 (수정중)

뚜비·2022년 7월 30일
0

2022 OSSCA

목록 보기
7/14
post-thumbnail

2022 오픈소스 컨트리뷰션 아카데미 "NHN Toast Power Platform Connector" 프로젝트에 참여하면서 Challengers 기간 동안 배운 내용을 기록하였습니다.
github-actions-from-scratch를 추가로 공부하면서 기록하였습니다.



✅ 실습

이번 편은 Workflow syntax for GitHub ActionsActions를 참고합시다!

Events & Refactoring

Events – push, pull_request

name: 'My First GitHub Actions'

on:
  push:
    branches: # main branch에서 push가 일어났을 때 발생 
    - main
    tags: # 또한 v* tag일 때 발생
    - 'v*'
  pull_request:
    branches:
    - main

jobs:
  build: # job 이름 
    name: Build Apps
    runs-on: ubuntu-latest

    steps:
    - name: Checkout the repo
      uses: actions/checkout@v2 # 액션 사용

    - name: Setup .NET SDK
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: '6.x'

    - name: Restore NuGet packages
      shell: bash
      
      run: |
        dotnet restore ./api

    - name: Build solution
      shell: bash
      run: |
        dotnet build ./api -c Release

    - name: Create FunctionApp artifact
      shell: bash
      run: |
        dotnet publish ./api -c Release -o ./api/bin/published

    - name: Upload FunctionApp artifact
      uses: actions/upload-artifact@v2
      with:
        name: apiapp
        path: api/bin/published


  release: # job 이름 
    name: Release Apps
    needs: build

    runs-on: ubuntu-latest

    steps:
    - name: Download FunctionApp artifact
      uses: actions/download-artifact@v2
      with:
        name: apiapp
        path: artifacts/api

    - name: Zip FunctionApp artifact
      shell: bash
      run: |
        pushd artifacts/api
        zip -qq -r apiapp.zip .
        popd

        mv artifacts/api/apiapp.zip artifacts/apiapp.zip

    - name: Release FunctionApp artifact to GitHub
      uses: "marvinpinto/action-automatic-releases@latest"
      with:
        repo_token: "${{ secrets.GITHUB_TOKEN }}" # 자동으로 생성되는 토큰임 
        automatic_release_tag: "latest"
        prerelease: false
        files: |
          artifacts/apiapp.zip
    #sㄴㄴㄴ

jobs.<job_id>.steps[*].uses

  • 어떤 Actions는 반드시 with keyword를 사용해야하는 inputs을 요구할 때가 있다.


❌위기❌

자꾸 파라미터인 "automatic_release_tag" 가 setting이 안 된다고 에러가 뜨는 것!!!! 인터넷이 쳐보니.. Automatic Releases에 가서 확인해보니...

automatic_release_tag를 직접 지정해주면 해결이 된다.



workflow_dispatch

name: 'Manual Release'

on:
  workflow_dispatch:
    inputs:
      title:
        type: string
        required: true
        description: Enter the release title

jobs:
  build:
    name: Build Apps

    runs-on: ubuntu-latest

    steps:
    - name: Checkout the repo
      uses: actions/checkout@v2

    - name: Setup .NET SDK
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: '6.x'

    - name: Restore NuGet packages
      shell: bash
      run: |
        dotnet restore ./api

    - name: Build solution
      shell: bash
      run: |
        dotnet build ./api -c Release

    - name: Create FunctionApp artifact
      shell: bash
      run: |
        dotnet publish ./api -c Release -o ./api/bin/published

    - name: Upload FunctionApp artifact
      uses: actions/upload-artifact@v2
      with:
        name: apiapp
        path: api/bin/published

  release:
    name: Release Apps
    needs: build

    runs-on: ubuntu-latest

    steps:
    - name: Download FunctionApp artifact
      uses: actions/download-artifact@v2
      with:
        name: apiapp
        path: artifacts/api

    - name: Zip FunctionApp artifact
      shell: bash
      run: |
        pushd artifacts/api
        zip -qq -r apiapp.zip .
        popd

        mv artifacts/api/apiapp.zip artifacts/apiapp.zip

    - name: Release FunctionApp artifact to GitHub
      uses: "marvinpinto/action-automatic-releases@latest"
      with:
        repo_token: "${{ secrets.GITHUB_TOKEN }}"
        prerelease: false
        automatic_release_tag : latest
        title: ${{ github.event.inputs.title }}
        files: |
          artifacts/apiapp.zip

이 Github actions를 실행시키려면 push시키고 workflow_dispatch라는 events를 발생시켜야 한다.

workflow_call

main

name: 'My First GitHub Actions'

on:
  push:
    branches:
    - main
    tags:
    - 'v*'
  pull_request:
    branches:
    - main

jobs:
  call_build:
    uses: ./.github/workflows/ci.yaml
    with:
      artifact_name: apiapp

  call_release:
    uses: ./.github/workflows/cd.yaml
    needs: call_build
    with:
      title: latest
      artifact_name: apiapp
    secrets: inherit

manual-release

name: 'Manual Release'

on:
  workflow_dispatch:
    inputs:
      title:
        type: string
        required: true
        description: Enter the release title

jobs:
  call_build:
    uses: ./.github/workflows/ci.yaml
    with:
      artifact_name: apiapp

  call_release:
    uses: ./.github/workflows/cd.yaml
    needs: call_build
    with:
      title: ${{ github.event.inputs.title }}
      artifact_name: apiapp
    secrets: inherit

ci

name: 'Continuous Integration'

on:
  workflow_call:
    inputs:
      artifact_name:
        type: string
        required: false
        description: Name of artifact
        default: apiapp

jobs:
  build:
    name: Build Apps

    runs-on: ubuntu-latest

    steps:
    - name: Checkout the repo
      uses: actions/checkout@v2

    - name: Setup .NET SDK
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: '6.x'

    - name: Restore NuGet packages
      shell: bash
      run: |
        dotnet restore ./api

    - name: Build solution
      shell: bash
      run: |
        dotnet build ./api -c Release

    - name: Create FunctionApp artifact
      shell: bash
      run: |
        dotnet publish ./api -c Release -o ./api/bin/published

    - name: Upload FunctionApp artifact
      uses: actions/upload-artifact@v2
      with:
        name: ${{ inputs.artifact_name }}
        path: api/bin/published

cd

name: 'Continuous Deployment'

on:
  workflow_call:
    inputs:
      title:
        type: string
        required: true
        description: Enter the release title
      artifact_name:
        type: string
        required: false
        description: Name of artifact
        default: apiapp
      env:
        type: string
        required: false
        description: Environment name
        default: DEV

jobs:
  release:
    name: Release Apps

    runs-on: ubuntu-latest

    environment: ${{ inputs.env }}

    steps:
    - name: Download FunctionApp artifact
      uses: actions/download-artifact@v2
      with:
        name: ${{ inputs.artifact_name }}
        path: artifacts/api

    - name: Zip FunctionApp artifact
      shell: bash
      run: |
        pushd artifacts/api
        zip -qq -r ${{ inputs.artifact_name }}-${{ inputs.env }}.zip .
        popd

        mv artifacts/api/${{ inputs.artifact_name }}-${{ inputs.env }}.zip artifacts/${{ inputs.artifact_name }}-${{ inputs.env }}.zip

    - name: Release FunctionApp artifact to GitHub
      uses: "marvinpinto/action-automatic-releases@latest"
      with:
        repo_token: "${{ secrets.GITHUB_TOKEN }}"
        prerelease: false
        title: ${{ inputs.title }}
        automatic_release_tag : latest
        files: |
          artifacts/${{ inputs.artifact_name }}-${{ inputs.env }}.zip



Multi-Stage Deployments

Release to DEV/PROD


업로드중..

profile
SW Engineer 꿈나무 / 자의식이 있는 컴퓨터

0개의 댓글