-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildspec.yml
99 lines (91 loc) · 3.09 KB
/
buildspec.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
version: 0.2
env:
variables:
# Set Go version and environment variables
GO_VERSION: "1.23"
CGO_ENABLED: "0"
GOOS: "linux"
GOARCH: "amd64"
# GitHub specific variables
GITHUB_REPO: "sanchitrk/goaws"
GITHUB_BRANCH: "main"
exported-variables:
- GO_VERSION
- GOOS
- GOARCH
phases:
install:
commands:
# Update Go version with goenv
- echo "Make sure goenv is up to date"
- cd $HOME/.goenv && git pull --ff-only && cd -
- echo "Install $GO_VERSION..."
- goenv install $GO_VERSION && goenv $GO_VERSION
# Install additional tools needed for GitHub integration
- echo "Installing build dependencies and GitHub tools..."
- go version
- go env
pre_build:
commands:
- echo "Authenticating with GitHub..."
- echo "$GH_TOKEN" | gh auth login --with-token || true
# - git config --global url."https://${GH_TOKEN}@github.com/".insteadOf "https://github.com/"
# - git checkout -f $CODEBUILD_SOURCE_VERSION
# Initialize Git repository
# - git init
# - git remote add origin $CODEBUILD_SOURCE_REPO_URL
# - git fetch
# - git checkout -f $CODEBUILD_SOURCE_VERSION
# Set up GitHub authentication for gh CLI
# - echo "$GITHUB_TOKEN" | gh auth login --with-token
# Verify GitHub repository and branch
- echo "Building from repository $GITHUB_REPO branch $GITHUB_BRANCH"
# Download dependencies and verify modules
- go mod download
- go mod verify
# Run tests and linting
- echo "Running tests and code quality checks..."
- go vet ./...
# Optional: Check if all files are properly formatted
- test -z $(gofmt -l .)
# Optional: Run static code analysis
- if [ -n "$(which golangci-lint)" ]; then golangci-lint run; fi
build:
commands:
# Build the Go binary with version information
- echo "Building Go application from GitHub source..."
- |
GIT_COMMIT=$CODEBUILD_SOURCE_VERSION
GIT_TAG=$(gh release list -R $GITHUB_REPO --limit 1 --json tagName --jq '.[0].tagName' 2>/dev/null || echo $CODEBUILD_SOURCE_VERSION)
- |
go build -v \
-ldflags="-s -w \
-X main.GitCommit=$GIT_COMMIT \
-X main.GitTag=$GIT_TAG \
-X main.BuildTime=$(date -u '+%Y-%m-%d_%H:%M:%S')" \
-o bin/app server.go
# Create deployment package
- zip -j app.zip bin/app
post_build:
commands:
- echo "Build completed on `date`"
# Create GitHub release if building from a tag
- |
if [ "$CODEBUILD_GIT_TAG" != "" ]; then
echo "Creating GitHub release for tag $CODEBUILD_GIT_TAG..."
gh release -R $GITHUB_REPO create "$CODEBUILD_GIT_TAG" \
--title "Release $CODEBUILD_GIT_TAG" \
--notes "Automated release from AWS CodeBuild" \
bin/app app.zip
fi
artifacts:
files:
- bin/app
- app.zip
- appspec.yml
- scripts/**/*
discard-paths: no
cache:
paths:
- /go/pkg/mod/**/* # Cache Go modules
- ~/.cache/go-build/** # Cache build cache