Skip to content

Commit

Permalink
gen: fixed generation of the workflow (#314)
Browse files Browse the repository at this point in the history
Co-authored-by: dorav <[email protected]>
Co-authored-by: Ariel Mashraki <[email protected]>
  • Loading branch information
3 people authored Sep 10, 2023
1 parent 41bc842 commit 7811e08
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 73 deletions.
63 changes: 4 additions & 59 deletions gen/atlas.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -12,55 +12,7 @@ permissions:
pull-requests: write
jobs:
lint:
{{- if eq .Driver "mysql" }}
services:
# Spin up a mysql:8 container to be used as the dev-database for analysis.
mysql:
image: mysql:8
env:
MYSQL_DATABASE: dev
MYSQL_ROOT_PASSWORD: pass
ports:
- 3306:3306
options: >-
--health-cmd "mysqladmin ping -ppass"
--health-interval 10s
--health-start-period 10s
--health-timeout 5s
--health-retries 10
{{- else if eq .Driver "postgres" }}
services:
# Spin up a postgres:15 container to be used as the dev-database for analysis.
postgres15:
image: postgres:15
env:
POSTGRES_DB: dev
POSTGRES_PASSWORD: pass
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-start-period 10s
--health-timeout 5s
--health-retries 5
{{- else if eq .Driver "mariadb" }}
services:
# Spin up a mariadb:11 container to be used as the dev-database for analysis.
maria11:
image: mariadb:11
env:
MYSQL_DATABASE: dev
MYSQL_ROOT_PASSWORD: pass
ports:
- 3306:3306
options: >-
--health-cmd "mysqladmin ping -ppass"
--health-interval 10s
--health-start-period 10s
--health-timeout 5s
--health-retries 10
{{- end }}
{{- template "services" . }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand All @@ -69,24 +21,17 @@ jobs:
- uses: ariga/atlas-action@v0
with:
dir: '{{ .Path }}'
{{- if eq .Driver "mysql" }}
dev-url: 'mysql://root:pass@localhost:3306/dev'
{{- else if eq .Driver "postgres" }}
dev-url: 'postgres://postgres:pass@localhost:5432/dev?sslmode=disable'
{{- else if eq .Driver "mariadb" }}
dev-url: 'maria://root:pass@localhost:3306/dev'
{{- else if eq .Driver "sqlite" }}
dev-url: 'sqlite://dev?mode=memory'
{{- end }}
{{ template "UseServices" (args .Driver "localhost") }}
cloud-token: {{`${{ secrets.`}}{{ .SecretName }}{{` }}`}}
sync:
needs: lint
{{- template "services" . }}
if: github.ref == 'refs/heads/{{ .DefaultBranch }}'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: ariga/atlas-sync-action@v0
with:
dir: '{{ .Path }}'
driver: {{ .Driver }}
{{ template "UseServices" (args .Driver .Driver) }}
cloud-token: {{`${{ secrets.`}}{{ .SecretName }}{{` }}`}}
22 changes: 13 additions & 9 deletions gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gen

import (
"bytes"
"embed"
_ "embed"
"fmt"
"text/template"
Expand All @@ -15,6 +16,7 @@ type (
SecretName string
DefaultBranch string
Driver string
Services string
}
)

Expand All @@ -28,17 +30,18 @@ func validateDriver(s string) error {
}

var (
//go:embed atlas.tmpl
resource string
tmpl *template.Template
//go:embed *.tmpl
files embed.FS

tmpl = template.Must(template.New("atlas-sync-action").
Funcs(argsFunc()).
ParseFS(files, "*.tmpl"))
)

func init() {
t, err := template.New("atlas.tmpl").Parse(resource)
if err != nil {
panic(err)
}
tmpl = t
func argsFunc() template.FuncMap {
return template.FuncMap{"args": func(els ...any) []any {
return els
}}
}

// Generate the content of the atlas ci lint yaml.
Expand All @@ -47,6 +50,7 @@ func Generate(cfg *Config) ([]byte, error) {
return nil, err
}
b := bytes.NewBuffer(nil)

if err := tmpl.ExecuteTemplate(b, "atlas.tmpl", cfg); err != nil {
return nil, err
}
Expand Down
65 changes: 65 additions & 0 deletions gen/services.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{{- define "services" }}
{{- if eq .Driver "mysql" }}
services:
# Spin up a mysql:8 container to be used as the dev-database for analysis.
mysql:
image: mysql:8
env:
MYSQL_DATABASE: dev
MYSQL_ROOT_PASSWORD: pass
ports:
- 3306:3306
options: >-
--health-cmd "mysqladmin ping -ppass"
--health-interval 10s
--health-start-period 10s
--health-timeout 5s
--health-retries 10
{{- else if eq .Driver "postgres" }}
services:
# Spin up a postgres:15 container to be used as the dev-database for analysis.
postgres:
image: postgres:15
env:
POSTGRES_DB: dev
POSTGRES_PASSWORD: pass
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-start-period 10s
--health-timeout 5s
--health-retries 5
{{- else if eq .Driver "mariadb" }}
services:
# Spin up a mariadb:11 container to be used as the dev-database for analysis.
mariadb:
image: mariadb:11
env:
MYSQL_DATABASE: dev
MYSQL_ROOT_PASSWORD: pass
ports:
- 3306:3306
options: >-
--health-cmd "healthcheck.sh --su-mysql --connect --innodb_initialized"
--health-interval 10s
--health-start-period 10s
--health-timeout 5s
--health-retries 10
{{- end }}
{{- end }}

{{- define "UseServices" }}
{{- $Driver := index . 0 }}
{{- $Host := index . 1 }}
{{- if eq $Driver "mysql" -}}
dev-url: 'mysql://root:pass@{{- $Host }}:3306/dev'
{{- else if eq $Driver "postgres" -}}
dev-url: 'postgres://postgres:pass@{{- $Host }}:5432/dev?search_path=public&sslmode=disable'
{{- else if eq $Driver "mariadb" -}}
dev-url: 'maria://root:pass@{{- $Host }}:3306/dev'
{{- else if eq $Driver "sqlite" -}}
dev-url: 'sqlite://dev?mode=memory'
{{- end -}}
{{- end -}}
65 changes: 65 additions & 0 deletions gen/testdata/mariadb.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
name: Atlas
on:
push:
branches:
- master
pull_request:
paths:
- 'migrations/*'
# Permissions to write comments on the pull request.
permissions:
contents: read
pull-requests: write
jobs:
lint:
services:
# Spin up a mariadb:11 container to be used as the dev-database for analysis.
mariadb:
image: mariadb:11
env:
MYSQL_DATABASE: dev
MYSQL_ROOT_PASSWORD: pass
ports:
- 3306:3306
options: >-
--health-cmd "healthcheck.sh --su-mysql --connect --innodb_initialized"
--health-interval 10s
--health-start-period 10s
--health-timeout 5s
--health-retries 10
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: ariga/atlas-action@v0
with:
dir: 'migrations'
dev-url: 'maria://root:pass@localhost:3306/dev'
cloud-token: ${{ secrets.ATLAS_CLOUD_TOKEN }}
sync:
needs: lint
services:
# Spin up a mariadb:11 container to be used as the dev-database for analysis.
mariadb:
image: mariadb:11
env:
MYSQL_DATABASE: dev
MYSQL_ROOT_PASSWORD: pass
ports:
- 3306:3306
options: >-
--health-cmd "healthcheck.sh --su-mysql --connect --innodb_initialized"
--health-interval 10s
--health-start-period 10s
--health-timeout 5s
--health-retries 10
if: github.ref == 'refs/heads/master'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: ariga/atlas-sync-action@v0
with:
dir: 'migrations'
dev-url: 'maria://root:pass@mariadb:3306/dev'
cloud-token: ${{ secrets.ATLAS_CLOUD_TOKEN }}
17 changes: 16 additions & 1 deletion gen/testdata/mysql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,27 @@ jobs:
cloud-token: ${{ secrets.ATLAS_CLOUD_TOKEN }}
sync:
needs: lint
services:
# Spin up a mysql:8 container to be used as the dev-database for analysis.
mysql:
image: mysql:8
env:
MYSQL_DATABASE: dev
MYSQL_ROOT_PASSWORD: pass
ports:
- 3306:3306
options: >-
--health-cmd "mysqladmin ping -ppass"
--health-interval 10s
--health-start-period 10s
--health-timeout 5s
--health-retries 10
if: github.ref == 'refs/heads/master'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: ariga/atlas-sync-action@v0
with:
dir: 'migrations'
driver: mysql
dev-url: 'mysql://root:pass@mysql:3306/dev'
cloud-token: ${{ secrets.ATLAS_CLOUD_TOKEN }}
21 changes: 18 additions & 3 deletions gen/testdata/postgres.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
lint:
services:
# Spin up a postgres:15 container to be used as the dev-database for analysis.
postgres15:
postgres:
image: postgres:15
env:
POSTGRES_DB: dev
Expand All @@ -35,16 +35,31 @@ jobs:
- uses: ariga/atlas-action@v0
with:
dir: 'migrations'
dev-url: 'postgres://postgres:pass@localhost:5432/dev?sslmode=disable'
dev-url: 'postgres://postgres:pass@localhost:5432/dev?search_path=public&sslmode=disable'
cloud-token: ${{ secrets.ATLAS_CLOUD_TOKEN }}
sync:
needs: lint
services:
# Spin up a postgres:15 container to be used as the dev-database for analysis.
postgres:
image: postgres:15
env:
POSTGRES_DB: dev
POSTGRES_PASSWORD: pass
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-start-period 10s
--health-timeout 5s
--health-retries 5
if: github.ref == 'refs/heads/master'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: ariga/atlas-sync-action@v0
with:
dir: 'migrations'
driver: postgres
dev-url: 'postgres://postgres:pass@postgres:5432/dev?search_path=public&sslmode=disable'
cloud-token: ${{ secrets.ATLAS_CLOUD_TOKEN }}
2 changes: 1 addition & 1 deletion gen/testdata/sqlite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ jobs:
- uses: ariga/atlas-sync-action@v0
with:
dir: 'migrations'
driver: sqlite
dev-url: 'sqlite://dev?mode=memory'
cloud-token: ${{ secrets.ATLAS_CLOUD_TOKEN }}

0 comments on commit 7811e08

Please sign in to comment.