Skip to content

Commit adf1f07

Browse files
committed
Updated build workflow for CGO cross compile.
1 parent bee624b commit adf1f07

File tree

2 files changed

+57
-17
lines changed

2 files changed

+57
-17
lines changed

.github/build/compiler_arch.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"386": {"linux": "i686"},
3+
"amd64": {"linux": "x86_64", "darwin": "o64"},
4+
"arm64": {"linux": "aarch64", "darwin": "oa64"}
5+
}

.github/workflows/build.yml

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Build and Publish
1+
name: Build
22

33
on:
44
push:
@@ -37,9 +37,9 @@ jobs:
3737
- name: Set up nodejs
3838
uses: actions/setup-node@v2
3939
with:
40-
node-version: '14.x'
40+
node-version: '16.x'
4141
cache: 'yarn'
42-
cache-dependency-path: '**/yarn.lock'
42+
cache-dependency-path: 'frontend/yarn.lock'
4343

4444
- name: Install dependencies
4545
run: yarn install
@@ -73,7 +73,7 @@ jobs:
7373
with:
7474
files: frontend-dist.tar.gz
7575

76-
build_backend:
76+
build:
7777
runs-on: ubuntu-latest
7878
needs: build_frontend
7979
strategy:
@@ -85,9 +85,12 @@ jobs:
8585
- goarch: 386
8686
goos: darwin
8787
env:
88+
CGO_ENABLED: 1
8889
GOOS: ${{ matrix.goos }}
8990
GOARCH: ${{ matrix.goarch }}
90-
DIST: nginx-ui-${{ matrix.goos }}-${{ matrix.goarch }}
91+
DIST: nginx-ui-${{ matrix.GOOS }}-${{ matrix.GOARCH }}
92+
outputs:
93+
dist: ${{ env.DIST }}
9194
steps:
9295
- name: Checkout
9396
uses: actions/checkout@v2
@@ -107,6 +110,38 @@ jobs:
107110
restore-keys: |
108111
${{ runner.os }}-${{ env.GOOS }}-${{ env.GOARCH }}-go-
109112
113+
- name: Setup compiler environment
114+
id: info
115+
run: |
116+
export _ARCH=$(jq ".[\"$GOARCH\"].$GOOS" -r < .github/build/compiler_arch.json)
117+
echo "::set-output name=ARCH_NAME::$_ARCH"
118+
119+
- name: Install musl cross compiler
120+
if: env.GOOS == 'linux'
121+
uses: Lesmiscore/[email protected]
122+
id: musl
123+
with:
124+
target: ${{ steps.info.outputs.ARCH_NAME }}-linux-musl
125+
126+
- name: Post install musl cross compiler
127+
if: env.GOOS == 'linux'
128+
run: |
129+
echo "PATH=${{ steps.musl.outputs.path }}:$PATH" >> $GITHUB_ENV
130+
echo "CC=${{ steps.info.outputs.ARCH_NAME }}-linux-musl-gcc" >> $GITHUB_ENV
131+
echo "CXX=${{ steps.info.outputs.ARCH_NAME }}-linux-musl-g++" >> $GITHUB_ENV
132+
echo "LD_FLAGS=--extldflags '-static'" >> $GITHUB_ENV
133+
134+
- name: Install darwin cross compiler
135+
if: env.GOOS == 'darwin'
136+
run: |
137+
curl -L https://github.com/Hintay/crossosx/releases/latest/download/crossosx.tar.zst -o crossosx.tar.zst
138+
tar xvaf crossosx.tar.zst
139+
echo "LD_LIBRARY_PATH=$(pwd)/crossosx/lib/" >> $GITHUB_ENV
140+
echo "PATH=$(pwd)/crossosx/bin/:$PATH" >> $GITHUB_ENV
141+
echo "CC=${{ steps.info.outputs.ARCH_NAME }}-clang" >> $GITHUB_ENV
142+
echo "CXX=${{ steps.info.outputs.ARCH_NAME }}-clang++" >> $GITHUB_ENV
143+
echo "LD_FLAGS=-s -w" >> $GITHUB_ENV
144+
110145
- name: Download frontend artifacts
111146
uses: actions/download-artifact@v2
112147
with:
@@ -116,22 +151,22 @@ jobs:
116151
- name: Build
117152
run: |
118153
mkdir -p dist
119-
go build -o dist/nginx-ui -v main.go
154+
go build -ldflags "$LD_FLAGS" -o dist/nginx-ui -v main.go
120155
121156
- name: Archive backend artifacts
122157
uses: actions/upload-artifact@v2
123158
with:
124159
name: ${{ env.DIST }}
125160
path: dist/nginx-ui
126161

127-
- name: Prepare publish
128-
if: github.event_name == 'release'
129-
run: |
130-
cp README*.md ./dist
131-
find dist -printf '%P\n' | tar -C dist --no-recursion -zcvf ${{ env.DIST }}.tar.gz -T -
132-
133-
- name: Publish
134-
uses: softprops/action-gh-release@v1
135-
if: github.event_name == 'release'
136-
with:
137-
files: ${{ env.DIST }}.tar.gz
162+
- name: Prepare publish
163+
if: github.event_name == 'release'
164+
run: |
165+
cp README*.md ./dist
166+
find dist -printf '%P\n' | tar -C dist --no-recursion -zcvf ${{ env.DIST }}.tar.gz -T -
167+
168+
- name: Publish
169+
uses: softprops/action-gh-release@v1
170+
if: github.event_name == 'release'
171+
with:
172+
files: ${{ env.DIST }}.tar.gz

0 commit comments

Comments
 (0)