diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 0e1d383..9b64a3c 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -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) diff --git a/Dockerfile b/Dockerfile index a38a728..23a031d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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"] \ No newline at end of file diff --git a/go.mod b/go.mod index 6ad5725..9625bb4 100644 --- a/go.mod +++ b/go.mod @@ -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