From 106c6d77176e879df20f82f38761c28b59c9c21c Mon Sep 17 00:00:00 2001 From: Manu Garg Date: Fri, 12 Jan 2024 22:07:10 -0800 Subject: [PATCH] [docker] Add docker image build (#182) --- .github/workflows/build.yml | 36 ++++++++++++++++++++++++++++++++++++ Dockerfile | 22 ++++++++++++++++++++++ src/Makefile | 16 ++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 Dockerfile diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index c518545..c1715e7 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -207,3 +207,39 @@ jobs: TWINE_PASSWORD: ${{ secrets.PYPI }} run: | twine upload src/pymod/dist/* + + build_and_push_docker_multiarch: + name: Build and push multiarch docker image + if: github.repository == 'manugarg/pacparser' && (github.ref == 'refs/heads/master' || startswith(github.ref, 'refs/heads/docker') || startsWith(github.ref, 'refs/tags/v')) + runs-on: ubuntu-latest + + steps: + - name: Check out code into the Go module directory + if: ${{ !contains(github.event_name, 'workflow_dispatch') }} + uses: actions/checkout@v2 + with: + fetch-depth: 0 + + - name: Check out code into the Go module directory + if: ${{ contains(github.event_name, 'workflow_dispatch') }} + uses: actions/checkout@v2 + with: + fetch-depth: 0 + ref: ${{ github.event.inputs.tag }} + + - name: Set up QEMU + uses: docker/setup-qemu-action@v1 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + + - name: Login to GHCR + uses: docker/login-action@v1 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Build and push release Docker Image (main-ghcr) + run: make docker_multiarch DOCKER_IMAGE=ghcr.io/manugarg/pacparser + diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..db58d32 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,22 @@ +# Use Alpine Linux as the base image +FROM alpine:latest as build-stage + +# Install build dependencies +RUN apk update && apk add --no-cache \ + gcc \ + git \ + g++ \ + bash \ + make + +WORKDIR /build +COPY src /build +RUN make pactester + +# Final stage +FROM alpine:latest + +WORKDIR /app +COPY --from=build-stage /build/pactester /app/ + +CMD /app/pactester \ No newline at end of file diff --git a/src/Makefile b/src/Makefile index 861fc29..de4923b 100644 --- a/src/Makefile +++ b/src/Makefile @@ -30,6 +30,14 @@ endif VERSION ?= $(shell git describe --always --tags --candidate=100) +GIT_TAG := $(shell git describe --exact-match --exclude tip --tags HEAD 2>/dev/null || /bin/true) +DOCKER_IMAGE ?= manugarg/cloudprober +ifeq "$(GIT_TAG)" "" + DOCKER_TAGS := -t $(DOCKER_IMAGE):master -t $(DOCKER_IMAGE):main +else + DOCKER_TAGS := -t $(DOCKER_IMAGE):$(GIT_TAG) -t $(DOCKER_IMAGE):latest +endif + OS_ARCH := $(subst /,_,$(shell uname -s | sed /\ /s//_/)) LIBRARY_NAME = libpacparser @@ -155,6 +163,14 @@ pymod-dist: pacparser.o pacparser.h spidermonkey/libjs.a install-pymod: pymod cd pymod && ARCHFLAGS="" $(PYTHON) setup.py install --root="$(DESTDIR)/" $(EXTRA_ARGS) +docker_multiarch: pactester Dockerfile + docker buildx build --push \ + --build-arg BUILD_DATE=`date -u +"%Y-%m-%dT%H:%M:%SZ"` \ + --build-arg VERSION=$(VERSION) \ + --build-arg VCS_REF=$(GIT_COMMIT) \ + --platform linux/amd64,linux/arm64,linux/arm/v7 \ + $(DOCKER_TAGS) . + clean: rm -f $(LIBRARY_LINK) $(LIBRARY) pacparser.o pactester pymod/pacparser_o_buildstamp jsapi_buildstamp rm -rf dist