Skip to content

Commit

Permalink
chore: create multiarch docker image
Browse files Browse the repository at this point in the history
  • Loading branch information
abemedia committed Aug 14, 2020
1 parent 74ac88e commit a02c959
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 40 deletions.
72 changes: 51 additions & 21 deletions .github/workflows/fider.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ name: fider
on:
push:
branches:
- master
- master
pull_request:
branches:
- master
- master

jobs:
test-ui:
Expand All @@ -17,7 +17,7 @@ jobs:

steps:
- name: checkout code
uses: actions/checkout@v1
uses: actions/checkout@v2
- run: npm ci
- run: mage lint:ui
- run: mage test:ui
Expand All @@ -32,7 +32,7 @@ jobs:
minio:
image: getfider/minio:0.0.2
ports:
- 9000:9000
- 9000:9000
env:
MINIO_ACCESS_KEY: s3user
MINIO_SECRET_KEY: s3user-s3cr3t
Expand All @@ -43,12 +43,12 @@ jobs:
POSTGRES_PASSWORD: fider_ci_pw
POSTGRES_DB: fider_ci
ports:
- 5432:5432
- 5432:5432
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5

steps:
- name: checkout code
uses: actions/checkout@v1
uses: actions/checkout@v2
- run: mage lint:server
- name: mage test:server
run: |
Expand All @@ -67,24 +67,54 @@ jobs:

steps:
- name: checkout code
uses: actions/checkout@v1
- run: docker build -t getfider/fider .

- name: extract branch name
run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
id: getBranch
uses: actions/checkout@v2

- uses: azure/docker-login@v1
if: steps.getBranch.outputs.branch == 'master'
- name: set up docker buildx
uses: crazy-max/ghaction-docker-buildx@v3

- name: cache docker layers
uses: actions/cache@v2
with:
username: ${{ secrets.DOCKER_USER }}
password: ${{ secrets.DOCKER_PASS }}
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: ${{ runner.os }}-buildx-

- name: build docker image
run: |
docker buildx build \
--cache-from "type=local,src=/tmp/.buildx-cache" \
--cache-to "type=local,dest=/tmp/.buildx-cache" \
--platform linux/386,linux/amd64,linux/arm/v7,linux/arm64 \
--output "type=image,push=false" \
--tag getfider/fider .
# fails when used with buildx - see https://github.com/crazy-max/ghaction-docker-buildx/issues/230
# TODO: re-enable once issue above is fixed
# - uses: azure/docker-login@v1
# if: github.ref == 'refs/heads/master'
# with:
# username: ${{ secrets.DOCKER_USER }}
# password: ${{ secrets.DOCKER_PASS }}

- name: docker login
if: github.ref == 'refs/heads/master'
env:
DOCKER_USERNAME: ${{ secrets.DOCKER_USER }}
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASS }}
run: echo "${DOCKER_PASSWORD}" | docker login --username "${DOCKER_USERNAME}" --password-stdin

- name: push docker image
if: steps.getBranch.outputs.branch == 'master'
if: github.ref == 'refs/heads/master'
run: |
export SHORT_SHA=`git rev-parse --short=7 ${GITHUB_SHA}`
docker tag getfider/fider getfider/fider:${SHORT_SHA}
docker push getfider/fider:${SHORT_SHA}
docker tag getfider/fider getfider/fider:master
docker push getfider/fider:master
docker buildx build \
--cache-from "type=local,src=/tmp/.buildx-cache" \
--platform linux/386,linux/amd64,linux/arm/v7,linux/arm64 \
--tag getfider/fider:${SHORT_SHA} \
--tag getfider/fider:master \
--push .
- name: cleanup
if: always() && github.ref == 'refs/heads/master'
run: |
rm -f ${HOME}/.docker/config.json
6 changes: 5 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
# Build Step
FROM getfider/githubci:0.0.2 AS builder

ARG TARGETPLATFORM

RUN mkdir /app
WORKDIR /app

COPY . .
RUN npm ci
RUN node -v
RUN npm -v
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 mage build
RUN GOARCH="$(echo $TARGETPLATFORM | cut -d '/' -f 2)" \
GOARM="$(echo $TARGETPLATFORM | cut -d '/' -f 3 | tail -c 2)" \
CGO_ENABLED=0 GOOS=linux mage build

# Runtime Step
FROM alpine:3.10
Expand Down
28 changes: 12 additions & 16 deletions app/pkg/dbx/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"io/ioutil"
"os"
"sort"
"strconv"
"strings"

"github.com/getfider/fider/app/models/dto"
Expand All @@ -32,22 +31,19 @@ func Migrate(ctx context.Context, path string) error {
return errors.Wrap(err, "failed to read files from dir '%s'", path)
}

versions := make([]int, len(files))
versionFiles := make(map[int]string, len(files))
versions := make([]string, len(files))
versionFiles := make(map[string]string, len(files))
for i, file := range files {
fileName := file.Name()
parts := strings.Split(fileName, "_")
if len(parts[0]) != 12 {
return errors.New("migration file must have exactly 12 chars for version: '%s' is invalid.", fileName)
}

versions[i], err = strconv.Atoi(parts[0])
versions[i] = parts[0]
versionFiles[versions[i]] = fileName
if err != nil {
return errors.Wrap(err, "failed to convert '%s' to number", parts[0])
}
}
sort.Ints(versions)
sort.Strings(versions)

log.Infof(ctx, "Found total of @{Total} migration files.", dto.Props{
"Total": len(versions),
Expand Down Expand Up @@ -90,7 +86,7 @@ func Migrate(ctx context.Context, path string) error {
return nil
}

func runMigration(ctx context.Context, version int, path, fileName string) error {
func runMigration(ctx context.Context, version string, path, fileName string) error {
filePath := env.Path(path + "/" + fileName)
content, err := ioutil.ReadFile(filePath)
if err != nil {
Expand All @@ -115,29 +111,29 @@ func runMigration(ctx context.Context, version int, path, fileName string) error
return trx.Commit()
}

func getLastMigration() (int, error) {
func getLastMigration() (string, error) {
_, err := conn.Exec(`CREATE TABLE IF NOT EXISTS migrations_history (
version BIGINT PRIMARY KEY,
filename VARCHAR(100) null,
date TIMESTAMPTZ NOT NULL DEFAULT NOW()
)`)
if err != nil {
return 0, err
return "", err
}

var lastVersion sql.NullInt64
row := conn.QueryRow("SELECT MAX(version) FROM migrations_history LIMIT 1")
var lastVersion sql.NullString
row := conn.QueryRow("SELECT CAST(MAX(version) as varchar) FROM migrations_history LIMIT 1")
err = row.Scan(&lastVersion)
if err != nil {
return 0, err
return "", err
}

if !lastVersion.Valid {
// If it's the first run, maybe we have records on old migrations table, so try to get from it.
// This SHOULD be removed in the far future.
row := conn.QueryRow("SELECT version FROM schema_migrations LIMIT 1")
row := conn.QueryRow("SELECT CAST(version as varchar) FROM schema_migrations LIMIT 1")
_ = row.Scan(&lastVersion)
}

return int(lastVersion.Int64), nil
return lastVersion.String, nil
}
5 changes: 3 additions & 2 deletions magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ func (Build) All() {

func (Build) Server() error {
env := map[string]string{
"GOOS": runtime.GOOS,
"GOARCH": runtime.GOARCH,
"GOOS": os.Getenv("GOOS"),
"GOARCH": os.Getenv("GOARCH"),
"GOARM": os.Getenv("GOARM"),
}
ldflags := "-s -w -X main.buildtime=" + buildTime + " -X main.buildnumber=" + buildNumber
return sh.RunWith(env, "go", "build", "-ldflags", ldflags, "-o", exeName, ".")
Expand Down

0 comments on commit a02c959

Please sign in to comment.