From bfb2e6d1872effb5ca497fbb9aa79bf75a166ab6 Mon Sep 17 00:00:00 2001 From: mput Date: Tue, 13 Feb 2024 23:30:34 +0100 Subject: [PATCH] add docker build and publish step --- .github/workflows/go.yml | 25 ++++++++++++++++++++++--- Dockerfile | 19 +++++++++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 Dockerfile diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 9517886..7027032 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -24,11 +24,30 @@ jobs: run: | sudo apt-get install -y ledger - - name: Build - run: go build -v ./... - - name: Test run: go test -v ./... env: GITHUB_URL: ${{ secrets.GIT_URL }} GITHUB_TOKEN: ${{ secrets.GIT_ACCESS_TOKEN }} + + - name: set up Docker Buildx + id: buildx + uses: docker/setup-buildx-action@v3 + + - name: build and publish image + if: ${{ github.ref == 'refs/heads/main' }} + env: + DOCKER_HUB_USER: ${{ secrets.DOCKER_HUB_USER }} + DOCKER_HUB_TOKEN: ${{ secrets.DOCKER_HUB_TOKEN }} + GITHUB_SHA: ${{ github.sha }} + GITHUB_REF: ${{ github.ref }} + run: | + ref="$(echo ${GITHUB_REF} | cut -d'/' -f3)" + version=${ref}-${GITHUB_SHA:0:7}-$(date -u +%Y-%m-%dT%H:%M:%S) + echo "GITHUB_REF=${GITHUB_REF}, GITHUB_SHA=${GITHUB_SHA}, GIT_BRANCH=${ref}" + echo "version=${version}" + echo ${DOCKER_HUB_TOKEN} | docker login -u ${DOCKER_HUB_USER} --password-stdin + docker buildx build --push \ + --build-arg VERSION=${version} \ + --platform linux/amd64,linux/arm/v7,linux/arm64 \ + -t ${DOCKER_HUB_USER}/teledger:${ref} . diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..7037aaa --- /dev/null +++ b/Dockerfile @@ -0,0 +1,19 @@ +FROM golang:1.22.0-alpine as build + + +ADD ./ /repo +WORKDIR /repo + +ARG VERSION=docker-dev +RUN echo go version: `go version` +RUN echo build version: ${VERSION} + +RUN go build -o teledger -ldflags "-X main.version=${version} -s -w" ./app/main.go + +FROM golang:1.22.0-alpine + +WORKDIR /srv +RUN apk add --no-cache ledger=~3.3.2 && echo ledger: `which ledger` +COPY --from=build /repo/teledger /srv/teledger + +CMD ["/srv/teledger"]