Skip to content

Commit cf2f6e8

Browse files
committed
created a github actions workflow file to push latest image to AWS ECR
1 parent f2a39e1 commit cf2f6e8

File tree

5 files changed

+60
-11
lines changed

5 files changed

+60
-11
lines changed

.github/workflows/push_image.yaml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Build and Push Docker Image to AWS ECR
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
build-and-push:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- name: Checkout code
14+
uses: actions/checkout@v3
15+
16+
- name: Configure AWS credentials
17+
uses: aws-actions/configure-aws-credentials@v2
18+
with:
19+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
20+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
21+
aws-region: ap-south-1
22+
23+
- name: Login to Amazon ECR
24+
id: login-ecr
25+
uses: aws-actions/amazon-ecr-login@v2
26+
27+
- name: Set up Docker Buildx
28+
uses: docker/setup-buildx-action@v3
29+
30+
- name: Build and push Docker image to ECR
31+
uses: docker/build-push-action@v5
32+
with:
33+
context: ./backup/backend # Same as your previous
34+
push: true
35+
tags: 111654129626.dkr.ecr.ap-south-1.amazonaws.com/video-transcoder:latest

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
*.env
2-
env_vars.sh
2+
env_vars.sh
3+
bootstrap
4+
*.zip

aws/conf.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,33 @@ package aws_conf
33
import (
44
"context"
55
"fmt"
6+
"os"
67

78
"github.com/aws/aws-sdk-go-v2/aws"
89
"github.com/aws/aws-sdk-go-v2/config"
10+
"github.com/aws/aws-sdk-go-v2/credentials"
911
"github.com/aws/aws-sdk-go-v2/service/ecs"
1012
"github.com/aws/aws-sdk-go-v2/service/s3"
1113
)
1214

1315
// GetAwsConf loads the AWS configuration
1416
// It returns an aws.Config object and an error if any occurred
17+
1518
func GetAwsConf() (aws.Config, error) {
16-
// Load the default AWS configuration with the specified region
17-
cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion("ap-south-1"))
19+
// Hardcoded credentials (not recommended for prod)
20+
accessKey := os.Getenv("AWS_ACCESS_KEY")
21+
secretKey := os.Getenv("AWS_SECRET_KEY")
22+
23+
// Load the AWS configuration with custom credentials and region
24+
cfg, err := config.LoadDefaultConfig(context.TODO(),
25+
config.WithRegion("ap-south-1"),
26+
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(accessKey, secretKey, "")),
27+
)
28+
1829
if err != nil {
19-
// Return an empty config and a formatted error message
20-
return aws.Config{}, fmt.Errorf("unable to load sdk config: %v", err.Error())
30+
return aws.Config{}, fmt.Errorf("unable to load SDK config: %v", err)
2131
}
22-
// Return the loaded configuration and no error
32+
2333
return cfg, nil
2434
}
2535

cmd/ec2Worker/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func main() {
2626
DESTINATION_BUCKET_NAME := os.Getenv("DESTINATION_BUCKET_NAME")
2727

2828
if AWS_ACCESS_KEY_ID == "" || AWS_SECRET_ACCESS_KEY == "" || DESTINATION_BUCKET_NAME == "" {
29-
log.Fatal("Some required env are needed to proceed")
29+
log.Fatal("Some required env are needed to proceed", AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, DESTINATION_BUCKET_NAME)
3030
}
3131

3232
// START PROCESS

controllers/s3Controller/putImagePreSignedUrl.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package s3controller
33
import (
44
"context"
55
"net/http"
6+
"os"
67
"time"
78

89
"github.com/aws/aws-sdk-go-v2/aws"
@@ -59,13 +60,14 @@ func PreSignedUrlToPutImage(c *gin.Context) {
5960

6061
key := "videos/" + fileinfo.Email + "/" + uniqueKey + fileinfo.Filename
6162
input := &s3.PutObjectInput{
62-
Bucket: aws.String("harsh082ip.test"),
63-
Key: aws.String(key),
63+
Bucket: aws.String(os.Getenv("AWS_BUCKET")),
64+
Key: aws.String(key),
65+
ContentType: aws.String("video/mp4"),
6466
}
6567

6668
presignedClient := s3.NewPresignClient(s3Client)
6769

68-
presignedURL, err := presignedClient.PresignPutObject(context.TODO(), input, s3.WithPresignExpires(5*time.Minute))
70+
presignedURL, err := presignedClient.PresignPutObject(context.TODO(), input, s3.WithPresignExpires(15*time.Minute))
6971
if err != nil {
7072
c.JSON(http.StatusInternalServerError, gin.H{
7173
"status": "Error in creating a pre-signed url :/",
@@ -76,6 +78,6 @@ func PreSignedUrlToPutImage(c *gin.Context) {
7678
c.JSON(http.StatusOK, gin.H{
7779
"msg": "Pre-Signed Url Creation Success",
7880
"URL": presignedURL,
79-
"expires in": "5 Minutes",
81+
"expires in": "15 Minutes",
8082
})
8183
}

0 commit comments

Comments
 (0)