Skip to content

Commit

Permalink
tooling: build 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 8f91c1d
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 27 deletions.
68 changes: 49 additions & 19 deletions .github/workflows/fider.yml
Expand Up @@ -3,10 +3,10 @@ name: fider
on:
push:
branches:
- master
- master
pull_request:
branches:
- master
- master

jobs:
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,7 +43,7 @@ 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:
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

- name: set up docker buildx
uses: crazy-max/ghaction-docker-buildx@v3

- uses: azure/docker-login@v1
if: steps.getBranch.outputs.branch == 'master'
- 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
@@ -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
24 changes: 19 additions & 5 deletions app/pkg/dbx/migrate.go
Expand Up @@ -7,8 +7,8 @@ import (
"io/ioutil"
"os"
"sort"
"strconv"
"strings"
"time"

"github.com/getfider/fider/app/models/dto"
"github.com/getfider/fider/app/pkg/env"
Expand Down Expand Up @@ -41,7 +41,7 @@ func Migrate(ctx context.Context, path string) error {
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], err = versionToInt(parts[0])
versionFiles[versions[i]] = fileName
if err != nil {
return errors.Wrap(err, "failed to convert '%s' to number", parts[0])
Expand Down Expand Up @@ -107,7 +107,7 @@ func runMigration(ctx context.Context, version int, path, fileName string) error
return err
}

_, err = trx.Execute("INSERT INTO migrations_history (version, filename) VALUES ($1, $2)", version, fileName)
_, err = trx.Execute("INSERT INTO migrations_history (version, filename) VALUES ($1, $2)", intToVersion(version), fileName)
if err != nil {
return err
}
Expand All @@ -125,7 +125,7 @@ func getLastMigration() (int, error) {
return 0, err
}

var lastVersion sql.NullInt64
var lastVersion sql.NullString
row := conn.QueryRow("SELECT MAX(version) FROM migrations_history LIMIT 1")
err = row.Scan(&lastVersion)
if err != nil {
Expand All @@ -139,5 +139,19 @@ func getLastMigration() (int, error) {
_ = row.Scan(&lastVersion)
}

return int(lastVersion.Int64), nil
return versionToInt(lastVersion.String)
}

const timeFormat = "200601021504"

func versionToInt(s string) (int, error) {
t, err := time.Parse(timeFormat, s)
if err != nil {
return 0, errors.Wrap(err, "failed to convert '%s' to number", s)
}
return int(t.Unix()), nil
}

func intToVersion(i int) string {
return time.Unix(int64(i), 0).Format(timeFormat)
}
5 changes: 3 additions & 2 deletions magefile.go
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 8f91c1d

Please sign in to comment.