Skip to content

Commit

Permalink
feat: 添加构建脚本
Browse files Browse the repository at this point in the history
  • Loading branch information
wojiushixiaobai committed May 25, 2024
1 parent 74ea0f1 commit 3b992b5
Show file tree
Hide file tree
Showing 5 changed files with 431 additions and 201 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
* text=lf
99 changes: 99 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
name: build

on:
workflow_dispatch:
schedule:
- cron: '0 20 * * *'

jobs:
check-binaries:
runs-on: ubuntu-latest
strategy:
matrix:
app: [compose]
repo: ['docker/compose']
steps:
- uses: actions/checkout@v4
- uses: docker/setup-buildx-action@v3

- name: Get Version
run: |
APP_VERSION=$(curl -s "https://api.github.com/repos/${{ matrix.repo }}/releases/latest" | jq -r .tag_name)
GO_VERSION=$(curl -sSL https://github.com/${{ matrix.repo }}/raw/${APP_VERSION}/Dockerfile | grep '^ARG GO_VERSION=' | awk -F "=" '{print $2}')
echo "APP_VERSION=${APP_VERSION}" >> $GITHUB_ENV
sed -i "s@ARG GO_VERSION=.*@ARG GO_VERSION=${GO_VERSION}@g" Dockerfile
sed -i "s@ARG COMPOSE_VERSION=.*@ARG COMPOSE_VERSION=${APP_VERSION}@g" Dockerfile
echo ""
echo "========== Build Args =========="
echo "GO_VERSION=${GO_VERSION}"
echo "COMPOSE_VERSION=${APP_VERSION}"
- name: Check Release
id: check-release
run: |
gh release view ${{ env.APP_VERSION }} -R ${{ github.repository }} >/dev/null 2>&1 || echo "create=1" >> $GITHUB_OUTPUT
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Create Tag
if : steps.check-release.outputs.create == '1'
run: |
git config --global user.name "${GITHUB_ACTOR}"
git config --global user.email "${GITHUB_ACTOR}@users.noreply.github.com"
git add .
git commit -m "Release ${{ env.APP_VERSION }}" || true
git tag ${{ env.APP_VERSION }}
git push origin ${{ env.APP_VERSION }} || true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Create Release
if : steps.check-release.outputs.create == '1'
run: |
gh release create ${{ env.APP_VERSION }} -R ${{ github.repository }} --title ${{ env.APP_VERSION }} --notes "**Full Changelog**: [${{ env.APP_VERSION }}](https://github.com/docker/buildx/releases/tag/${{ env.APP_VERSION }})"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

build-binaries:
runs-on: ubuntu-latest
needs: check-binaries
strategy:
matrix:
app: [compose]
repo: ['docker/compose']
steps:
- name: Get Version
run: |
APP_VERSION=$(curl -s "https://api.github.com/repos/${{ matrix.repo }}/releases/latest" | jq -r .tag_name)
echo "APP_VERSION=${APP_VERSION}" >> $GITHUB_ENV
- uses: actions/checkout@v4
with:
ref: ${{ env.APP_VERSION }}

- uses: docker/setup-buildx-action@v3

- name: Check Release
id: create-binaries
run: |
gh release view ${{ env.APP_VERSION }} -R ${{ github.repository }} | grep ${{ matrix.app }}-.* >/dev/null 2>&1 || echo "create=1" >> $GITHUB_OUTPUT
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Setup QEMU
if : steps.create-binaries.outputs.create == '1'
run: docker run --rm --privileged ghcr.io/loong64/qemu-user-static --reset -p yes

- name: Build Binaries
if : steps.create-binaries.outputs.create == '1'
run: |
docker buildx build --platform linux/loong64 -t ${{ matrix.app }}-static-loong64:${{ env.APP_VERSION }} . --load
- name: Upgrade Release
if : steps.create-binaries.outputs.create == '1'
run: |
docker run --rm -v $(pwd)/dist:/dist ${{ matrix.app }}-static-loong64:${{ env.APP_VERSION }}
ls -al dist
gh release upload ${{ env.APP_VERSION }} -R ${{ github.repository }} dist/*
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
44 changes: 44 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
ARG GO_VERSION=1.21

FROM ghcr.io/loong64/golang:${GO_VERSION}-trixie as builder

ARG COMPOSE_VERSION=v2.27.1

RUN set -ex; \
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime; \
apt-get update; \
apt-get install -y git file make

RUN set -ex; \
git clone -b ${COMPOSE_VERSION} https://github.com/docker/compose /opt/compose --depth=1

WORKDIR /opt/compose

ENV GOFLAGS=-mod=vendor \
CGO_ENABLED=0

RUN set -ex; \
go mod download -x; \
go mod tidy; \
go mod vendor; \
PKG=github.com/docker/compose/v2 VERSION=$(git describe --match 'v[0-9]*' --dirty='.m' --always --tags); \
echo "-X ${PKG}/internal.Version=${VERSION}" | tee /tmp/.ldflags; \
echo -n "${VERSION}" | tee /tmp/.version

RUN set -ex; \
mkdir /opt/compose/dist; \
go build -trimpath -tags "$BUILD_TAGS" -ldflags "$(cat /tmp/.ldflags) -w -s" -o /opt/compose/dist/docker-compose ./cmd; \
cd /opt/compose/dist; \
mv docker-compose docker-compose-linux-$(uname -m); \
sha256sum docker-compose-linux-loongarch64 > /tmp/checksums.txt; \
cat /tmp/checksums.txt | while read sum file; do echo "$sum *$file" > ${file#\*}.sha256; done

FROM ghcr.io/loong64/debian:trixie-slim

WORKDIR /opt/compose

COPY --from=builder /opt/compose/dist /opt/compose/dist

VOLUME /dist

CMD cp -rf dist/* /dist/
Loading

0 comments on commit 3b992b5

Please sign in to comment.