diff --git a/.github/actions/deploy/backend/action.yml b/.github/actions/deploy/backend/action.yml index 19e1b112..df6b1238 100644 --- a/.github/actions/deploy/backend/action.yml +++ b/.github/actions/deploy/backend/action.yml @@ -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' @@ -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/ssh-deploy@v5.1.0 diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 6fa7cda5..6b443699 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -21,6 +21,15 @@ 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: @@ -28,15 +37,11 @@ jobs: 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 diff --git a/go/cmd/main.go b/go/cmd/main.go index 209b9f1d..bfb164cd 100644 --- a/go/cmd/main.go +++ b/go/cmd/main.go @@ -1,6 +1,8 @@ package main import ( + "os" + "github.com/bufbuild/protovalidate-go" "go.uber.org/fx" "go.uber.org/zap" @@ -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, diff --git a/go/pkg/db/db.go b/go/pkg/db/db.go index 880bfe94..1272cee4 100644 --- a/go/pkg/db/db.go +++ b/go/pkg/db/db.go @@ -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 {