From fc648454df6dbec3caea8efb381fe57dac87ef6c Mon Sep 17 00:00:00 2001 From: Nicola Murino Date: Thu, 8 Sep 2022 13:41:00 +0200 Subject: [PATCH] CI: use Docker to build x86_64 Linux packages therefore Linux packages are compiled with Docker for all supported architectures Signed-off-by: Nicola Murino --- .github/workflows/development.yml | 50 ++++++++++++++++++++----------- .github/workflows/release.yml | 41 ++++++++++++++----------- Dockerfile | 2 +- Dockerfile.alpine | 2 +- Dockerfile.distroless | 2 +- docs/build-from-source.md | 2 +- 6 files changed, 60 insertions(+), 39 deletions(-) diff --git a/.github/workflows/development.yml b/.github/workflows/development.yml index 01cfe0da..0b3b8689 100644 --- a/.github/workflows/development.yml +++ b/.github/workflows/development.yml @@ -32,7 +32,7 @@ jobs: - name: Build for Linux/macOS x86_64 if: startsWith(matrix.os, 'windows-') != true run: | - go build -trimpath -ldflags "-s -w -X github.com/drakkan/sftpgo/v2/version.commit=`git describe --always --dirty` -X github.com/drakkan/sftpgo/v2/version.date=`date -u +%FT%TZ`" -o sftpgo + go build -trimpath -ldflags "-s -w -X github.com/drakkan/sftpgo/v2/version.commit=`git describe --always --abbrev=8 --dirty` -X github.com/drakkan/sftpgo/v2/version.date=`date -u +%FT%TZ`" -o sftpgo cd tests/eventsearcher go build -trimpath -ldflags "-s -w" -o eventsearcher cd - @@ -42,12 +42,12 @@ jobs: - name: Build for macOS arm64 if: startsWith(matrix.os, 'macos-') == true - run: CGO_ENABLED=1 GOOS=darwin GOARCH=arm64 SDKROOT=$(xcrun --sdk macosx --show-sdk-path) go build -trimpath -ldflags "-s -w -X github.com/drakkan/sftpgo/v2/version.commit=`git describe --always --dirty` -X github.com/drakkan/sftpgo/v2/version.date=`date -u +%FT%TZ`" -o sftpgo_arm64 + run: CGO_ENABLED=1 GOOS=darwin GOARCH=arm64 SDKROOT=$(xcrun --sdk macosx --show-sdk-path) go build -trimpath -ldflags "-s -w -X github.com/drakkan/sftpgo/v2/version.commit=`git describe --always --abbrev=8 --dirty` -X github.com/drakkan/sftpgo/v2/version.date=`date -u +%FT%TZ`" -o sftpgo_arm64 - name: Build for Windows if: startsWith(matrix.os, 'windows-') run: | - $GIT_COMMIT = (git describe --always --dirty) | Out-String + $GIT_COMMIT = (git describe --always --abbrev=8 --dirty) | Out-String $DATE_TIME = ([datetime]::Now.ToUniversalTime().toString("yyyy-MM-ddTHH:mm:ssZ")) | Out-String $LATEST_TAG = ((git describe --tags $(git rev-list --tags --max-count=1)) | Out-String).Trim() $REV_LIST=$LATEST_TAG+"..HEAD" @@ -367,12 +367,13 @@ jobs: build-linux-packages: name: Build Linux packages - runs-on: ubuntu-18.04 + runs-on: ubuntu-latest strategy: matrix: include: - arch: amd64 - go: 1.18 + distro: ubuntu:18.04 + go: latest go-arch: amd64 - arch: aarch64 distro: ubuntu18.04 @@ -390,16 +391,36 @@ jobs: - uses: actions/checkout@v3 with: fetch-depth: 0 - - name: Set up Go - if: ${{ matrix.arch == 'amd64' }} - uses: actions/setup-go@v3 - with: - go-version: ${{ matrix.go }} + + - name: Get commit SHA + id: get_commit + run: echo ::set-output name=COMMIT::${GITHUB_SHA::8} + shell: bash - name: Build on amd64 if: ${{ matrix.arch == 'amd64' }} run: | - go build -trimpath -ldflags "-s -w -X github.com/drakkan/sftpgo/v2/version.commit=`git describe --always --dirty` -X github.com/drakkan/sftpgo/v2/version.date=`date -u +%FT%TZ`" -o sftpgo + echo '#!/bin/bash' > build.sh + echo '' >> build.sh + echo 'set -e' >> build.sh + echo 'apt-get update -q -y' >> build.sh + echo 'apt-get install -q -y curl gcc' >> build.sh + if [ ${{ matrix.go }} == 'latest' ] + then + echo 'GO_VERSION=$(curl -L https://go.dev/VERSION?m=text)' >> build.sh + else + echo 'GO_VERSION=${{ matrix.go }}' >> build.sh + fi + echo 'GO_DOWNLOAD_ARCH=${{ matrix.go-arch }}' >> build.sh + echo 'curl --retry 5 --retry-delay 2 --connect-timeout 10 -o go.tar.gz -L https://go.dev/dl/${GO_VERSION}.linux-${GO_DOWNLOAD_ARCH}.tar.gz' >> build.sh + echo 'tar -C /usr/local -xzf go.tar.gz' >> build.sh + echo 'export PATH=$PATH:/usr/local/go/bin' >> build.sh + echo 'go version' >> build.sh + echo 'cd /usr/local/src' >> build.sh + echo 'go build -buildvcs=false -trimpath -ldflags "-s -w -X github.com/drakkan/sftpgo/v2/version.commit=${{ steps.get_commit.outputs.COMMIT }} -X github.com/drakkan/sftpgo/v2/version.date=`date -u +%FT%TZ`" -o sftpgo' >> build.sh + + chmod 755 build.sh + docker run --rm --name ubuntu-build --mount type=bind,source=`pwd`,target=/usr/local/src ${{ matrix.distro }} /usr/local/src/build.sh mkdir -p output/{init,bash_completion,zsh_completion} cp sftpgo.json output/ cp -r templates output/ @@ -412,12 +433,6 @@ jobs: gzip output/man/man1/* cp sftpgo output/ - - name: Get commit SHA - if: ${{ matrix.arch != 'amd64' }} - id: get_commit - run: echo ::set-output name=COMMIT::${GITHUB_SHA::8} - shell: bash - - uses: uraimo/run-on-arch-action@v2 if: ${{ matrix.arch != 'amd64' }} name: Build for ${{ matrix.arch }} @@ -448,6 +463,7 @@ jobs: tar -C /usr/local -xzf go.tar.gz run: | export PATH=$PATH:/usr/local/go/bin + go version if [ ${{ matrix.arch}} == 'armv7' ] then export GOARM=7 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index b8627ec7..bd8b4db6 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -5,7 +5,7 @@ on: tags: 'v*' env: - GO_VERSION: 1.18.5 + GO_VERSION: 1.18.6 jobs: prepare-sources-with-deps: @@ -71,16 +71,16 @@ jobs: - name: Build for macOS x86_64 if: startsWith(matrix.os, 'windows-') != true - run: go build -trimpath -ldflags "-s -w -X github.com/drakkan/sftpgo/v2/version.commit=`git describe --always --dirty` -X github.com/drakkan/sftpgo/v2/version.date=`date -u +%FT%TZ`" -o sftpgo + run: go build -trimpath -ldflags "-s -w -X github.com/drakkan/sftpgo/v2/version.commit=`git describe --always --abbrev=8 --dirty` -X github.com/drakkan/sftpgo/v2/version.date=`date -u +%FT%TZ`" -o sftpgo - name: Build for macOS arm64 if: startsWith(matrix.os, 'macos-') == true - run: CGO_ENABLED=1 GOOS=darwin GOARCH=arm64 SDKROOT=$(xcrun --sdk macosx --show-sdk-path) go build -trimpath -ldflags "-s -w -X github.com/drakkan/sftpgo/v2/version.commit=`git describe --always --dirty` -X github.com/drakkan/sftpgo/v2/version.date=`date -u +%FT%TZ`" -o sftpgo_arm64 + run: CGO_ENABLED=1 GOOS=darwin GOARCH=arm64 SDKROOT=$(xcrun --sdk macosx --show-sdk-path) go build -trimpath -ldflags "-s -w -X github.com/drakkan/sftpgo/v2/version.commit=`git describe --always --abbrev=8 --dirty` -X github.com/drakkan/sftpgo/v2/version.date=`date -u +%FT%TZ`" -o sftpgo_arm64 - name: Build for Windows if: startsWith(matrix.os, 'windows-') run: | - $GIT_COMMIT = (git describe --always --dirty) | Out-String + $GIT_COMMIT = (git describe --always --abbrev=8 --dirty) | Out-String $DATE_TIME = ([datetime]::Now.ToUniversalTime().toString("yyyy-MM-ddTHH:mm:ssZ")) | Out-String $FILE_VERSION = $Env:SFTPGO_VERSION.substring(1) + ".0" go install github.com/tc-hib/go-winres@latest @@ -254,11 +254,12 @@ jobs: prepare-linux: name: Prepare Linux binaries - runs-on: ubuntu-18.04 + runs-on: ubuntu-latest strategy: matrix: include: - arch: amd64 + distro: ubuntu:18.04 go-arch: amd64 deb-arch: amd64 rpm-arch: x86_64 @@ -284,17 +285,13 @@ jobs: steps: - uses: actions/checkout@v3 - - name: Set up Go - if: ${{ matrix.arch == 'amd64' }} - uses: actions/setup-go@v3 - with: - go-version: ${{ env.GO_VERSION }} - name: Get versions id: get_version run: | echo ::set-output name=SFTPGO_VERSION::${GITHUB_REF/refs\/tags\//} echo ::set-output name=GO_VERSION::${GO_VERSION} + echo ::set-output name=COMMIT::${GITHUB_SHA::8} shell: bash env: GO_VERSION: ${{ env.GO_VERSION }} @@ -302,7 +299,20 @@ jobs: - name: Build on amd64 if: ${{ matrix.arch == 'amd64' }} run: | - go build -trimpath -ldflags "-s -w -X github.com/drakkan/sftpgo/v2/version.commit=`git describe --always --dirty` -X github.com/drakkan/sftpgo/v2/version.date=`date -u +%FT%TZ`" -o sftpgo + echo '#!/bin/bash' > build.sh + echo '' >> build.sh + echo 'set -e' >> build.sh + echo 'apt-get update -q -y' >> build.sh + echo 'apt-get install -q -y curl gcc' >> build.sh + echo 'curl --retry 5 --retry-delay 2 --connect-timeout 10 -o go.tar.gz -L https://go.dev/dl/go${{ steps.get_version.outputs.GO_VERSION }}.linux-${{ matrix.go-arch }}.tar.gz' >> build.sh + echo 'tar -C /usr/local -xzf go.tar.gz' >> build.sh + echo 'export PATH=$PATH:/usr/local/go/bin' >> build.sh + echo 'go version' >> build.sh + echo 'cd /usr/local/src' >> build.sh + echo 'go build -buildvcs=false -trimpath -ldflags "-s -w -X github.com/drakkan/sftpgo/v2/version.commit=${{ steps.get_version.outputs.COMMIT }} -X github.com/drakkan/sftpgo/v2/version.date=`date -u +%FT%TZ`" -o sftpgo' >> build.sh + + chmod 755 build.sh + docker run --rm --name ubuntu-build --mount type=bind,source=`pwd`,target=/usr/local/src ${{ matrix.distro }} /usr/local/src/build.sh mkdir -p output/{init,sqlite,bash_completion,zsh_completion} echo "For documentation please take a look here:" > output/README.txt echo "" >> output/README.txt @@ -326,12 +336,6 @@ jobs: env: SFTPGO_VERSION: ${{ steps.get_version.outputs.SFTPGO_VERSION }} - - name: Get commit SHA - if: ${{ matrix.arch != 'amd64' }} - id: get_commit - run: echo ::set-output name=COMMIT::${GITHUB_SHA::8} - shell: bash - - uses: uraimo/run-on-arch-action@v2 if: ${{ matrix.arch != 'amd64' }} name: Build for ${{ matrix.arch }} @@ -356,7 +360,8 @@ jobs: tar -C /usr/local -xzf go.tar.gz run: | export PATH=$PATH:/usr/local/go/bin - go build -buildvcs=false -trimpath -ldflags "-s -w -X github.com/drakkan/sftpgo/v2/version.commit=${{ steps.get_commit.outputs.COMMIT }} -X github.com/drakkan/sftpgo/v2/version.date=`date -u +%FT%TZ`" -o sftpgo + go version + go build -buildvcs=false -trimpath -ldflags "-s -w -X github.com/drakkan/sftpgo/v2/version.commit=${{ steps.get_version.outputs.COMMIT }} -X github.com/drakkan/sftpgo/v2/version.date=`date -u +%FT%TZ`" -o sftpgo mkdir -p output/{init,sqlite,bash_completion,zsh_completion} echo "For documentation please take a look here:" > output/README.txt echo "" >> output/README.txt diff --git a/Dockerfile b/Dockerfile index 426c9081..4e4802e0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -20,7 +20,7 @@ ARG FEATURES COPY . . RUN set -xe && \ - export COMMIT_SHA=${COMMIT_SHA:-$(git describe --always --dirty)} && \ + export COMMIT_SHA=${COMMIT_SHA:-$(git describe --always --abbrev=8 --dirty)} && \ go build $(if [ -n "${FEATURES}" ]; then echo "-tags ${FEATURES}"; fi) -trimpath -ldflags "-s -w -X github.com/drakkan/sftpgo/v2/version.commit=${COMMIT_SHA} -X github.com/drakkan/sftpgo/v2/version.date=`date -u +%FT%TZ`" -v -o sftpgo # Set to "true" to download the "official" plugins in /usr/local/bin diff --git a/Dockerfile.alpine b/Dockerfile.alpine index 19452483..7fa1460c 100644 --- a/Dockerfile.alpine +++ b/Dockerfile.alpine @@ -22,7 +22,7 @@ ARG FEATURES COPY . . RUN set -xe && \ - export COMMIT_SHA=${COMMIT_SHA:-$(git describe --always --dirty)} && \ + export COMMIT_SHA=${COMMIT_SHA:-$(git describe --always --abbrev=8 --dirty)} && \ go build $(if [ -n "${FEATURES}" ]; then echo "-tags ${FEATURES}"; fi) -trimpath -ldflags "-s -w -X github.com/drakkan/sftpgo/v2/version.commit=${COMMIT_SHA} -X github.com/drakkan/sftpgo/v2/version.date=`date -u +%FT%TZ`" -v -o sftpgo diff --git a/Dockerfile.distroless b/Dockerfile.distroless index 97ce4824..af883ea6 100644 --- a/Dockerfile.distroless +++ b/Dockerfile.distroless @@ -20,7 +20,7 @@ ARG FEATURES=nosqlite COPY . . RUN set -xe && \ - export COMMIT_SHA=${COMMIT_SHA:-$(git describe --always --dirty)} && \ + export COMMIT_SHA=${COMMIT_SHA:-$(git describe --always --abbrev=8 --dirty)} && \ go build $(if [ -n "${FEATURES}" ]; then echo "-tags ${FEATURES}"; fi) -trimpath -ldflags "-s -w -X github.com/drakkan/sftpgo/v2/version.commit=${COMMIT_SHA} -X github.com/drakkan/sftpgo/v2/version.date=`date -u +%FT%TZ`" -v -o sftpgo # Modify the default configuration file diff --git a/docs/build-from-source.md b/docs/build-from-source.md index 95c6be5d..8934d688 100644 --- a/docs/build-from-source.md +++ b/docs/build-from-source.md @@ -29,7 +29,7 @@ Version info, such as git commit and build date, can be embedded setting the fol For example, you can build using the following command: ```bash -go build -tags nogcs,nos3,nosqlite -ldflags "-s -w -X github.com/drakkan/sftpgo/v2/version.commit=`git describe --always --dirty` -X github.com/drakkan/sftpgo/v2/version.date=`date -u +%FT%TZ`" -o sftpgo +go build -tags nogcs,nos3,nosqlite -ldflags "-s -w -X github.com/drakkan/sftpgo/v2/version.commit=`git describe --always --abbrev=8 --dirty` -X github.com/drakkan/sftpgo/v2/version.date=`date -u +%FT%TZ`" -o sftpgo ``` You should get a version that includes git commit, build date and available features like this one: