-
-
Notifications
You must be signed in to change notification settings - Fork 0
147 lines (126 loc) · 4.79 KB
/
build-develop.yml
File metadata and controls
147 lines (126 loc) · 4.79 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
name: Develop
permissions:
contents: read
packages: write
on:
workflow_dispatch:
push:
branches:
- master
paths:
- '**/*.go'
- 'go.mod'
- 'go.sum'
- 'Dockerfile'
pull_request:
branches:
- master
paths:
- '**/*.go'
- 'go.mod'
- 'go.sum'
- 'Dockerfile'
jobs:
test-and-build:
name: 'Test and Build'
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v6
- name: Set up Go
uses: actions/setup-go@v6
with:
go-version: '1.25'
- name: Get Project Version for User-Agent and Docker Tags
id: get_version
run: |
TAG_NAME=${GITHUB_REF_NAME}
COMMIT_SHA=${GITHUB_SHA}
SHORT_SHA=${COMMIT_SHA:0:7}
# Initialize variables
BUILD_VERSION="dev"
GIT_COMMIT=$SHORT_SHA
IS_SEMVER_TAG="false"
PUSH_DOCKER="false" # Flag to control if Docker images should be pushed at all
# Logic for determining BUILD_VERSION and PUSH_DOCKER
if [[ "${GITHUB_REF}" == "refs/heads/master" ]]; then
BUILD_VERSION="testing"
PUSH_DOCKER="true"
elif [[ "${GITHUB_REF}" == refs/tags/v* && "${TAG_NAME}" =~ ^v([0-9]+\.){2}[0-9]+$ ]]; then
BUILD_VERSION=$TAG_NAME
IS_SEMVER_TAG="true"
PUSH_DOCKER="true"
fi
# Export as both env vars and step outputs for later steps
echo "BUILD_VERSION=$BUILD_VERSION" >> $GITHUB_ENV
echo "GIT_COMMIT=$GIT_COMMIT" >> $GITHUB_ENV
echo "IS_SEMVER_TAG=$IS_SEMVER_TAG" >> $GITHUB_ENV
echo "PUSH_DOCKER=$PUSH_DOCKER" >> $GITHUB_ENV
echo "BUILD_VERSION=$BUILD_VERSION" >> $GITHUB_OUTPUT
echo "GIT_COMMIT=$GIT_COMMIT" >> $GITHUB_OUTPUT
echo "IS_SEMVER_TAG=$IS_SEMVER_TAG" >> $GITHUB_OUTPUT
echo "PUSH_DOCKER=$PUSH_DOCKER" >> $GITHUB_OUTPUT
echo "User-Agent Build Version: $BUILD_VERSION ($GIT_COMMIT)"
- name: Build application (dry run)
run: |
go build -v \
-ldflags "-X 'github.com/user00265/dxclustergoapi/version.BuildVersion=${{ steps.get_version.outputs.BUILD_VERSION }}' \
-X 'github.com/user00265/dxclustergoapi/version.GitCommit=${{ steps.get_version.outputs.GIT_COMMIT }}'" \
./
- name: Run unit tests
run: go test -v -short ./...
- name: Run integration tests
run: go test -v -tags=integration ./...
# Build and Push Docker images (only if PUSH_DOCKER is true)
- name: Set up Docker Buildx
if: steps.get_version.outputs.PUSH_DOCKER == 'true'
uses: docker/setup-buildx-action@v4
with:
version: latest
- name: Set up QEMU (multi-arch emulation)
if: steps.get_version.outputs.PUSH_DOCKER == 'true'
uses: docker/setup-qemu-action@v4
- name: Login to Docker Hub
if: steps.get_version.outputs.PUSH_DOCKER == 'true' && github.event_name != 'pull_request'
uses: docker/login-action@v4
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Login to GitHub Container Registry
if: steps.get_version.outputs.PUSH_DOCKER == 'true' && github.event_name != 'pull_request'
uses: docker/login-action@v4
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Prepare Docker Hub tags
id: docker_hub_tags
if: steps.get_version.outputs.PUSH_DOCKER == 'true'
run: |
TAGS="user00265/dxclustergoapi:${{ steps.get_version.outputs.BUILD_VERSION }}"
if [ "${{ steps.get_version.outputs.IS_SEMVER_TAG }}" == "true" ]; then
TAGS="${TAGS},user00265/dxclustergoapi:latest"
fi
echo "tags=${TAGS}" >> $GITHUB_OUTPUT
- name: Prepare GitHub Packages tags
id: ghcr_tags
if: steps.get_version.outputs.PUSH_DOCKER == 'true'
run: |
TAGS="ghcr.io/user00265/dxclustergoapi:${{ steps.get_version.outputs.BUILD_VERSION }}"
if [ "${{ steps.get_version.outputs.IS_SEMVER_TAG }}" == "true" ]; then
TAGS="${TAGS},ghcr.io/user00265/dxclustergoapi:latest"
fi
echo "tags=${TAGS}" >> $GITHUB_OUTPUT
- name: Build and Push Docker images (multi-arch)
if: steps.get_version.outputs.PUSH_DOCKER == 'true' && github.event_name != 'pull_request'
uses: docker/build-push-action@v7
with:
context: .
push: true
platforms: linux/amd64,linux/arm64
tags: ${{ steps.docker_hub_tags.outputs.tags }},${{ steps.ghcr_tags.outputs.tags }}
cache-from: type=gha
cache-to: type=gha,mode=max
build-args: |
BUILD_VERSION=${{ steps.get_version.outputs.BUILD_VERSION }}
GIT_COMMIT=${{ steps.get_version.outputs.GIT_COMMIT }}