Skip to content

Commit cb6eb36

Browse files
authored
Refactor Docker workflow to include deployment
Updated workflow to include deployment steps after building and pushing the Docker image.
1 parent 69efb5a commit cb6eb36

1 file changed

Lines changed: 41 additions & 3 deletions

File tree

.github/workflows/docker-image.yml

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Build and Push Docker Image
1+
name: Build, Push and Deploy
22

33
on:
44
push:
@@ -9,7 +9,7 @@ env:
99
REGISTRY: ghcr.io
1010

1111
jobs:
12-
build:
12+
build-and-deploy:
1313
runs-on: ubuntu-latest
1414

1515
permissions:
@@ -24,7 +24,7 @@ jobs:
2424
run: |
2525
echo "IMAGE_NAME=${GITHUB_REPOSITORY,,}" >> $GITHUB_ENV
2626
27-
- name: Login to GitHub Container Registry
27+
- name: Login to GitHub Container Registry (CI)
2828
run: |
2929
echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io \
3030
-u "${{ github.actor }}" \
@@ -42,3 +42,41 @@ jobs:
4242
run: |
4343
docker push $REGISTRY/$IMAGE_NAME:latest
4444
docker push $REGISTRY/$IMAGE_NAME:${{ github.sha }}
45+
46+
# ---------- DEPLOY ----------
47+
- name: Setup SSH
48+
run: |
49+
mkdir -p ~/.ssh
50+
echo "${{ secrets.DO_SSH_KEY }}" > ~/.ssh/id_ed25519
51+
chmod 600 ~/.ssh/id_ed25519
52+
ssh-keyscan -H ${{ secrets.DO_HOST }} >> ~/.ssh/known_hosts
53+
54+
- name: Deploy to VPS
55+
run: |
56+
ssh ${{ secrets.DO_USER }}@${{ secrets.DO_HOST }} << 'EOF'
57+
set -e
58+
59+
IMAGE="ghcr.io/${IMAGE_NAME}:latest"
60+
CONTAINER_NAME="fops"
61+
62+
echo "Logging into GHCR..."
63+
echo "${{ secrets.GHCR_PAT }}" | docker login ghcr.io \
64+
-u "${{ github.actor }}" \
65+
--password-stdin
66+
67+
echo "Pulling new image..."
68+
docker pull $IMAGE
69+
70+
echo "Stopping old container if exists..."
71+
if docker ps -q --filter "name=$CONTAINER_NAME" | grep -q .; then
72+
docker stop $CONTAINER_NAME
73+
docker rm $CONTAINER_NAME
74+
fi
75+
76+
echo "Starting new container..."
77+
docker run -d \
78+
--name $CONTAINER_NAME \
79+
--restart unless-stopped \
80+
-p 80:80 \
81+
$IMAGE
82+
EOF

0 commit comments

Comments
 (0)