Skip to content

Commit

Permalink
deploy qa
Browse files Browse the repository at this point in the history
  • Loading branch information
sang-d committed Jun 15, 2020
1 parent ad73ffc commit bf8e441
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 16 deletions.
6 changes: 4 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
app-name: gitwize
org-name: wizeline
- run:
command: serverless deploy -v
command: ./scripts/deploy.sh
name: deploy
- slack/status:
fail_only: false
Expand All @@ -59,4 +59,6 @@ workflows:
branches:
only:
- master
- /deploy-.*/
tags:
only:
only: /^v.*/
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@ https://www.serverless.com/framework/docs/providers/aws/guide/quick-start/

- install aws cli and config with aws credentials (user in aws iam `lambda`) and config region `ap-southeast-1`

- build and deploy: `make && sls deploy`
- build and deploy dev: `make && sls deploy --stage dev`

- build and deploy qa: `make && sls deploy --stage qa`
15 changes: 7 additions & 8 deletions functions/update_all_repos/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"github.com/GitWize/gitwize-lambda/db"
"github.com/GitWize/gitwize-lambda/gogit"
"github.com/GitWize/gitwize-lambda/utils"
"github.com/aws/aws-lambda-go/events"
lbd "github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go/aws"
Expand All @@ -15,24 +16,21 @@ import (
)

const (
updateOneRepoFuncName = "gitwize-lambda-dev-update_one_repo"
awsRegion = "ap-southeast-1"
defaultBranch = ""
awsRegion = "ap-southeast-1"
)

func triggerLambda(p interface{}) {
func triggerLambda(p interface{}, functionName string) {
payload, err := json.Marshal(p)
if err != nil {
log.Println("ERR", err)
return
}

mySession := session.Must(session.NewSession())
svc := lambda.New(mySession, aws.NewConfig().WithRegion(awsRegion))

input := &lambda.InvokeInput{
InvocationType: aws.String("Event"),
FunctionName: aws.String(updateOneRepoFuncName),
FunctionName: aws.String(functionName),
Payload: payload,
LogType: aws.String("Tail"),
}
Expand All @@ -59,6 +57,7 @@ func updateAllRepos() {
log.Printf("[WARN] No repositories found")
return
}

count := 0

for rows.Next() {
Expand All @@ -72,9 +71,9 @@ func updateAllRepos() {
URL: url,
RepoName: name,
RepoPass: password,
Branch: defaultBranch,
Branch: "",
}
triggerLambda(payload)
triggerLambda(payload, utils.GetUpdateOneRepoFuncName())
}
}
log.Println("Completed trigger update ", count, "repositories")
Expand Down
1 change: 0 additions & 1 deletion functions/update_single/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
func Handler(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
log.Printf("Processing request data for request %s.\n", request.RequestContext.RequestID)
log.Println("querystring", request.QueryStringParameters)
log.Println("pathparam", request.PathParameters)
conn := db.SQLDBConn()
defer conn.Close()

Expand Down
3 changes: 0 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"github.com/GitWize/gitwize-lambda/db"
"github.com/GitWize/gitwize-lambda/gogit"
"log"
"os"
Expand All @@ -19,7 +18,5 @@ func main() {

url = "https://github.com/wizeline/gitwize-be"
go gogit.GetRepo("gitwize-be", url, token)

db.UpdateMetricTable()
time.Sleep(10 * time.Second)
}
14 changes: 14 additions & 0 deletions scripts/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env bash

export APP_STAGE=dev
export PATTERN="v[0-9]+(\.[0-9]+).*"
if [[ ${CIRCLE_TAG} =~ ${PATTERN} ]]; then
export APP_STAGE=qa
export DB_CONN_STRING=${DB_CONN_STRING_QA}
export DEFAULT_GITHUB_TOKEN=${DEFAULT_GITHUB_TOKEN_QA}
export CYPHER_PASS_PHASE=${CYPHER_PASS_PHASE_QA}
export USE_DEFAULT_API_TOKEN=${USE_DEFAULT_API_TOKEN_QA}
echo "deploy to QA"
fi

sls deploy --stage ${APP_STAGE} --verbose
6 changes: 5 additions & 1 deletion serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ provider:
runtime: go1.x

# you can overwrite defaults here
stage: dev
stage: ${opt:stage, 'dev'}
region: ap-southeast-1


Expand Down Expand Up @@ -62,6 +62,7 @@ functions:
USE_DEFAULT_API_TOKEN: "TRUE"
DEFAULT_GITHUB_TOKEN: ${env:DEFAULT_GITHUB_TOKEN}
CYPHER_PASS_PHASE: ${env:CYPHER_PASS_PHASE}
APP_STAGE: ${opt:stage}
update_all_repos:
handler: bin/update_all_repos
memorySize: 1024
Expand All @@ -71,6 +72,7 @@ functions:
USE_DEFAULT_API_TOKEN: "TRUE"
DEFAULT_GITHUB_TOKEN: ${env:DEFAULT_GITHUB_TOKEN}
CYPHER_PASS_PHASE: ${env:CYPHER_PASS_PHASE}
APP_STAGE: ${opt:stage}
events:
- http:
path: update_all_repos
Expand All @@ -85,6 +87,7 @@ functions:
USE_DEFAULT_API_TOKEN: "TRUE"
DEFAULT_GITHUB_TOKEN: ${env:DEFAULT_GITHUB_TOKEN}
CYPHER_PASS_PHASE: ${env:CYPHER_PASS_PHASE}
APP_STAGE: ${opt:stage}
events:
- http:
path: update_single
Expand All @@ -95,6 +98,7 @@ functions:
timeout: 900
environment:
DB_CONN_STRING: ${env:DB_CONN_STRING}
APP_STAGE: ${opt:stage}
events:
- http:
path: load_metric
Expand Down
18 changes: 18 additions & 0 deletions utils/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import (
"time"
)

const (
functionPrefix = "gitwize-lambda-"
)

// TimeTrack use with defer to track processing time of a function
func TimeTrack(start time.Time, name string) {
elapsed := time.Since(start)
Expand All @@ -23,3 +27,17 @@ func GetAccessToken(repoPass string) (accessToken string) {
}
return accessToken
}

//GetAppStage return deployed environment dev/qa/prod...
func GetAppStage() string {
stage := os.Getenv("APP_STAGE")
if stage == "" {
stage = "dev"
}
return stage
}

//GetUpdateOneRepoFuncName return update-one-repo function name
func GetUpdateOneRepoFuncName() string {
return functionPrefix + GetAppStage() + "-update_one_repo"
}

0 comments on commit bf8e441

Please sign in to comment.