From 820a4fff0cf42798352e9103deb8d28ff9f8a539 Mon Sep 17 00:00:00 2001 From: Aleksandr Soloshenko Date: Mon, 28 Oct 2024 20:31:50 +0700 Subject: [PATCH] [build] add Docker release --- .dockerignore | 1 + .github/workflows/release.yml | 24 +++++++++++++++++------- .github/workflows/test.yml | 18 ++++++++++++++++++ .goreleaser.yaml | 30 +++++++++++++++++------------- Dockerfile | 30 ++++++++++++++++++++++++++++++ 5 files changed, 83 insertions(+), 20 deletions(-) create mode 120000 .dockerignore create mode 100644 .github/workflows/test.yml create mode 100644 Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 120000 index 0000000..3e4e48b --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +.gitignore \ No newline at end of file diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 1d769ab..9e4f3dc 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -2,7 +2,6 @@ name: goreleaser on: - pull_request: push: # run only against tags tags: @@ -10,7 +9,7 @@ on: permissions: contents: write - # packages: write + packages: write # issues: write jobs: @@ -25,11 +24,22 @@ jobs: uses: actions/setup-go@v5 with: go-version: stable - # - name: Login to Docker Hub - # uses: docker/login-action@v3 - # with: - # username: ${{ secrets.DOCKERHUB_USERNAME }} - # password: ${{ secrets.DOCKERHUB_TOKEN }} + + - name: Login to Docker Hub + if: github.event_name != 'pull_request' + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + - name: Login to GitHub Container registry + if: github.event_name != 'pull_request' + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + - name: Run GoReleaser uses: goreleaser/goreleaser-action@v6 with: diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..2a61356 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,18 @@ +name: test + +on: + pull_request: + push: + +permissions: + contents: read + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-go@v5 + with: + go-version: stable + - run: go test -v ./... diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 1e04396..0e6e89c 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -40,19 +40,23 @@ archives: - goos: windows format: zip -# dockers: -# - image_templates: -# - "capcom6/labeled-storage:{{ .Tag }}" -# - "capcom6/labeled-storage:v{{ .Major }}" -# - "capcom6/labeled-storage:v{{ .Major }}.{{ .Minor }}" -# - "capcom6/labeled-storage:latest" -# dockerfile: Dockerfile -# build_flag_templates: -# - "--label=org.opencontainers.image.created={{ .Date }}" -# - "--label=org.opencontainers.image.title={{ .ProjectName }}" -# - "--label=org.opencontainers.image.revision={{ .FullCommit }}" -# - "--label=org.opencontainers.image.version={{ .Version }}" -# skip_push: false +dockers: + - image_templates: + - "smsgate/cli:{{ .Tag }}" + - "smsgate/cli:v{{ .Major }}" + - "smsgate/cli:v{{ .Major }}.{{ .Minor }}" + - "smsgate/cli:latest" + - ghcr.io/android-sms-gateway/cli:{{ .Tag }} + - ghcr.io/android-sms-gateway/cli:v{{ .Major }} + - ghcr.io/android-sms-gateway/cli:v{{ .Major }}.{{ .Minor }} + - ghcr.io/android-sms-gateway/cli:latest + dockerfile: Dockerfile + build_flag_templates: + - "--label=org.opencontainers.image.created={{ .Date }}" + - "--label=org.opencontainers.image.title={{ .ProjectName }}" + - "--label=org.opencontainers.image.revision={{ .FullCommit }}" + - "--label=org.opencontainers.image.version={{ .Version }}" + skip_push: false changelog: sort: asc diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..71157ca --- /dev/null +++ b/Dockerfile @@ -0,0 +1,30 @@ +# Start from the official Go image +FROM golang:1.23-alpine AS builder + +# Set the working directory inside the container +WORKDIR /app + +# Copy go mod and sum files +COPY go.mod go.sum ./ + +# Download all dependencies +RUN go mod download + +# Copy the source code into the container +COPY . . + +# Build the application +RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o smsgate ./cmd/smsgate + +# Start a new stage from scratch +FROM alpine:latest + +RUN apk --no-cache add ca-certificates + +WORKDIR /root/ + +# Copy the pre-built binary file from the previous stage +COPY --from=builder /app/smsgate . + +# Command to run the executable +ENTRYPOINT ["./smsgate"]