Skip to content

Commit

Permalink
[Feature] Support to run with docker (#32)
Browse files Browse the repository at this point in the history
Adds Dockerfile to build image
Adds makefile docker image build
Updates readme
Renamed pr workflow to Pull Request
Renamed tag worflow to Release
Improved overall workflows syntax and names
Added deploy to dockerhub to Release workflow
Updated Makefile command build-docker-image
Updated README
  • Loading branch information
atedesch1 authored Jul 10, 2022
1 parent c2255e9 commit 597b976
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 11 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: pr
name: Pull Request Tests

on:
pull_request:
Expand All @@ -7,13 +7,16 @@ on:
jobs:
build:
runs-on: ubuntu-latest
name: Test
name: Build & Test

steps:
- uses: actions/checkout@v2
with:
go-version: '1.17.6'

- name: Build
run: make build

- name: Run tests
run: make check
- name: Upload coverage reports to Codecov with GitHub Action
Expand Down
57 changes: 49 additions & 8 deletions .github/workflows/tag.yml → .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
name: tag
name: Release

on:
push:
branches:
- master

jobs:
build-and-test:
runs-on: ubuntu-latest
name: Test
name: Build & Test

steps:
- uses: actions/checkout@v2
with:
go-version: '1.17.6'

- name: Build
run: make build

- name: Run tests
run: make check

- name: Upload coverage reports to Codecov with GitHub Action
run: |
curl -Os https://uploader.codecov.io/latest/linux/codecov
Expand All @@ -26,31 +31,40 @@ jobs:
tag:
runs-on: ubuntu-latest
needs: build-and-test
name: Generate & Push Tag

outputs:
tag: ${{ steps.tag_generator.outputs.version_tag }}

steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: tag

- name: Generate tag
id: tag_generator
uses: paulhatch/[email protected]
with:
tag_prefix: "v"
major_pattern: "(MAJOR)"
minor_pattern: "(MINOR)"
format: "${major}.${minor}.${patch}"
tag_prefix: 'v'
major_pattern: '(MAJOR)'
minor_pattern: '(MINOR)'
format: '${major}.${minor}.${patch}'
short_tags: false
bump_each_commit: false
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: git

- name: Push tag to github
run: |
git tag ${{ steps.tag_generator.outputs.version_tag }}
git push origin --tags
release:
runs-on: ubuntu-latest
needs: tag
name: Release

steps:
- name: Checkout
uses: actions/checkout@v2
Expand All @@ -68,3 +82,30 @@ jobs:
- name: Run GoReleaser
run: make release

deploy-docker:
runs-on: ubuntu-latest
needs: tag
name: Deploy to DockerHub

env:
DOCKER_USER: ${{ secrets.DOCKER_USER }}
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
DOCKER_TAG_LATEST_RELEASE: '${{ secrets.DOCKER_USER }}/mgr8:latest'
DOCKER_TAG_RELEASE: '${{ secrets.DOCKER_USER }}/mgr8:${{ needs.tag.outputs.tag }}'

steps:
- uses: actions/checkout@v2

- name: Docker login
run: |
docker login -u $DOCKER_USER -p $DOCKER_PASSWORD
- name: Build Docker image
run: |
docker build . --file Dockerfile --tag $DOCKER_TAG_LATEST_RELEASE $DOCKER_TAG_RELEASE
- name: Push to DockerHub
run: |
docker push $DOCKER_TAG_RELEASE
docker push $DOCKER_TAG_LATEST_RELEASE
23 changes: 23 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
FROM golang:1.17-alpine3.15 AS builder

RUN apk add --no-cache git gcc musl-dev make

WORKDIR /go/src/github.com/migratemgr8/mgr8

ENV GO111MODULE=on

COPY go.mod go.sum ./

RUN go mod download

COPY . ./

RUN make build

FROM alpine:3.15

COPY --from=builder /go/src/github.com/migratemgr8/mgr8/bin/mgr8 /usr/local/bin/mgr8
RUN ln -s /usr/local/bin/mgr8 /mgr8

ENTRYPOINT ["mgr8"]
CMD ["--help"]
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ GOLANG_CROSS_VERSION ?= v1.18.2
build: main.go
go build -o bin/mgr8 main.go

build-docker-image:
docker build . --file Dockerfile --tag migratemgr8/mgr8:latest

install-tools:
go install github.com/golang/mock/[email protected]

Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ When committing to a repository, check if the reference and the latest schema ma

To generate an empty migration (e.g. for DML), use `./bin/mgr8 generate empty`.

### Run with docker

Pull latest image with `docker pull migratemgr8/mgr8:latest` or build it yourself with `make build-docker-image`.

Run commands:
```bash
docker run -v {{ migrations path }}:/migrations --network host -e RUN_WITH_DOCKER=true -e MGR8_USERNAME={{ logs username }} -e DB_HOST={{ database connection string }} migratemgr8/mgr8 <command>
```
Make sure to replace the variables surrounded by double curly braces.

## Develop

### Requirements
Expand Down Expand Up @@ -102,11 +112,17 @@ To add a new mock, add new lines to the `mock` command in the Makefile.

Executing migrations with postgres driver
```bash
# CLI version
./bin/mgr8 apply up --database=postgres://root:root@localhost:5432/core?sslmode=disable --dir=./migrations
# Docker version
docker run -v $PWD/migrations:/migrations -e DB_HOST=postgres://root:root@localhost:5432/core?sslmode=disable --network host -e MGR8_USERNAME=username migratemgr8/mgr8 apply up
```

Executing migrations with mysql driver
```bash
# CLI version
./bin/mgr8 apply up --database=root:root@tcp\(localhost:3306\)/core --dir=./migrations --driver=mysql
# Docker version
docker run -v $PWD/migrations:/migrations -e DB_HOST=postgres://root:root@localhost:5432/core?sslmode=disable --network host -e MGR8_USERNAME=username migratemgr8/mgr8 apply up --driver=mysql
```

2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func Execute() {
}
applyCmd.Flags().StringVar(&applyCommand.databaseURL, "database", os.Getenv("DB_HOST"), "Database URL")
applyCmd.Flags().StringVar(&applyCommand.driverName, "driver", defaultDriverName, "Driver Name")
applyCmd.Flags().StringVar(&applyCommand.migrationsDir, "dir", "", "Migrations Directory")
applyCmd.Flags().StringVar(&applyCommand.migrationsDir, "dir", "migrations", "Migrations Directory")

validateCommand := Command{cmd: &validate{}}
validateCmd := &cobra.Command{
Expand Down

0 comments on commit 597b976

Please sign in to comment.