1+ name : CI/CD Pipeline
2+
3+ on :
4+ push :
5+ branches : [ master ]
6+ pull_request :
7+ branches : [ master ]
8+ release :
9+ types : [ published ]
10+
11+ jobs :
12+ test :
13+ runs-on : ubuntu-latest
14+ strategy :
15+ matrix :
16+ go-version : [1.21, 1.22]
17+
18+ steps :
19+ - uses : actions/checkout@v4
20+
21+ - name : Set up Go
22+ uses : actions/setup-go@v4
23+ with :
24+ go-version : ${{ matrix.go-version }}
25+
26+ - name : Cache Go modules
27+ uses : actions/cache@v3
28+ with :
29+ path : ~/go/pkg/mod
30+ key : ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
31+ restore-keys : |
32+ ${{ runner.os }}-go-
33+
34+ - name : Install dependencies
35+ run : go mod download
36+
37+ - name : Verify dependencies
38+ run : go mod verify
39+
40+ - name : Run tests
41+ run : go test -v -race -coverprofile=coverage.out ./...
42+
43+ - name : Upload coverage to Codecov
44+ uses : codecov/codecov-action@v3
45+ with :
46+ file : ./coverage.out
47+
48+ - name : Run go vet
49+ run : go vet ./...
50+
51+ - name : Run staticcheck
52+ uses : dominikh/staticcheck-action@v1.3.0
53+ with :
54+ version : " 2023.1.6"
55+
56+ build :
57+ needs : test
58+ runs-on : ubuntu-latest
59+ strategy :
60+ matrix :
61+ os : [linux, windows, darwin]
62+ arch : [amd64, arm64]
63+ exclude :
64+ - os : windows
65+ arch : arm64
66+
67+ steps :
68+ - uses : actions/checkout@v4
69+
70+ - name : Set up Go
71+ uses : actions/setup-go@v4
72+ with :
73+ go-version : 1.21
74+
75+ - name : Cache Go modules
76+ uses : actions/cache@v3
77+ with :
78+ path : ~/go/pkg/mod
79+ key : ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
80+ restore-keys : |
81+ ${{ runner.os }}-go-
82+
83+ - name : Install dependencies
84+ run : go mod download
85+
86+ - name : Build server
87+ run : |
88+ CGO_ENABLED=1 GOOS=${{ matrix.os }} GOARCH=${{ matrix.arch }} go build -ldflags="-s -w" -o server-${{ matrix.os }}-${{ matrix.arch }}${{ matrix.os == 'windows' && '.exe' || '' }} .
89+
90+ - name : Build CLI
91+ run : |
92+ CGO_ENABLED=1 GOOS=${{ matrix.os }} GOARCH=${{ matrix.arch }} go build -ldflags="-s -w" -o uploader-${{ matrix.os }}-${{ matrix.arch }}${{ matrix.os == 'windows' && '.exe' || '' }} ./cmd/cli
93+
94+ - name : Upload artifacts
95+ uses : actions/upload-artifact@v3
96+ with :
97+ name : binaries-${{ matrix.os }}-${{ matrix.arch }}
98+ path : |
99+ server-${{ matrix.os }}-${{ matrix.arch }}*
100+ uploader-${{ matrix.os }}-${{ matrix.arch }}*
101+
102+ docker :
103+ needs : test
104+ runs-on : ubuntu-latest
105+ if : github.ref == 'refs/heads/main' || github.event_name == 'release'
106+
107+ steps :
108+ - uses : actions/checkout@v4
109+
110+ - name : Set up Docker Buildx
111+ uses : docker/setup-buildx-action@v3
112+
113+ - name : Login to Docker Hub
114+ uses : docker/login-action@v3
115+ with :
116+ username : ${{ secrets.DOCKERHUB_USERNAME }}
117+ password : ${{ secrets.DOCKERHUB_TOKEN }}
118+
119+ - name : Extract metadata
120+ id : meta
121+ uses : docker/metadata-action@v5
122+ with :
123+ images : ${{ secrets.DOCKERHUB_USERNAME }}/fileuploader
124+ tags : |
125+ type=ref,event=branch
126+ type=ref,event=pr
127+ type=semver,pattern={{version}}
128+ type=semver,pattern={{major}}.{{minor}}
129+ type=raw,value=latest,enable={{is_default_branch}}
130+
131+ - name : Build and push
132+ uses : docker/build-push-action@v5
133+ with :
134+ context : .
135+ push : true
136+ tags : ${{ steps.meta.outputs.tags }}
137+ labels : ${{ steps.meta.outputs.labels }}
138+ cache-from : type=gha
139+ cache-to : type=gha,mode=max
140+ platforms : linux/amd64,linux/arm64
141+
142+ release :
143+ needs : [test, build]
144+ runs-on : ubuntu-latest
145+ if : github.event_name == 'release'
146+
147+ steps :
148+ - uses : actions/checkout@v4
149+
150+ - name : Download all artifacts
151+ uses : actions/download-artifact@v3
152+
153+ - name : Create release package
154+ run : |
155+ mkdir -p release
156+ for dir in binaries-*/; do
157+ cp -r "$dir"* release/
158+ done
159+ cp README.md docker-compose.yml Dockerfile release/
160+ cd release
161+ tar -czf ../fileuploader-${{ github.event.release.tag_name }}.tar.gz .
162+ cd ..
163+
164+ - name : Upload release assets
165+ uses : actions/upload-release-asset@v1
166+ env :
167+ GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
168+ with :
169+ upload_url : ${{ github.event.release.upload_url }}
170+ asset_path : ./fileuploader-${{ github.event.release.tag_name }}.tar.gz
171+ asset_name : fileuploader-${{ github.event.release.tag_name }}.tar.gz
172+ asset_content_type : application/gzip
173+
174+ - name : Upload individual binaries
175+ run : |
176+ for file in release/server-* release/uploader-*; do
177+ if [ -f "$file" ]; then
178+ filename=$(basename "$file")
179+ curl \
180+ -X POST \
181+ -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
182+ -H "Content-Type: application/octet-stream" \
183+ --data-binary @"$file" \
184+ "${{ github.event.release.upload_url }}?name=$filename"
185+ fi
186+ done
0 commit comments