Skip to content

Commit 0334432

Browse files
authored
Build binary file inside docker (#64)
1 parent 07d41ba commit 0334432

File tree

2 files changed

+54
-28
lines changed

2 files changed

+54
-28
lines changed

.github/workflows/build-release.yml

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ on:
1818
env:
1919
APP_NAME: module-core-loraraw
2020
GHCR_IMAGE: ghcr.io/nubeio/module-core-loraraw
21-
PLATFORMS: linux/amd64,linux/arm/v7
21+
PLATFORMS: linux/amd64
2222

2323
jobs:
2424

2525
context:
26-
runs-on: ubuntu-20.04
26+
runs-on: ubuntu-22.04
2727

2828
outputs:
2929
shouldBuild: ${{ steps.context.outputs.decision_build }}
@@ -36,7 +36,7 @@ jobs:
3636
fqn: ${{ env.APP_NAME }}-${{ steps.context.outputs.version }}-${{ steps.context.outputs.shortCommitId }}
3737

3838
steps:
39-
- uses: actions/checkout@v2
39+
- uses: actions/checkout@v4
4040
with:
4141
token: ${{ secrets.NUBEIO_CI_GITHUBPROJECT_TOKEN }}
4242

@@ -58,7 +58,7 @@ jobs:
5858
defaultBranch: master
5959

6060
build:
61-
runs-on: ubuntu-20.04
61+
runs-on: ubuntu-22.04
6262
needs: context
6363
if: needs.context.outputs.shouldBuild == 'true'
6464
env:
@@ -72,37 +72,22 @@ jobs:
7272
-v /home/runner:/var/lib/registry
7373
--name registry
7474
steps:
75-
- uses: actions/checkout@v2
76-
- uses: actions/setup-go@v2
77-
with:
78-
go-version: '^1.16.6'
75+
- uses: actions/checkout@v4
7976

8077
- name: Set current date as env variable
8178
id: date
8279
run: echo "::set-output name=date::$(date +'%Y-%m-%dT%H:%M:%S')"
8380

84-
- name: Edit main.go for environment
85-
env:
86-
VERSION: ${{ needs.context.outputs.version }}
87-
COMMIT: ${{ needs.context.outputs.shortCommitId }}
88-
BUILD_DATE: ${{ steps.date.outputs.date }}
89-
run: |
90-
sed -i -e 's,<version>,'"${VERSION}"',' main.go
91-
sed -i -e 's/<commit>/'"${COMMIT}"'/' main.go
92-
sed -i -e 's/<build_date>/'"${BUILD_DATE}"'/' main.go
93-
94-
- name: Build amd64
81+
- name: Build inside Docker
9582
run: |
96-
git config --global url."https://$GITHUB_TOKEN:x-oauth-basic@github.com/NubeIO".insteadOf "https://github.com/NubeIO"
97-
go mod tidy
98-
go build -o module-core-loraraw-amd64
83+
docker build \
84+
-f Dockerfile \
85+
-t build-image .
9986
100-
- name: Build armv7
101-
if: ${{ needs.context.outputs.isRelease == 'true' }}
102-
run: |
103-
sudo apt-get update -y
104-
sudo apt-get install -y gcc-arm-linux-gnueabihf g++-arm-linux-gnueabihf
105-
env GOOS=linux GOARCH=arm GOARM=7 CGO_ENABLED=1 CC=arm-linux-gnueabihf-gcc CXX=arm-linux-gnueabihf-g++ go build -o module-core-loraraw-armv7
87+
container_id=$(docker create build-image)
88+
docker cp $container_id:/app/module-core-loraraw-amd64 ./module-core-loraraw-amd64
89+
docker cp $container_id:/app/module-core-loraraw-armv7 ./module-core-loraraw-armv7
90+
docker rm $container_id
10691
10792
- name: Zip artifacts
10893
if: ${{ needs.context.outputs.isRelease == 'true' }}

Dockerfile

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Use Ubuntu 20.04 as the base image
2+
FROM ubuntu:20.04 AS builder
3+
4+
# Install Go and necessary dependencies for cross-compilation
5+
RUN apt-get update
6+
RUN apt-get install -y \
7+
gcc \
8+
gcc-x86-64-linux-gnu \
9+
gcc-arm-linux-gnueabihf \
10+
g++-arm-linux-gnueabihf \
11+
wget
12+
13+
# Set the working directory
14+
WORKDIR /app
15+
16+
# Copy the Go project files
17+
COPY . .
18+
19+
# Define build arguments
20+
ARG VERSION
21+
ARG COMMIT
22+
ARG BUILD_DATE
23+
24+
RUN wget https://go.dev/dl/go1.21.13.linux-amd64.tar.gz
25+
RUN tar -C /usr/local -xzf go1.21.13.linux-amd64.tar.gz && \
26+
rm go1.21.13.linux-amd64.tar.gz
27+
28+
# Set Go environment variables
29+
ENV PATH=$PATH:/usr/local/go/bin
30+
ENV GOPATH=/go
31+
32+
# Run Go mod tidy and build for the default platform (amd64)
33+
RUN go mod tidy
34+
RUN env GOOS=linux GOARCH=amd64 CGO_ENABLED=1 \
35+
CC=x86_64-linux-gnu-gcc CXX=x86_64-linux-gnu-g++ \
36+
go build -o module-core-loraraw-amd64
37+
38+
# Build for ARMv7 architecture
39+
RUN env GOOS=linux GOARCH=arm GOARM=7 CGO_ENABLED=1 \
40+
CC=arm-linux-gnueabihf-gcc CXX=arm-linux-gnueabihf-g++ \
41+
go build -o module-core-loraraw-armv7

0 commit comments

Comments
 (0)