iT邦幫忙

2023 iThome 鐵人賽

DAY 17
0
自我挑戰組

Techschool Goalng Backend Master Class 的學習記錄系列 第 17

[Day 17] Setup a workflow for Golang and Postgres in Github Action

  • 分享至 

  • xImage
  •  

Setup a workflow for Golang and Postgres

這是一個詳細的步驟說明,教你如何為 Golang 應用程序設定工作流程,特別是當與 Github Actions 整合時。以下是你提到的步驟摘要:

設定 Github Actions 工作流程:

  1. 透過Github Actions 所提供的Golang Template在 Golang 專案中建立 .github/workflows/ci.yml

  2. 為工作流程設定trigger events,在 push 或 pull_request 到 master branch時。

  3. 創建一個在 ubuntu-latest 上運行的工作。

  4. 以下的Test 會執行失敗,因為我們沒有設定PostgreSQL。

    https://ithelp.ithome.com.tw/upload/images/20231002/201217468HYji4Q3xg.png

# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: ci-test

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

jobs:

  test:
    name: Test
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3

    - name: Set up Go
      uses: actions/setup-go@v4
      with:
        go-version: '1.20'

    - name: Build
      run: go build -v ./...

    - name: Test
      run: go test -v ./...

Setup jobs:

  • 安裝所需的 Go 版本。
  • 從倉庫中取出代碼。
  • 執行測試 (使用 go build & go test)。

Add PostgreSQL:

jobs:
    test:
        name: Test
        runs-on: ubuntu-latest
        # Service containers to run with `container-job`
        services:
            # Label used to access the service container
            postgres:
                # Docker Hub image
                image: postgres:14-alpine
                # Provide the password for postgres
                env:
                    POSTGRES_USER: root
                    POSTGRES_PASSWORD: secret
                    POSTGRES_DB: simple_bank
                ports:
                    - 5432:5432
                # Set health checks to wait until postgres has started
                options: >-
                    --health-cmd pg_isready
                    --health-interval 10s
                    --health-timeout 5s
                    --health-retries 5

Add run migrations step:

  • 在確認 PostgreSQL 正在運行且在執行測試之前,使用 make migrateup 來應用數據庫遷移。
  • 但因為沒有安裝golang-migrate 所以會執行失敗。

Untitled

- name: Run migrations
  run: make migrateup

Install golang-migrate CLI

  • 下載適當的 OS 版本的 golang-migrate CLI
  • 解壓縮並將 migrate 二進制文件移動到 /usr/bin,以便它可以在路徑中使用。
  • 移動時確保重命名二進制文件為 migrate
steps:
            - uses: actions/checkout@v3

            - name: Set up Go
              uses: actions/setup-go@v4
              with:
                  go-version: '1.20'

            - name: Install golang-migrate
              run: |
                  curl -L https://github.com/golang-migrate/migrate/releases/download/v4.15.2/migrate.linux-amd64.tar.gz | tar xvz
                  sudo mv migrate /usr/bin/migrate
                  which migrate

提交並推送工作流程:

  • 一旦設定了工作流程,提交更改並推送到 Github。
  • 檢查你的 Github 倉庫中的 Github Actions 標籤以監控工作流程。

一些建議:

  • 總是查看 Github Actions 日誌,以對故障和了解任何問題或失敗。
  • 確保所有環境變量和 secrets(如果有)都設定正確。
  • 定期查看和更新版本(例如,Go 或 PostgreSQL 版本),以保持工作流程的兼容性和安全性。
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: ci-test

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

jobs:
    test:
        name: Test
        runs-on: ubuntu-latest
        services:
            # Label used to access the service container
            postgres:
                # Docker Hub image
                image:
                    postgres:14-alpine
                    # Provide the password for postgres
                env:
                    POSTGRES_USER: root
                    POSTGRES_PASSWORD: secret
                    POSTGRES_DB: simple_bank
                ports:
                    - 5432:5432
                      # Set health checks to wait until postgres has started
                options: >-
                    --health-cmd pg_isready
                    --health-interval 10s
                    --health-timeout 5s
                    --health-retries 5
        steps:
            - uses: actions/checkout@v3

            - name: Set up Go
              uses: actions/setup-go@v4
              with:
                  go-version: '1.20'

            - name: Install golang-migrate
              run: |
                  curl -L https://github.com/golang-migrate/migrate/releases/download/v4.15.2/migrate.linux-amd64.tar.gz | tar xvz
                  sudo mv migrate /usr/bin/migrate
                  which migrate

            - name: Run migrations
              run: make migrateup

            - name: Test
              run: go test -v ./...

上一篇
[Day 16] Introduction Github Action
下一篇
[Day 18] Implement RESTful in GO using Gin Part 1
系列文
Techschool Goalng Backend Master Class 的學習記錄31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言