Merge pull request #50 from IntelliSOFT-Consulting/fix-schedule-bugs #47
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: React App Deployment # name of the workflow | |
on: | |
push: | |
branches: | |
- main # branch name | |
env: | |
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }} | |
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }} | |
SSH_PORT: 22 | |
CONTAINER_NAME: chanjokeclient | |
IMAGE_NAME: ${{ secrets.DOCKER_USERNAME }}/chanjokeclient:latest | |
SSH_PASS: ${{ secrets.SSH_PRIVATE_KEY }} | |
SSH_HOST: ${{ secrets.SSH_HOST }} | |
SSH_USER: ${{ secrets.SSH_USER }} | |
SUDO_PASSWORD: ${{ secrets.SUDO_PASSWORD }} | |
jobs: | |
build_frontend_app: | |
runs-on: ubuntu-latest | |
env: | |
EXAMPLE: example # environment variable | |
steps: | |
# checkout the repository content to github runner | |
- name: Checkout | |
uses: actions/checkout@v2 | |
# setup nodejs environment | |
- name: Setup Node.js environment | |
uses: actions/setup-node@v2.1.5 | |
with: | |
node-version: "18.16.1" | |
# cache the dependencies to speed up the build | |
- name: Cache dependencies | |
uses: actions/cache@v2 | |
with: | |
path: ~/.npm | |
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} | |
restore-keys: | | |
${{ runner.os }}-node- | |
# install dependencies | |
- name: Install dependencies | |
run: npm i | |
# build the react app | |
- name: Build & push Docker image | |
run: | | |
echo '${{ env.DOCKER_PASSWORD }}' | docker login -u ${{ env.DOCKER_USERNAME }} --password-stdin | |
docker buildx create --use --name workflowBuilder | |
docker buildx build --platform=linux/amd64,linux/arm64 -t ${{ env.IMAGE_NAME }} --push . | |
docker logout | |
- name: Setup SSH keys | |
uses: webfactory/ssh-agent@v0.7.0 | |
with: | |
ssh-private-key: ${{ env.SSH_PASS }} | |
- name: Remote ssh to server and update the running container | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ env.SSH_HOST }} | |
username: ${{ env.SSH_USER }} | |
port: ${{ env.SSH_PORT }} | |
key: ${{ env.SSH_PASS }} | |
script_stop: true | |
script: | | |
if docker ps -a --format '{{.Names}}' | grep -q ${{ env.CONTAINER_NAME }}; then | |
echo "Check 1" | |
docker stop ${{ env.CONTAINER_NAME }} | |
docker rm ${{ env.CONTAINER_NAME }} | |
fi | |
# Login to Docker Hub | |
echo '${{ env.DOCKER_PASSWORD }}' | docker login -u ${{ env.DOCKER_USERNAME }} --password-stdin | |
if [ $? -eq 0 ]; then | |
echo "Docker login successful" | |
else | |
echo "Error: Docker login failed" | |
exit 1 | |
fi | |
docker pull --quiet ${{ env.IMAGE_NAME }} | |
if [ $? -eq 0 ]; then | |
echo "Docker image ${{ env.IMAGE_NAME }} pulled successfully" | |
else | |
echo "Error: Failed to pull Docker image ${{ env.IMAGE_NAME }}" | |
exit 1 | |
fi | |
# Run Docker commands with sudo and pass the password | |
echo '${{ env.SUDO_PASSWORD }}' | sudo -S docker run -d --name ${{ env.CONTAINER_NAME }} -p 8098:3000 ${{ env.IMAGE_NAME }} | |
sudo docker logout | |
sleep 10 | |