Golang在GitHub Actions中构建失败的问题排查
Golang在GitHub Actions中构建失败的问题排查 我正在尝试使用Go语言构建一个Azure函数应用。如果在本地构建处理程序并部署到函数应用,它可以正常工作。
如果在GitHub Actions中构建,它就会损坏。
两次构建都是在Linux系统上,并且使用相同的Go版本1.20.4。 我还注意到,本地构建的文件大小是24 MB,而在GitHub Actions中是12 MB。
为什么来自GitHub Actions的构建文件无法工作?可能的原因是什么?
以下是我的yml文件:
name: Build and deploy Go project to Azure Function App - myfinapp18-dev
on:
push:
branches:
- dev
paths:
- backend-api/**
- .github/workflows/backend_dev.yml
workflow_dispatch:
env:
AZURE_FUNCTIONAPP_PACKAGE_PATH: backend-api
GO_VERSION: '1.20.4'
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Setup Go version
uses: actions/setup-go@v4
with:
go-version: ${{ env.GO_VERSION }}
cache-dependency-path: backend-api/go.sum
- name: Build
run: |
cd backend-api
go build -o handler handler.go
ls -R
- name: Upload artifact for deployment job
uses: actions/upload-artifact@v3
with:
name: go-app
path: backend-api
deploy:
runs-on: ubuntu-latest
needs: build
environment:
name: 'Production'
url: ${{ steps.deploy-to-function.outputs.webapp-url }}
steps:
- name: Download artifact from build job
uses: actions/download-artifact@v3
with:
name: go-app
path: backend-api
- name: "Login"
uses: azure/login@v1
with:
creds: ${{ secrets.AZURE_FUNCTION_RBAC_CREDENTIALS_DEV }}
- name: 'Deploy to Azure Functions'
uses: Azure/functions-action@v1
id: deploy-to-function
with:
app-name: 'myfinapp18-dev'
slot-name: 'Production'
package: ${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }}
更多关于Golang在GitHub Actions中构建失败的问题排查的实战教程也可以访问 https://www.itying.com/category-94-b0.html
我刚刚将 CGO_ENABLED 设置为 1,默认情况下它是 0。现在可以正常工作了。
更多关于Golang在GitHub Actions中构建失败的问题排查的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
构建文件大小差异表明GitHub Actions中的构建可能缺少必要的依赖或使用了不同的构建参数。以下是可能的原因和排查步骤:
1. 检查构建环境差异
本地和GitHub Actions的构建环境可能存在差异:
// 在构建前添加环境检查
name: Build
run: |
cd backend-api
echo "Go version:"
go version
echo "GOOS: $GOOS"
echo "GOARCH: $GOARCH"
echo "CGO_ENABLED: $CGO_ENABLED"
echo "GOPATH: $GOPATH"
go env
go build -o handler handler.go
ls -lh handler
2. 确保静态链接依赖
Azure Functions需要静态链接的二进制文件:
// 修改构建命令
name: Build
run: |
cd backend-api
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \
-a \
-ldflags="-s -w" \
-o handler \
handler.go
# 检查文件类型
file handler
# 检查链接的库
ldd handler 2>/dev/null || echo "静态链接"
3. 检查模块依赖
确保所有依赖正确下载:
// 在构建前添加依赖下载
name: Build
run: |
cd backend-api
# 清理模块缓存
go clean -modcache
# 下载所有依赖
go mod download
# 验证依赖
go mod verify
# 使用vendor模式确保一致性
go mod vendor
# 从vendor构建
go build -mod=vendor -o handler handler.go
4. 完整的工作流示例
name: Build and deploy Go project to Azure Function App
on:
push:
branches: [dev]
paths: ['backend-api/**']
env:
AZURE_FUNCTIONAPP_PACKAGE_PATH: backend-api
GO_VERSION: '1.20.4'
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: ${{ env.GO_VERSION }}
cache-dependency-path: backend-api/go.sum
- name: Build Go Binary
run: |
cd backend-api
echo "构建环境:"
go version
go env
# 清理并重新构建
rm -f handler
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
go build \
-trimpath \
-ldflags="-s -w -extldflags '-static'" \
-o handler \
handler.go
echo "构建结果:"
ls -lh handler
file handler
# 检查是否为静态链接
if ldd handler 2>&1 | grep -q "not a dynamic executable"; then
echo "✓ 静态链接"
else
echo "✗ 动态链接"
ldd handler
fi
- name: Test Binary
run: |
cd backend-api
# 简单测试二进制文件是否能运行
if [ -f handler ]; then
echo "测试二进制文件..."
./handler --help 2>&1 || true
fi
- name: Upload artifact
uses: actions/upload-artifact@v3
with:
name: go-app
path: |
backend-api/handler
backend-api/host.json
backend-api/local.settings.json
backend-api/function.json
5. 检查Azure Functions特定要求
确保handler文件符合Azure Functions的预期:
// 在handler.go中添加构建约束
//go:build linux
// +build linux
package main
import (
"context"
"fmt"
"log"
"os"
)
func main() {
// Azure Functions需要这个入口点
fmt.Println("Azure Function handler initialized")
// 检查环境变量
if port := os.Getenv("FUNCTIONS_CUSTOMHANDLER_PORT"); port != "" {
log.Printf("Custom handler port: %s", port)
}
}
6. 调试部署问题
在部署步骤中添加调试信息:
- name: 'Deploy to Azure Functions'
uses: Azure/functions-action@v1
id: deploy-to-function
with:
app-name: 'myfinapp18-dev'
slot-name: 'Production'
package: ${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }}
env:
AZURE_HTTP_USER_AGENT: 'github-actions'
AZURE_HTTP_LOG_LEVEL: 'debug'
文件大小差异通常是由于动态链接与静态链接、调试信息剥离或依赖包含不完整导致的。使用CGO_ENABLED=0和静态链接标志可以确保构建一致性。



