chore: set up cicd #22
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Build and Run | |
on: | |
push: | |
branches: | |
- main | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout Repository | |
uses: actions/checkout@v3 | |
- name: Set up Go | |
uses: actions/setup-go@v4 | |
with: | |
go-version: 1.21.5 | |
- name: Build | |
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) | |