Skip to content

Commit

Permalink
chore: Update golang.yml workflow to copy golang-build-deploy.sh from…
Browse files Browse the repository at this point in the history
… the correct directory
  • Loading branch information
tanjilbhuiyan committed May 16, 2024
1 parent 15a5adb commit b913895
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 19 deletions.
34 changes: 17 additions & 17 deletions .github/scripts/golang-build-deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,23 @@ while read line; do
# Run the build command in the directory
echo "Building in directory $directory_path:"
# Run the build command in the directory and capture the output
build_output=$(GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build -o bootstrap 2>&1)
zip_name=$(echo "$directory_path" | awk -F'/' '{print $2}')

# Check the exit status of the build command
if [ $? -eq 0 ]; then
echo "Build in directory $directory_path succeeded:"
echo "$build_output"
# Zip the bootstrap file
zip -q "$zip_name.zip" bootstrap
echo "Bootstrap file zipped as bootstrap.zip"
echo "Uploading to AWS S3"
aws s3 cp "$zip_name.zip" "s3://$S3_BUCKET_NAME/$zip_name.zip"

else
echo "Build in directory $directory_path failed:"
echo "$build_output"
fi
# build_output=$(GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build -o bootstrap 2>&1)
# zip_name=$(echo "$directory_path" | awk -F'/' '{print $2}')

# # Check the exit status of the build command
# if [ $? -eq 0 ]; then
# echo "Build in directory $directory_path succeeded:"
# echo "$build_output"
# # Zip the bootstrap file
# zip -q "$zip_name.zip" bootstrap
# echo "Bootstrap file zipped as bootstrap.zip"
# echo "Uploading to AWS S3"
# aws s3 cp "$zip_name.zip" "s3://$S3_BUCKET_NAME/$zip_name.zip"

# else
# echo "Build in directory $directory_path failed:"
# echo "$build_output"
# fi

# Return to the original directory
cd "$current_dir"
Expand Down
72 changes: 72 additions & 0 deletions golang-build-deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/bin/bash

file=$1
S3_BUCKET_NAME="my-unique-bucket-golang-lambda-test"

while read line; do
# Remove starting and ending square brackets using parameter expansion
line="${line#\[}" # Remove starting square bracket
line="${line%\]}" # Remove ending square bracket
printf '%s\n' "$line"
IFS=',' read -ra parts <<< "$line"

# Iterate over the split parts and echo each one
for part in "${parts[@]}"; do
# Remove any leading or trailing spaces
directory_path=$(echo "$part" | tr -d '[:space:]' | sed 's/"//g' | sed 's#^github/##;s#/main\.go$##')
printf '%s\n' "$directory_path"

# Extract the path without the filename and the initial 'lambda/' part
zip_name=$(dirname "$directory_path" | sed 's|^lambda/||')

# Determine if the last segment is 'src' or not
last_segment=$(basename "$zip_name")

if [ "$last_segment" = "src" ]; then
# Remove the last segment if it is 'src'
zip_name =$(dirname "$zip_name")
fi

# Replace '/' with '_'
result=$(echo "$zip_name" | tr '/' '_')

echo "$result"


# Check if the directory exists
if [ -d "$directory_path" ]; then
# Store the current directory
current_dir="$(pwd)"

# Change to the target directory
cd "$directory_path"

# Run the build command in the directory
echo "Building in directory $directory_path:"
# Run the build command in the directory and capture the output
build_output=$(GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build -o bootstrap 2>&1)
# zip_name=$(echo "$directory_path" | awk -F'/' '{print $2}')

# Check the exit status of the build command
if [ $? -eq 0 ]; then
echo "Build in directory $directory_path succeeded:"
echo "$build_output"
# Zip the bootstrap file
zip -q "$result.zip" bootstrap
echo "Bootstrap file zipped as bootstrap.zip"
echo "Uploading to AWS S3"
aws s3 cp "$result.zip" "s3://$S3_BUCKET_NAME/$result.zip"

else
echo "Build in directory $directory_path failed:"
echo "$build_output"
fi

# Return to the original directory
cd "$current_dir"
else
echo "Directory $directory_path does not exist."
fi
done

done < list.txt
2 changes: 1 addition & 1 deletion lambda/github-lambda-test-1/src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ func handler(ctx context.Context) (string, error) {

func main() {
lambda.Start(handler)
}
}
2 changes: 1 addition & 1 deletion lambda/github-lambda-test-2/src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ func handler(ctx context.Context) (string, error) {

func main() {
lambda.Start(handler)
}
}
23 changes: 23 additions & 0 deletions lambda/payments/gateways/stripe/confirm/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Download package from S3
data "aws_s3_object" "application_zip" {
bucket = var.s3_bucket
key = "payments_gateways_stripe_confirm.zip"
}

# Lambda module to deploy using terraform
module "lambda_function" {
source = "terraform-aws-modules/lambda/aws"

function_name = "payments_gateways_stripe_confirm"
description = "This lambda is being deployed from github"
handler = "bootstrap"
runtime = "provided.al2023"
architectures = ["arm64"]
create_package = false
s3_existing_package = {
bucket = data.aws_s3_object.application_zip.bucket
key = data.aws_s3_object.application_zip.key
version_id = data.aws_s3_object.application_zip.version_id
}

}
5 changes: 5 additions & 0 deletions lambda/payments/gateways/stripe/confirm/src/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module main

go 1.21.1

require github.com/aws/aws-lambda-go v1.41.0
10 changes: 10 additions & 0 deletions lambda/payments/gateways/stripe/confirm/src/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/aws/aws-lambda-go v1.41.0 h1:l/5fyVb6Ud9uYd411xdHZzSf2n86TakxzpvIoz7l+3Y=
github.com/aws/aws-lambda-go v1.41.0/go.mod h1:jwFe2KmMsHmffA1X2R09hH6lFzJQxzI8qK17ewzbQMM=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s=
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
19 changes: 19 additions & 0 deletions lambda/payments/gateways/stripe/confirm/src/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
"context"
"fmt"

"github.com/aws/aws-lambda-go/lambda"
)

// handler is the main Lambda function handler
func handler(ctx context.Context) (string, error) {
message := "Hello, World!"
fmt.Println(message)
return message, nil
}

func main() {
lambda.Start(handler)
}
4 changes: 4 additions & 0 deletions lambda/payments/gateways/stripe/confirm/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# This variable is used to specify the S3 bucket name.
variable "s3_bucket" {
type = string
}
1 change: 1 addition & 0 deletions list.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["lambda/github-lambda-test-1/src/main.go","lambda/github-lambda-test-2/src/main.go"]

0 comments on commit b913895

Please sign in to comment.