Skip to content

Commit

Permalink
docker deploy with bash file
Browse files Browse the repository at this point in the history
  • Loading branch information
nemanjam committed Jun 7, 2024
1 parent 4bf7a29 commit fa4b0ff
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 2 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/deploy-docker-bash.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Deploy Docker with bash file

on:
workflow_run:
workflows: ['Build and push Docker - disabled']
types:
- completed

workflow_dispatch:

env:
IMAGE_NAME_WITH_TAG: ${{ secrets.DOCKER_USERNAME }}/${{ github.event.repository.name }}:latest

jobs:
deploy:
name: Deploy image from Dockerhub to remote host with ssh
runs-on: ubuntu-latest

steps:
- name: Set up SSH config
run: |
# Set up SSH key
mkdir -p ~/.ssh
echo "${{ secrets.REMOTE_KEY_ED25519 }}" > ~/.ssh/id_ed25519
chmod 600 ~/.ssh/id_ed25519
# Set up SSH config
echo "Host arm1" > ~/.ssh/config
echo " HostName ${{ secrets.REMOTE_HOST }}" >> ~/.ssh/config
echo " User ${{ secrets.REMOTE_USERNAME }}" >> ~/.ssh/config
echo " IdentityFile ~/.ssh/id_ed25519" >> ~/.ssh/config
echo " Port ${{ secrets.REMOTE_PORT }}" >> ~/.ssh/config
chmod 600 ~/.ssh/config
# Add SSH server key to known_hosts
ssh-keyscan -H ${{ secrets.REMOTE_HOST }} >> ~/.ssh/known_hosts
- name: Run bash file with yarn
run: yarn deploy:docker
20 changes: 20 additions & 0 deletions docs/temp-code/deploy-docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

REMOTE_HOST=$1
REMOTE_PATH=$2
IMAGE_NAME_WITH_TAG=$3

# Check if all arguments are provided
if [[ -z "$REMOTE_PATH" || -z "$REMOTE_HOST" || -z "$IMAGE_NAME_WITH_TAG" ]]; then
echo "Incorrect args, usage: $0 <remote_path> <remote_host> <image_name>"
exit 1
fi

# Navigate to the docker-compose.yml folder
ssh $REMOTE_HOST "cd $REMOTE_PATH && \
docker compose down && \
docker image rm $IMAGE_NAME_WITH_TAG && \
docker compose up -d"

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
"format": "prettier-config",
"clean": "rm -rf node_modules yarn.lock",
"check-types": "tsc --noEmit --pretty",
"deploy:nginx": "bash scripts/deploy-nginx-local.sh '~/traefik-proxy/apps/nmc-nginx-with-volume/website' arm1",
"deploy:nginx:local": "bash scripts/deploy-nginx-local.sh '~/traefik-proxy/apps/nmc-nginx-with-volume/website' lxc11",
"deploy:nginx": "bash scripts/deploy-nginx.sh '~/traefik-proxy/apps/nmc-nginx-with-volume/website' arm1",
"deploy:nginx:local": "bash scripts/deploy-nginx.sh '~/traefik-proxy/apps/nmc-nginx-with-volume/website' lxc11",
"deploy:docker": "bash scripts/deploy-docker.sh arm1 '~/traefik-proxy/apps/nmc-docker' nemanjamitic/nemanjam.github.io",
"deploy:docker:local": "bash scripts/deploy-docker.sh lxc11 '~/traefik-proxy/apps/nmc-docker' nemanjamitic/nemanjam.github.io",
"docker:build:push:arm": "docker buildx build -f ./docker/Dockerfile -t nemanjamitic/nemanjam.github.io --build-arg ARG_SITE_URL='https://nmc-docker.arm1.nemanjamitic.com' --platform linux/arm64 --push .",
"docker:build:push:x86": "docker buildx build -f ./docker/Dockerfile -t nemanjamitic/nemanjam.github.io --build-arg ARG_SITE_URL='https://nmc-docker.local.nemanjamitic.com' --platform linux/amd64 --push .",
"docker:push": "docker push nemanjamitic/nemanjam.github.io"
Expand Down
29 changes: 29 additions & 0 deletions scripts/deploy-docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash

# REMOTE_PATH="~/traefik-proxy/apps/nmc-docker"
# REMOTE_HOST="arm1"

REMOTE_HOST=$1
REMOTE_PATH=$2
IMAGE_NAME_WITH_TAG=$3

# Check if all arguments are provided
if [[ -z "$REMOTE_PATH" || -z "$REMOTE_HOST" || -z "$IMAGE_NAME_WITH_TAG" ]]; then
echo "Incorrect args, usage: $0 <remote_path> <remote_host> <image_name>"
exit 1
fi

# Navigate to the docker-compose.yml folder
ssh $REMOTE_HOST "cd $REMOTE_PATH && \
echo 'Old image id:' && \
\$(docker image ls --format='{{.Repository}}:{{.ID}}' | grep '$IMAGE_NAME_WITH_TAG')
docker compose down && \
docker image rm '$IMAGE_NAME_WITH_TAG' && \
docker compose up -d && \
echo 'New image id:' && \
\$(docker image ls --format='{{.Repository}}:{{.ID}}' | grep '$IMAGE_NAME_WITH_TAG')"

File renamed without changes.

0 comments on commit fa4b0ff

Please sign in to comment.