🔥
Caster 開發日誌
  • Java
    • JVM Performance Tool
      • Java Debug Wire Protocol (JDWP) 的詳細介紹
      • JConsole 詳細介紹
    • Spring Boot
      • Spring Security
      • Spring Boot Admin
      • Spring Event
      • Spring AOP
      • Spring Boot JUnit 5
      • Apache Dubbo
    • Reflect 應用
    • ELK + F 建構
    • Socket.IO
    • OCR - 光學字元辨識
    • 讀取JAR resource文件
    • LocalTime & MySQL時間精度
    • Gradle multi module
    • MyBatis-Plus
    • Java Date operation
    • Java IP to Long
    • Apache Commons lang3 應用
      • Function 應用
    • Cloud Platform
      • Amazon S3
        • SDK V1
          • Bucket
        • SDK V2
          • Bucket
      • Google Cloud Platform
      • Azure Cloud
        • Storage
      • OVHcloud
        • Config
    • SSL/TLS工具
    • Util 工具
      • Jackson Json工具
      • Charles應用
      • JMeter – Performing Distributed Load Testing with Docker
    • Redis
      • Stream
      • Redisson 分布式鎖機制
      • Create Redis Cluster Using Docker
      • List Operations
    • Java 8
      • method & constructor Reference
      • CompletableFuture
      • FunctionInterface
      • Stream 應用
      • 繁簡轉換 - 簡易調整
    • MySQL
      • 建立測試用 流水號Table
      • SQL 效能調校 - Explain
      • SQL 效能調校 - Partition
      • 排程 - Event
    • Apache ShardingSphere
  • Kubernetes
    • 初入江湖(K8S)
    • 零中斷服務滾動更新
    • Kubernetes DNS
    • Ingress & Ingress Controller 教學
    • Ingress TLS Easy setup
  • 指令集
  • Telegram
  • SourceTree
    • 踩坑紀錄(ㄧ) - Git Flow
    • 踩坑紀錄(二) - 修改檔名
  • 專案統計
    • Robot
    • Recharge
  • GitHub
    • Actions
  • GitLab
    • 介紹 GitLab
    • 使用 Docker 自架 GitLab
    • 簡介 GitLab CI/CD
      • GitLab Runner 詳細介紹與設定方式
Powered by GitBook
On this page
  • CI (Continuous Integration)
  • CD (Continuous Deployment)
  • Github Actions 簡述
  • 實作測試
  • 究竟預設執行了哪些事情:
  • 參考網站:
  1. GitHub

Actions

利用 Github Actions 做一個簡單的 CI/CD

PreviousGitHubNextGitLab

Last updated 1 year ago

Github Actions 是 Github 在 2019 年才推出的 CI/CD 服務,相較於老牌的一些 CI/CD 要年輕了許多,相對的可能支援度也還沒有到很全面,但對於想在 Github 上小專案做簡單 CI/CD, 相較於 Jenkins 還要安裝到一台 Server 上,Github Actions 可以直接在 Github 上操作,應該會是最方便又比較快速上手的工具了。

何謂 CI/CD? 這也不是一個新名詞了,全名是 Continuous Integration and Continuous Deployment,也就是持續整合與持續部署。簡單來說,就是把一些日常的建置環境、測試、build,一直到部署都交給自動化的工具,目的除了加快開發流程之外,也可以提早發現問題。

身為一個軟體工程師,為了提高日常工作效率,不把時間浪費在不需要的事情上,不論前端後端甚至DevOps工程師,CI/CD 都是在開發過程中非常重要的一環。

CI (Continuous Integration)

CI 顧名思義為「持續整合」,在每次的 Commit and Push 時,經過各項的測試,與完成後的 build code,讓每一次的小改變都可以馬上經過自動化的測試驗證,藉此快速幫助開發者找出潛在或是被忽略的問題,並且產生可靠性的 build 以供正式版本使用。

CD (Continuous Deployment)

CD 指的是「持續部署」,透過自動化流程將 CI 產出的 build,部署到我們服務的環境或是伺服器上,減少花費人工手動 deploy 的時間,也會透過一些 moniter 的工具來確保我們的服務有正常在運作。

Github Actions 簡述

對於 Github Actions 有幾個重要的核心概念:

Actions/commands

Actions/commands 可以說是在 Github Actions 中的最小單位,可以用來組合成各種的 steps 來建立你要自動化的 jobs 。

Step

由多個 action 或是 command 組合成的 step ,用來定義每一個自動化流程的步驟。

Job

多個 step 組成 job ,透過 job 定義不同 scope 的任務,比如說可以分為測試的 job 與 build code 的 job 來執行,執行順序會根據你寫在 yaml 檔案中的順序來執行。

Workflow

Workflow 是整個自動化的流程,一個自動化流程可能包含多個 job ,就要看你的自動化流程怎麼設計了。

實作測試

這邊實作就以最基本的Java spring boot gradle專案作為範例。

究竟預設執行了哪些事情:

on

  1. push:此段是指當 push 到 master 這個 branch 時會觸發

  2. pull-request:當 master 有出現拉取請求觸發

jobs

  1. 此jobs 命名為 build

  2. runs-on:執行一個 ubuntu 的環境

  3. 設定JDK 11

  4. 使用Gradle build

最後附上實際專案應用的yml

當main 分支 push時觸發Actions,最後 SSH連線到server 執行 command

name: Java CI with Gradle

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Set up JDK 11
        uses: actions/setup-java@v2
        with:
          java-version: '11'
          distribution: 'temurin'
      - name: Build with Gradle
        uses: gradle/gradle-build-action@4137be6a8bf7d7133955359dbd952c0ca73b1021
        with:
          # 先暫時跳過測試 理論上應該要執行junit test 才可發版
          arguments: build -x test
      - uses: actions/checkout@v2
      - name: deploy to vm
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.STOCK_HOST }}
          username: ${{ secrets.STOCK_USERNAME }}
          key: ${{ secrets.STOCK_SSHKEY }}
          script: |
            cd StockDecisionSupportPrj/
            git pull
            sudo ./gradlew build
            sudo sh service.sh restart dev '-Xms1g -Xmx1g -Djava.net.preferIPv4Stack=true' /home/opc/StockDecisionSupportPrj/build/libs/stock-decision-support /home/opc/StockDecisionSupportPrj/log/startup.log

參考網站:

實際個別參數說明可參閱官方文件

https://docs.github.com/en/actions
https://reurl.cc/3jyx4l
https://newideas.coderbridge.io/2021/03/13/github-action-to-linode-vm/
https://reurl.cc/l9nDNY
from: Github
進到專案內點選Actions,這邊可以選擇建議或是自行搜尋適合當前專案的模板在進行修改。
預設就會先幫你用上最基本的Action並且建立yml