From 137c512d475cf9184aeb662e127b27e742c3387d Mon Sep 17 00:00:00 2001 From: duskmoon Date: Sun, 10 Mar 2024 08:21:10 +0000 Subject: [PATCH] Add Dockerfile and CI Add a Dockerfile to build an image for GoAuthing, which can be used to run the app on a server without configuring systemd or other init systems (e.g. Some commercial NAS devices may not support systemd). Add two GitHub Actions jobs to build and push the Docker image to GitHub Container Registry. --- .github/workflows/go.yml | 19 ++++++++++++++++++ .github/workflows/release.yml | 38 +++++++++++++++++++++++++++++++++++ Dockerfile | 13 ++++++++++++ README.md | 15 ++++++++++++++ 4 files changed, 85 insertions(+) create mode 100644 Dockerfile diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 39146e5..49ee13a 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -42,3 +42,22 @@ jobs: CGO_ENABLED=0 GOARCH=riscv64 GOOS=linux go build -ldflags="-s -w" -o auth-thu.linux.riscv64 ./cli/main.go CGO_ENABLED=0 GOARCH=loong64 GOOS=linux go build -ldflags="-s -w" -o auth-thu.linux.loong64 ./cli/main.go + build-image: + name: Build Docker Image + runs-on: ubuntu-latest + needs: build + steps: + - name: Check out code into the Go module directory + uses: actions/checkout@v4 + + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Build the Docker image + uses: docker/build-push-action@v5 + with: + push: false + context: . diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 6840175..cc09381 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -55,3 +55,41 @@ jobs: tag_name="${GITHUB_REF##*/}" hub release edit $(find . -type f -executable -name "auth-thu.*" -printf "-a %p ") -m "" "$tag_name" + build-image: + name: Build Image + runs-on: ubuntu-latest + needs: build + env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + + steps: + - name: Check out code into the Go module directory + uses: actions/checkout@v4 + + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Log in to the Container Registry + uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract metadata + id: metadata + uses: docker/metadata-action@v5 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + + - name: Build and push the Docker image + uses: docker/build-push-action@v5 + with: + push: true + context: . + tags: ${{ steps.metadata.outputs.tags }} + labels: ${{ steps.metadata.outputs.labels }} diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..cc4873b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,13 @@ +FROM golang:alpine AS builder + +WORKDIR /app +COPY . . + +RUN CGO_ENABLED=0 go build -ldflags="-s -w" -o /app/auth-thu /app/cli/main.go + +FROM scratch + +COPY --from=builder /app/auth-thu /auth-thu +COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt + +ENTRYPOINT [ "/auth-thu" ] \ No newline at end of file diff --git a/README.md b/README.md index 657da77..84335e9 100644 --- a/README.md +++ b/README.md @@ -126,6 +126,21 @@ uci commit goauthing /etc/init.d/goauthing start ``` +### Docker + +For Docker users, you can run the container with a restart policy. An example docker compose is like this: + +```yaml +services: + goauthing: + image: ghcr.io/z4yx/goauthing:latest + container_name: goauthing + restart: always + volumes: + - /path/to/your/config:/.auth-thu + command: auth -k +``` + ## Build Requires Go 1.11 or above