這是一個詳細的步驟說明,教你如何為 Golang 應用程序設定工作流程,特別是當與 Github Actions 整合時。以下是你提到的步驟摘要:
透過Github Actions 所提供的Golang Template在 Golang 專案中建立 .github/workflows/ci.yml
。
為工作流程設定trigger events,在 push 或 pull_request 到 master branch時。
創建一個在 ubuntu-latest
上運行的工作。
以下的Test 會執行失敗
,因為我們沒有設定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
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 ./...
go build
& go test
)。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
make migrateup
來應用數據庫遷移。golang-migrate
所以會執行失敗。- name: Run migrations
run: make migrateup
golang-migrate
CLIgolang-migrate CLI
。/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
# 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 ./...