Create docker_build_and_deploy.yml #3
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: Docker Image CI and Deploy to Amazon EC2 | |
on: | |
push: | |
branches: [ "main" ] | |
pull_request: | |
branches: [ "main" ] | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Docker Setup Buildx | |
uses: docker/[email protected] | |
- name: Login to DockerHub | |
uses: docker/[email protected] | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_PASSWORD }} | |
- name: Set image name with date | |
run: echo "IMAGE_NAME=bada308/mooluck-node:$(date +%s)" >> $GITHUB_ENV | |
- name: Build and push the Docker image | |
run: | | |
docker build . --file Dockerfile -t ${{ env.IMAGE_NAME }} | |
docker push ${{ env.IMAGE_NAME }} | |
- name: Save image name to file | |
run: echo ${{ env.IMAGE_NAME }} > image-name.txt | |
- name: Upload image name artifact | |
uses: actions/[email protected] | |
with: | |
name: image-name | |
path: image-name.txt # Adjusted path | |
deploy: | |
runs-on: ubuntu-latest | |
needs: build | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Download image name artifact | |
uses: actions/[email protected] | |
with: | |
name: image-name | |
- name: Read image name from file | |
id: read-image-name | |
run: echo "IMAGE_NAME=$(cat image-name.txt)" >> $GITHUB_ENV | |
- name: SSH into EC2 and deploy | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ secrets.EC2_HOST }} | |
username: ${{ secrets.EC2_USER }} | |
key: ${{ secrets.EC2_SSH_KEY }} | |
script: | | |
sudo docker login -u ${{ secrets.DOCKERHUB_USERNAME }} -p ${{ secrets.DOCKERHUB_PASSWORD }} | |
sudo docker pull ${{ env.IMAGE_NAME }} | |
sudo docker stop mooluck-node || true | |
sudo docker rm mooluck-node || true | |
sudo docker run -d --name mooluck-node -p 3000:8080 ${{ env.IMAGE_NAME }} \ | |
-e LIVEKIT_API_KEY="${{ secrets.LIVEKIT_API_KEY }}" \ | |
-e LIVEKIT_API_SECRET="${{ secrets.LIVEKIT_API_SECRET }}" \ | |
-e LOCALHOST_URL=${{ secrets.LOCALHOST_URL }} \ | |
-e APP_URL=${{ secrets.APP_URL }} |