본문 바로가기

Dev

Github Actions 101

Github Actions 101

  • Github 에서 제공하는 Workflow 툴
  • GitHub-hosted Runner or Self-Hosted Runner 에서 실행
  • Actions 탭을 통해서 Template을 선택하고 Yaml 파일로 Task 내용을 기술
    • .github/workflows 디렉토리 밑에 위치

Contents

Runner 종류

Actions Basic

Workflow

name: CI                            # workflow 이름 

on:                                 # Triggers Event 
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  build:                            # Single job name
    runs-on: ubuntu-latest          # virtual machine

    steps:
    - uses: actions/checkout@v2

    - name: Run a one-line script
      run: echo Hello, world!

    - name: Run a multi-line script
      run: |
        echo Add other actions to build,
        echo test, and deploy your project.

Actions 예제

Docker Build & Registry Push

  • ubuntu-latest 이미지에는 Docker Daemon 활성화됨
  • secrets 변수 : [Settings] - [Secrets] 에서 변수 세팅 (DOCKERHUB_PASS)
    • ${{ secrets.DOCKERHUB_PASS }}
name: Docker Image CI

on:
  push:
    branches: [ master ]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2

    - name: Build the Docker image
      run: docker build . --tag cdecl/gcc-boost
    - name: docker login
      run: echo '${{ secrets.DOCKERHUB_PASS }}' | docker login -u cdecl --password-stdin
    - name: docker push
      run: docker push cdecl/gcc-boost

MSBuild & Nuget

  • Windows 서버 이미지 MSBuild 기반 빌드
  • msbuild 및 nuget setup
    • uses: nuget/setup-nuget@v1
      • uses: microsoft/setup-msbuild@v1
  • name 단위의 run task시 current direcotry reset
name: C/C++ CI

on:
  push:
    branches: [ master ]

jobs:
  build:
    runs-on: windows-2019

    steps:
    - uses: actions/checkout@v2
    - uses: nuget/setup-nuget@v1
    - uses: microsoft/setup-msbuild@v1

    - name: nuget restore 
      run: | 
        cd src 
        nuget restore asb.sln

    - name: build
      run: |
        cd src 
        msbuild asb.vcxproj /p:configuration=release /p:platform=x64

Container Image 활용

  • docker container (cdecl/gcc-boost)를 활용 Task 실행
    • container: cdecl/gcc-boost
name: C/C++ CI

on:
  push:
    branches: [ master ]

jobs:
  build:
    runs-on: ubuntu-latest
    container: cdecl/gcc-boost

    steps:
    - uses: actions/checkout@v2

    - name: check 
      run: | 
        g++ --version

    - name: make 
      run: |
        cd src 
        make

Go build 및 Docker image registry

  • Tag push only : push tags : '.'
  • CGO_ENABLED=0 : libc dynamic linked binary 비활성화를 위해
name: Go

on:
  push:
    tags: '*.*'

jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    steps:

    - name: Set up Go 1.x
      uses: actions/setup-go@v2
      with:
        go-version: ^1.13
      id: go

    - name: Check out code into the Go module directory
      uses: actions/checkout@v2

    - name: Get dependencies
      run: |
        go get -v -t -d ./...

    - name: Build
      run: CGO_ENABLED=0 go build -v .

    - name: Build the Docker image
      run: docker build . --tag cdecl/go-sitecheck

    - name: docker login & push
      run: | 
        echo '${{ secrets.DOCKERHUB_PASS }}' | docker login -u cdecl --password-stdin
    docker push cdecl/go-sitecheck

Persisting workflow data using artifacts

  • Artifacts 활용 지속적인 workflow 데이터 관리
    • actions/upload-artifact@v1 / actions/download-artifact@v1
  • 순서보장을 위해 needs 사용 : 병렬수행
name: artifact test

on:
  push:
    branches: [ master ]

jobs:
  build-stage:
    runs-on: ubuntu-latest
    container: gcc
    steps:
    - uses: actions/checkout@v2
    - name: check
      run: |
        mkdir output
        cat /etc/*-release > output/release.txt
    - name: upload artifact
      uses: actions/upload-artifact@v1
      with:
        name: output
        path: output

  test-stage:
    needs: build-stage
    runs-on: windows-latest
    steps:
    - uses: actions/checkout@v2
    - name:  download artifact
      uses: actions/download-artifact@v1
      with:
        name: output
    - name: check
      run: |
        dir 
        type output\release.txt

Create release, upload asset file

name: C/C++ CI

on:
  push:
    paths:
    - '.github/workflows/ccpp.yml'

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - name: file 
      run: |
        mkdir release
        cat 'data ' > release/data.txt

    - name: upload artifact
      uses: actions/upload-artifact@v1
      with:
        name: release-linux
        path: release

  build-release:
    needs: 
      - build
    runs-on: ubuntu-latest

    steps: 
    - name: download artifact
      uses: actions/download-artifact@v1
      with:
        name: release-linux      
    - name: zip
      run: |
        zip -r release.zip release

    - name: create release
      id: create_release
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}  
      uses: actions/create-release@v1
      with:
        tag_name: 'v1.0'
        release_name: Release v1.0
        draft: false
        prerelease: false

    - name: upload release asset 
      uses: actions/upload-release-asset@v1
      env:
        GITHUB_TOKEN: ${{ secrets.TOKEN }}
      with:
        upload_url: ${{ steps.create_release.outputs.upload_url }}  # create_release 의 결과 
        asset_path: ./release.zip
        asset_name: release.zip
        asset_content_type: application/zip

'Dev' 카테고리의 다른 글

Kubernetes 101  (0) 2020.10.29
Docker Swarm 101  (0) 2020.10.29
Github Actions 101  (0) 2020.10.29
MinIO 101  (0) 2020.10.29
Visual Studio Code, Python 개발환경 구축  (0) 2020.01.27
OS 커널 관련된 글들..  (0) 2008.09.01