Skip to content

Commit

Permalink
chore: set up cicd
Browse files Browse the repository at this point in the history
  • Loading branch information
dkrest1 committed Feb 6, 2024
1 parent a55dd8a commit 0d2b114
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 11 deletions.
36 changes: 35 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,42 @@ jobs:
go-version: 1.21.5

- name: Build
run: go build -v .
run: |
go mod download
go build -v .
env:
PORT: ${{ secrets.PORT }}
JWT_SECRET_KEY: ${{ secrets.JWT_SECRET_KEY }}

deploy:
runs-on: ubuntu-latest

steps:
- name: Checkout Repository
uses: actions/checkout@v3

- name: Set up Docker
uses: docker/setup-buildx-action@v1

- name: Build and Push Docker Image
run: |
echo ${{ secrets.DOCKER_PASSWORD }} | docker login -u ${{ secrets.DOCKER_USERNAME }} --password-stdin
docker buildx create --use
docker buildx build -t yourusername/yourapp:latest -f .
docker push yourusername/yourapp:latest
- name: SSH into EC2 Instance and Deploy
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USERNAME }}
key: ${{ secrets.EC2_PRIVATE_KEY }}
port: ${{ secrets.EC2_PORT }}

script: |
sleep 10
docker pull yourusername/yourapp:latest
docker stop yourapp || true
docker rm yourapp || true
docker run -d -p 8080:8080 --name yourapp yourusername/yourapp:latest || (echo "Deployment failed" && exit 1)
42 changes: 33 additions & 9 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,17 +1,41 @@
# Go image as the base image
# Mini golang base image
FROM golang:1.20-alpine as builder

# Set working directory
RUN apk add --no-cache build-base


# Create work dir
WORKDIR /app

# Copy application code into the working directory
# Copy go module files and download dependecy
COPY go.mod ./

COPY go.sum ./

RUN go mod download

# Copy the entire application to directory
COPY . .

# Builid the application
RUN go build -o main
RUN CGO_ENABLED=1 GOOS=linux go build -o /app -a -ldflags '-linkmode external -extldflags "-static"' .

# Build the application
RUN go build -o main .

# Image for fina stage
FROM alpine:latest

# create work dir
WORKDIR /app

# Copy built app to work directory
COPY --from=builder /app/main .

# Copy env file
COPY .env .env

# Expose application port
EXPOSE ${PORT}
# Expose port
EXPOSE 9090

# Run executable
CMD [ "./main" ]
# Run the executable
CMD ["./main"]
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/dkrest1/task-manager

go 1.21.5
go 1.21

require (
github.com/golang-jwt/jwt/v5 v5.2.0
Expand Down

0 comments on commit 0d2b114

Please sign in to comment.