diff --git a/.github/workflows/publish-to-ghcr.yml b/.github/workflows/publish-to-ghcr.yml new file mode 100644 index 00000000..76614638 --- /dev/null +++ b/.github/workflows/publish-to-ghcr.yml @@ -0,0 +1,108 @@ +name: Publish Docker Image to GitHub Container Registry +run-name: Publish branch ${{ github.ref_name }} to tag ${{ inputs.dockerTag }} + +on: + workflow_dispatch: + inputs: + dockerTag: + description: 'Docker Tag' + required: true + type: string + default: 'latest' + container: + type: choice + description: 'Container to build and publish.' + required: true + options: + - nodemon + - nodemon-telegram + - nodemon-discord + workflow_call: + inputs: + dockerTag: + description: 'Docker Tag' + required: true + type: string + default: 'latest' + container: + type: string + description: 'Container to build and publish (nodemon, nodemon-telegram, nodemon-discord).' + required: true + default: 'nodemon' + +env: + IMAGE_TAG: ${{ inputs.dockerTag }} + IMAGE_ARG_APP: ${{ inputs.container }} + IMAGE_NAME: |- + ghcr.io/${{ github.repository }}${{ + case( + inputs.container == 'nodemon', '', + inputs.container == 'nodemon-telegram', '-telegram', + inputs.container == 'nodemon-discord', '-discord', + '' + ) + }} + +permissions: { } + +jobs: + build: + name: Build and Push Docker Image to GitHub Container Registry + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + attestations: write + id-token: write + steps: + - name: Check inputs + env: + IMAGE_ARG_APP: ${{ env.IMAGE_ARG_APP }} + IMAGE_TAG: ${{ env.IMAGE_TAG }} + run: | + echo "$IMAGE_TAG" | grep -E '^[a-zA-Z0-9._-]+$' || exit 1 # check if the docker tag is valid + echo "$IMAGE_ARG_APP" | grep -E '^(nodemon|nodemon-telegram|nodemon-discord)$' || exit 1 # check if the container name is valid + + - name: Check out code into the Go module directory + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v5 + with: + fetch-depth: 0 + persist-credentials: false + + - name: Login to GitHub Container Registry + uses: docker/login-action@4907a6ddec9925e35a0a9e82d7399ccc52663121 # v4.1.0 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Set up Docker BuildX + uses: docker/setup-buildx-action@4d04d5d9486b7bd6fa91e7baf45bbb4f8b9deedd # v4.0.0 + + - name: Docker Metadata + id: meta + uses: docker/metadata-action@030e881283bb7a6894de51c315a6bfe6a94e05cf # v6.0.0 + with: + images: ${{ env.IMAGE_NAME }} + flavor: latest=false + tags: | + type=sha + type=raw,value=${{ env.IMAGE_TAG }} + labels: | + org.opencontainers.image.source=https://github.com/${{ github.repository }} + org.opencontainers.image.licenses=MIT + org.opencontainers.image.description="Nodemonitoring and alerting tools for Waves Nodes" + + - name: Build and Push Docker Image + uses: docker/build-push-action@bcafcacb16a39f128d818304e6c9c0c18556b85f # v5 + id: push + with: + context: . + platforms: linux/amd64,linux/arm64 + push: true + pull: true + build-args: 'APP=${{ env.IMAGE_ARG_APP }}' + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + cache-from: type=gha + cache-to: type=gha,mode=max diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..df490df0 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,56 @@ +FROM golang:1.26-alpine3.22@sha256:07e91d24f6330432729082bb580983181809e0a48f0f38ecde26868d4568c6ac AS builder +ARG DIR=/app +WORKDIR ${DIR} + +ARG APP=nodemon +ARG TARGETOS +ARG TARGETARCH + +RUN apk add --no-cache make git +# disable cgo for go build +ENV CGO_ENABLED=0 + +COPY go.mod . +COPY go.sum . + +RUN go mod download + +# Copy the .git directory and restore the worktree, also handle current possible changes in go.mod and go.sum +COPY .git .git +RUN git restore --source=HEAD --worktree . +COPY go.mod . +COPY go.sum . + +# Copy the necessary files for building and override the restored worktree +COPY Makefile . +COPY cmd . +COPY pkg . +COPY internal internal + +RUN make build-$APP-$TARGETOS-$TARGETARCH + +FROM alpine:3.23@sha256:5b10f432ef3da1b8d4c7eb6c487f2f5a8f096bc91145e68878dd4a5019afde11 +ARG DIR=/app +ENV TZ=Etc/UTC \ + APP_USER=appuser + +ARG APP=nodemon +ARG TARGETOS +ARG TARGETARCH + +STOPSIGNAL SIGINT + +RUN addgroup -S $APP_USER \ + && adduser -S $APP_USER -G $APP_USER + +RUN apk add --no-cache bind-tools + +USER $APP_USER +WORKDIR ${DIR} +# Considered as a default HTTP API Port, NATS embedded server port +EXPOSE 8080 +EXPOSE 4222 + +COPY --from=builder ${DIR}/build/$TARGETOS-$TARGETARCH/$APP ${DIR}/$APP + +ENTRYPOINT ["./$APP"] diff --git a/Makefile b/Makefile index 5abdb78e..3153fa64 100644 --- a/Makefile +++ b/Makefile @@ -21,7 +21,9 @@ clean: rm -r build/ build: - @go build -o build/nodemon -ldflags="-X 'nodemon/internal.version=$(VERSION)'" ./cmd/nodemon + @go build -o build/native/nodemon -ldflags="-X 'nodemon/internal.version=$(VERSION)'" ./cmd/nodemon + @go build -o build/native/nodemon-telegram -ldflags="-X 'nodemon/internal.version=$(VERSION)'" ./cmd/bots/telegram + @go build -o build/native/nodemon-discord -ldflags="-X 'nodemon/internal.version=$(VERSION)'" ./cmd/bots/discord gotest: go test -cover -race -covermode=atomic ./... @@ -29,9 +31,24 @@ gotest: mod-clean: go mod tidy -build-bots-linux-amd64: +build-bots-linux-amd64: build-nodemon-telegram-linux-amd64 build-nodemon-discord-linux-amd64 + +build-nodemon-telegram-linux-amd64: @CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o build/linux-amd64/nodemon-telegram -ldflags="-X 'nodemon/internal.version=$(VERSION)'" ./cmd/bots/telegram + +build-nodemon-discord-linux-amd64: @CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o build/linux-amd64/nodemon-discord -ldflags="-X 'nodemon/internal.version=$(VERSION)'" ./cmd/bots/discord +build-bots-linux-arm64: build-nodemon-telegram-linux-arm64 build-nodemon-discord-linux-arm64 + +build-nodemon-telegram-linux-arm64: + @CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o build/linux-arm64/nodemon-telegram -ldflags="-X 'nodemon/internal.version=$(VERSION)'" ./cmd/bots/telegram + +build-nodemon-discord-linux-arm64: + @CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o build/linux-arm64/nodemon-discord -ldflags="-X 'nodemon/internal.version=$(VERSION)'" ./cmd/bots/discord + +build-nodemon-linux-arm64: + @CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o build/linux-arm64/nodemon -ldflags="-X 'nodemon/internal.version=$(VERSION)'" ./cmd/nodemon + build-nodemon-linux-amd64: @CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o build/linux-amd64/nodemon -ldflags="-X 'nodemon/internal.version=$(VERSION)'" ./cmd/nodemon