Skip to content

Commit

Permalink
refactor: magefile, ci: added docker workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
c-seeger committed Mar 6, 2024
1 parent bc6e170 commit 768aea4
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 72 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/docker.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Publish Docker image

on:
schedule:
- cron: "0 0 * * *"

jobs:
push_to_registry:
name: Push Docker image to Docker Hub
runs-on: ubuntu-latest
steps:
- name: Check out the repo
uses: actions/checkout@v4

- name: Log in to Docker Hub
uses: docker/login-action@f4ef78c080cd8ba55a85445d5b36e214a81df20a
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
with:
images: virtomize/mail2most

- name: Build and push Docker image
uses: docker/build-push-action@3b5e8027fcad23fda98b2e3ac259d8d67585f671
with:
context: .
file: ./Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
3 changes: 1 addition & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,4 @@ jobs:
cd mage && go run bootstrap.go
cd ..
- name: Test
run: mage test

run: mage test:run
13 changes: 2 additions & 11 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
From golang:latest as BUILDER

RUN git clone https://github.com/magefile/mage && cd mage && go run bootstrap.go

ADD . /mail2most/

RUN cd /mail2most/ && mage build

From alpine:latest

Maintainer Carsten Seeger <info@casee.de>
Maintainer Carsten Seeger <info@virtomize.com>

RUN apk update && apk add ca-certificates && rm -rf /var/cache/apk/*
RUN update-ca-certificates

RUN mkdir -p /mail2most/conf
WORKDIR /mail2most
COPY --from=BUILDER /mail2most/bin/mail2most /mail2most/mail2most
ADD mail2most mail2most
ADD conf/mail2most.conf /mail2most/conf/mail2most.conf
VOLUME /mail2most/conf
CMD ["./mail2most", "-c", "/mail2most/conf/mail2most.conf"]
105 changes: 46 additions & 59 deletions magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os/exec"
"strings"

"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
"github.com/mholt/archiver"
)
Expand All @@ -23,34 +24,55 @@ var (
registry = "virtomize/mail2most"
)

// Build - mage build
func Build() error {
err := Clean()
if err != nil {
return err
}
type (
Service mg.Namespace
Test mg.Namespace
Release mg.Namespace
)

err = Test()
if err != nil {
return err
}
// Run - mage run
func (t Service) Run() error {
return sh.RunV("go", "run", "main.go")
}

// Build - mage build
func (t Service) Build() error {
mg.Deps(Test.Run)
tag, _ := exec.Command("bash", "-c", "git tag --sort=-version:refname | head -n 1").Output()

return sh.RunV("go", "build", "-a", "-tags", "netgo", "-o", binPath+"/"+binName, "-ldflags", "-w -extldflags \"-static\" -X 'main.Version="+string(tag)+"'")
}

// CreateRelease - mage build
func CreateRelease() error {
err := Clean()
if err != nil {
return err
// Run - running tests and code coverage
func (t Test) Run() error {
mg.Deps(t.Clean)
return sh.RunV("go", "test", "-v", "-cover", "./...", "-coverprofile=coverage.out")
}

// Coverage - checking code coverage
func (t Test) Coverage() error {
if _, err := os.Stat("./coverage.out"); err != nil {
return fmt.Errorf("run mage test befor checking the code coverage")
}
return sh.RunV("go", "tool", "cover", "-html=coverage.out")
}

err = Test()
if err != nil {
return err
// Clean - cleans up the client generation and binarys
func (t Test) Clean() error {
mg.Deps(Release.CleanDocker)
fmt.Println("cleaning up")
if _, err := os.Stat("coverage.out"); err == nil {
err = os.Remove("coverage.out")
if err != nil {
return err
}
}
return os.RemoveAll("bin/")
}

// All - mage build all releases
func (Release) All() error {
mg.Deps(Test.Run)
osarch := make(map[string][]string)
osarch["linux"] = []string{"386", "amd64", "arm", "arm64"}
osarch["windows"] = []string{"386", "amd64"}
Expand Down Expand Up @@ -96,46 +118,10 @@ func CreateRelease() error {
return nil
}

// Test - running tests and code coverage
func Test() error {
return sh.RunV("go", "test", "-v", "-cover", "./...", "-coverprofile=coverage.out")
}

// Run - mage run
func Run() error {
return sh.RunV("go", "run", "main.go")
}

// Coverage - checking code coverage
func Coverage() error {
if _, err := os.Stat("./coverage.out"); err != nil {
return fmt.Errorf("run mage test befor checking the code coverage")
}
return sh.RunV("go", "tool", "cover", "-html=coverage.out")
}

// Clean cleans up the client generation and binarys
func Clean() error {
fmt.Println("cleaning up")
if _, err := os.Stat("coverage.out"); err == nil {
err = os.Remove("coverage.out")
if err != nil {
return err
}
}
err := cleanDocker()
if err != nil {
return err
}
return os.RemoveAll("bin/")
}

// Docker creates docker container
func Docker() error {
if err := cleanDocker(); err != nil {
return err
}

// Docker - creates docker container
func (r Release) Docker() error {
mg.Deps(r.CleanDocker)
mg.Deps(Service.Build)
if err := os.MkdirAll(dockerPath+"/conf", 0755); err != nil {
return err
}
Expand Down Expand Up @@ -172,7 +158,8 @@ func Docker() error {
return sh.RunV("docker", "push", registry+":latest")
}

func cleanDocker() error {
// CleanDocker - removes docker build files
func (Release) CleanDocker() error {
if _, err := os.Stat(dockerPath); err == nil {
err = os.RemoveAll(dockerPath)
if err != nil {
Expand Down

0 comments on commit 768aea4

Please sign in to comment.