Skip to content

Commit

Permalink
db env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
crlssn committed Nov 13, 2024
1 parent 4795fd2 commit 57de549
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 14 deletions.
22 changes: 21 additions & 1 deletion .github/actions/deploy/backend/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@ inputs:
aws_ec2_private_key:
description: "AWS EC2 Private Key"
required: true
db_host:
description: "Database Host"
required: true
db_port:
description: "Database Port"
required: true
db_user:
description: "Database User"
required: true
db_password:
description: "Database Password"
required: true
db_name:
description: "Database Name"
required: true

runs:
using: 'composite'
Expand All @@ -28,7 +43,12 @@ runs:
run: |
go mod download
GOOS=linux go build -o bin/app cmd/main.go
# zip bin.zip cmd/bin
env:
DB_HOST: ${{ inputs.db_host }}
DB_PORT: ${{ inputs.db_port }}
DB_USER: ${{ inputs.db_user }}
DB_PASSWORD: ${{ inputs.db_password }}
DB_NAME: ${{ inputs.db_name }}

- name: Deploy to my EC2 instance
uses: easingthemes/[email protected]
Expand Down
17 changes: 11 additions & 6 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,27 @@ jobs:
db_username: ${{ secrets.TF_VAR_DB_USERNAME }}
db_password: ${{ secrets.TF_VAR_DB_PASSWORD }}

- name: Migrate database
uses: ./.github/actions/migrate/database
with:
db_host: ${{ vars.DB_HOST }}
db_port: ${{ vars.DB_PORT }}
db_name: ${{ vars.DB_NAME }}
db_user: ${{ secrets.DB_USER }}
db_password: ${{ secrets.DB_PASSWORD }}

- name: Deploy backend to EC2
uses: ./.github/actions/deploy/backend
with:
aws_access_key_id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws_secret_access_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws_region: ${{ vars.AWS_REGION }}
aws_ec2_private_key: ${{ secrets.AWS_EC2_PRIVATE_KEY }}

- name: Migrate database
uses: ./.github/actions/migrate/database
with:
db_host: ${{ vars.DB_HOST }}
db_port: ${{ vars.DB_PORT }}
db_name: ${{ vars.DB_NAME }}
db_user: ${{ secrets.DB_USER }}
db_user: ${{ vars.DB_USER }}
db_password: ${{ secrets.DB_PASSWORD }}
db_name: ${{ vars.DB_NAME }}

- name: Deploy web app to S3
uses: ./.github/actions/deploy/web-app
Expand Down
12 changes: 7 additions & 5 deletions go/cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"os"

"github.com/bufbuild/protovalidate-go"
"go.uber.org/fx"
"go.uber.org/zap"
Expand All @@ -23,11 +25,11 @@ func options() []fx.Option {
fx.Provide(
func() db.Options {
return db.Options{
Host: "localhost",
Port: 5433,
User: "root",
Password: "root",
Database: "postgres",
Host: os.Getenv("DB_HOST"),
Port: os.Getenv("DB_PORT"),
User: os.Getenv("DB_USER"),
Password: os.Getenv("DB_PASSWORD"),
Database: os.Getenv("DB_NAME"),
}
},
db.New,
Expand Down
4 changes: 2 additions & 2 deletions go/pkg/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import (

type Options struct {
Host string
Port int
Port string
User string
Password string
Database string
}

func New(opts Options) (*sql.DB, error) {
return sql.Open("pgx", fmt.Sprintf("postgresql://%s:%s@%s:%d/%s", opts.User, opts.Password, opts.Host, opts.Port, opts.Database))
return sql.Open("pgx", fmt.Sprintf("postgresql://%s:%s@%s:%s/%s", opts.User, opts.Password, opts.Host, opts.Port, opts.Database))
}

func MustNewTest() *sql.DB {
Expand Down

0 comments on commit 57de549

Please sign in to comment.