From 4d146e8ce2333dc9f9d74194a8b7b3a84e1e9976 Mon Sep 17 00:00:00 2001 From: andrew Date: Fri, 8 Mar 2024 16:41:25 +0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8=20fix=20docker=20build?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build-and-push-docker.yml | 81 +++++++++++++++++++++ .github/workflows/docker-image.yml | 10 ++- .github/workflows/semantic.yml | 6 +- docker/Dockerfile | 24 ++++++ docker/alpine/Dockerfile | 36 +++++++++ docker/alpine/entrypoint.sh | 15 ++++ docker/entrypoint.sh | 15 ++++ 7 files changed, 184 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/build-and-push-docker.yml create mode 100644 docker/Dockerfile create mode 100644 docker/alpine/Dockerfile create mode 100755 docker/alpine/entrypoint.sh create mode 100755 docker/entrypoint.sh diff --git a/.github/workflows/build-and-push-docker.yml b/.github/workflows/build-and-push-docker.yml new file mode 100644 index 000000000..c9c61bb4b --- /dev/null +++ b/.github/workflows/build-and-push-docker.yml @@ -0,0 +1,81 @@ +name: Build & push vMonitor-Metrics-Agent Docker Image +on: + workflow_dispatch: + inputs: + push_to_hub: + description: 'Publish docker image to dockerhub' + required: false + default: false + type: boolean +env: + REPO_NAME: ${{ github.event.repository.name }} +jobs: + build: + name: Build & push docker image + runs-on: ubuntu-20.04 + steps: + - name: Checkout + uses: actions/checkout@v3 + - name: Create image name env + run: | + echo "IMG_NAME=${{ vars.DOCKERHUB_USERNAME }}/${{ env.REPO_NAME }}" >> $GITHUB_ENV + - name: debug + run: | + echo ${{ env.IMG_NAME }} + - name: Docker metadata + id: metadata + uses: docker/metadata-action@v4 + with: + images: | + ${{ env.IMG_NAME }} + tags: | + type=semver,pattern={{version}} + type=semver,pattern={{major}}.{{minor}} + type=raw,value={{sha}},enable=${{ github.ref_type != 'tag' }} + - name: Login to Docker Hub + uses: docker/login-action@v2 + with: + username: ${{ vars.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + - name: Build and push Docker image + uses: docker/build-push-action@v4 + with: + context: ./docker + push: ${{ inputs.push_to_hub }} + tags: ${{ steps.metadata.outputs.tags }} + labels: ${{ steps.metadata.outputs.labels }} + build-alpine: + name: Build & push docker image Alpine + runs-on: ubuntu-20.04 + steps: + - name: Checkout + uses: actions/checkout@v3 + - name: Create image name env + run: | + echo "IMG_NAME=${{ vars.DOCKERHUB_USERNAME }}/${{ env.REPO_NAME }}" >> $GITHUB_ENV + - name: debug + run: | + echo ${{ env.IMG_NAME }} + - name: Docker metadata + id: metadata + uses: docker/metadata-action@v4 + with: + images: | + ${{ env.IMG_NAME }} + tags: | + type=semver,pattern={{version}},suffix=-alpine + type=semver,pattern={{major}}.{{minor}},suffix=-alpine + type=raw,value={{sha}}-alpine,enable=${{ github.ref_type != 'tag' }} + - name: Login to Docker Hub + uses: docker/login-action@v2 + with: + username: ${{ vars.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + - name: Build and push Docker image + uses: docker/build-push-action@v4 + with: + context: ./docker/alpine + push: ${{ inputs.push_to_hub }} + tags: ${{ steps.metadata.outputs.tags }} + labels: ${{ steps.metadata.outputs.labels }} \ No newline at end of file diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml index 29e0e1bd8..e711f662f 100644 --- a/.github/workflows/docker-image.yml +++ b/.github/workflows/docker-image.yml @@ -13,4 +13,12 @@ jobs: steps: - uses: actions/checkout@v3 - name: Build the Docker image - run: docker build . --file Dockerfile --tag my-image-name:$(date +%s) + run: docker build ./docker --file Dockerfile --tag my-image-name:$(date +%s) + + build-alpine: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - name: Build the Docker image Alpine + run: docker build ./docker/alpine --file Dockerfile --tag my-image-name-alpine:$(date +%s) diff --git a/.github/workflows/semantic.yml b/.github/workflows/semantic.yml index 234874d3c..7dc9f439d 100644 --- a/.github/workflows/semantic.yml +++ b/.github/workflows/semantic.yml @@ -2,8 +2,10 @@ name: "Semantic PR and Commit Messages" on: - push: - branches: [ "release-1.26" ] + pull_request: + types: [opened, reopened, synchronize, edited] + branches: + - master jobs: semantic: diff --git a/docker/Dockerfile b/docker/Dockerfile new file mode 100644 index 000000000..3270f956f --- /dev/null +++ b/docker/Dockerfile @@ -0,0 +1,24 @@ +FROM buildpack-deps:bullseye-curl + +RUN DEBIAN_FRONTEND=noninteractive apt-get update && \ + DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends iputils-ping snmp procps lm-sensors libcap2-bin && \ + rm -rf /var/lib/apt/lists/* + +ENV TELEGRAF_VERSION 1.26.0-2.0.2 +RUN ARCH= && dpkgArch="$(dpkg --print-architecture)" && \ + case "${dpkgArch##*-}" in \ + amd64) ARCH='amd64';; \ + arm64) ARCH='arm64';; \ + armhf) ARCH='armhf';; \ + armel) ARCH='armel';; \ + *) echo "Unsupported architecture: ${dpkgArch}"; exit 1;; \ + esac && \ + wget --no-verbose https://github.com/vngcloud/vmonitor-metrics-agent/releases/download/${TELEGRAF_VERSION}/telegraf_nightly_${ARCH}.deb && \ + dpkg -i telegraf_nightly_${ARCH}.deb && \ + rm -f telegraf_nightly_${ARCH}.deb* + +EXPOSE 8125/udp 8092/udp 8094 + +COPY entrypoint.sh /entrypoint.sh +ENTRYPOINT ["/entrypoint.sh"] +CMD ["telegraf"] diff --git a/docker/alpine/Dockerfile b/docker/alpine/Dockerfile new file mode 100644 index 000000000..99e0ca4f4 --- /dev/null +++ b/docker/alpine/Dockerfile @@ -0,0 +1,36 @@ +FROM alpine:3.17 + +RUN echo 'hosts: files dns' >> /etc/nsswitch.conf +RUN apk add --no-cache iputils ca-certificates net-snmp-tools procps lm_sensors tzdata su-exec libcap && \ + update-ca-certificates + +ENV TELEGRAF_VERSION 1.26.0-2.0.2 + +RUN ARCH= && \ + case "$(apk --print-arch)" in \ + x86_64) ARCH='amd64';; \ + aarch64) ARCH='arm64';; \ + *) echo "Unsupported architecture: $(apk --print-arch)"; exit 1;; \ + esac && \ + set -ex && \ + mkdir ~/.gnupg; \ + echo "disable-ipv6" >> ~/.gnupg/dirmngr.conf; \ + apk add --no-cache --virtual .build-deps wget gnupg tar && \ + wget --no-verbose https://github.com/vngcloud/vmonitor-metrics-agent/releases/download/${TELEGRAF_VERSION}/telegraf-nightly_static_linux_${ARCH}.tar.gz && \ + mkdir -p /usr/src /etc/telegraf && \ + tar -C /usr/src -xzf telegraf-nightly_static_linux_${ARCH}.tar.gz && \ + mv /usr/src/telegraf*/etc/telegraf/telegraf.conf /etc/telegraf/ && \ + mkdir /etc/telegraf/telegraf.d && \ + cp -a /usr/src/telegraf*/usr/bin/telegraf /usr/bin/ && \ + gpgconf --kill all && \ + rm -rf *.tar.gz* /usr/src /root/.gnupg && \ + apk del .build-deps && \ + addgroup -S telegraf && \ + adduser -S telegraf -G telegraf && \ + chown -R telegraf:telegraf /etc/telegraf + +EXPOSE 8125/udp 8092/udp 8094 + +COPY entrypoint.sh /entrypoint.sh +ENTRYPOINT ["/entrypoint.sh"] +CMD ["telegraf"] diff --git a/docker/alpine/entrypoint.sh b/docker/alpine/entrypoint.sh new file mode 100755 index 000000000..33e84eb82 --- /dev/null +++ b/docker/alpine/entrypoint.sh @@ -0,0 +1,15 @@ +#!/bin/sh +set -e + +if [ "${1:0:1}" = '-' ]; then + set -- telegraf "$@" +fi + +if [ "$(id -u)" -ne 0 ]; then + exec "$@" +else + # Allow telegraf to send ICMP packets and bind to privliged ports + setcap cap_net_raw,cap_net_bind_service+ep /usr/bin/telegraf || echo "Failed to set additional capabilities on /usr/bin/telegraf" + + exec su-exec telegraf "$@" +fi diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh new file mode 100755 index 000000000..ef4162817 --- /dev/null +++ b/docker/entrypoint.sh @@ -0,0 +1,15 @@ +#!/bin/bash +set -e + +if [ "${1:0:1}" = '-' ]; then + set -- telegraf "$@" +fi + +if [ $EUID -ne 0 ]; then + exec "$@" +else + # Allow telegraf to send ICMP packets and bind to privliged ports + setcap cap_net_raw,cap_net_bind_service+ep /usr/bin/telegraf || echo "Failed to set additional capabilities on /usr/bin/telegraf" + + exec setpriv --reuid telegraf --init-groups "$@" +fi