Refactor some lines

This commit is contained in:
Murtadha 2024-07-27 15:31:33 -04:00
parent 88b5743bb7
commit e0679b4582

View file

@ -1,120 +1,119 @@
pipeline: pipeline:
build: build:
image: node:22 image: node:22
commands: commands:
- npm ci - npm ci
- npm run build - npm run build
- echo "VERSION=$(cat version.txt)" > .env - echo "VERSION=$(cat version.txt)" > .env
clear-from-host: clear-from-host:
image: appleboy/drone-ssh image: appleboy/drone-ssh
settings: settings:
host: host:
from_secret: ssh_host from_secret: ssh_host
username: username:
from_secret: ssh_username from_secret: ssh_username
key: key:
from_secret: ssh_key from_secret: ssh_key
port: 2332 port: 2332
script: script:
- rm -rf /home/mnisyif/docker-containers/mnisyif/frontend/* - rm -rf /home/mnisyif/docker-containers/mnisyif/frontend/*
- echo "Target directory cleared" - echo "Target directory cleared"
copy-to-host: copy-to-host:
image: appleboy/drone-scp image: appleboy/drone-scp
settings: settings:
host: host:
from_secret: ssh_host from_secret: ssh_host
username: username:
from_secret: ssh_username from_secret: ssh_username
key: key:
from_secret: ssh_key from_secret: ssh_key
port: 2332 port: 2332
target: /home/mnisyif/docker-containers/mnisyif/frontend target: /home/mnisyif/docker-containers/mnisyif/frontend
source: source:
- dist/ - dist/
- nginx.conf - nginx.conf
- version.txt - version.txt
webserver-deploy: webserver-deploy:
image: appleboy/drone-ssh image: appleboy/drone-ssh
settings: settings:
host: host:
from_secret: ssh_host from_secret: ssh_host
username: username:
from_secret: ssh_username from_secret: ssh_username
key: key:
from_secret: ssh_key from_secret: ssh_key
port: 2332 port: 2332
script: script:
- cd /home/mnisyif/docker-containers/mnisyif/frontend - cd /home/mnisyif/docker-containers/mnisyif/frontend
- VERSION=$(cat version.txt) - VERSION=$(cat version.txt)
- echo "Nginx configuration:" - echo "Nginx configuration:"
- cat nginx.conf - cat nginx.conf
- echo "Contents of dist directory:" - echo "Contents of dist directory:"
- ls -la dist - ls -la dist
# Stop and remove the existing container if it exists # Stop and remove the existing container if it exists
- docker stop frontend || true - docker stop frontend || true
- docker rm frontend || true - docker rm frontend || true
# Run the new container with the current version, mounting the files # Run the new container with the current version, mounting the files
- > - >
docker run -d --name frontend -p 5173:80 docker run -d --name frontend -p 5173:80
-v /home/mnisyif/docker-containers/mnisyif/frontend/dist:/usr/share/nginx/html:ro -v /home/mnisyif/docker-containers/mnisyif/frontend/dist:/usr/share/nginx/html:ro
-v /home/mnisyif/docker-containers/mnisyif/frontend/nginx.conf:/etc/nginx/nginx.conf:ro -v /home/mnisyif/docker-containers/mnisyif/frontend/nginx.conf:/etc/nginx/nginx.conf:ro
nginx:alpine nginx:alpine
# Tag the running container with the version # Tag the running container with the version
- docker tag nginx:alpine frontend:$VERSION - docker tag nginx:alpine frontend:$VERSION
- echo "Deployment completed" - echo "Deployment completed"
confirm-deployment: confirm-deployment:
image: appleboy/drone-ssh image: appleboy/drone-ssh
settings: settings:
host: host:
from_secret: ssh_host from_secret: ssh_host
username: username:
from_secret: ssh_username from_secret: ssh_username
key: key:
from_secret: ssh_key from_secret: ssh_key
port: 2332 port: 2332
script: script:
- echo "Verifying deployment..." - echo "Verifying deployment..."
# Verify the container is running # Verify the container is running
- docker ps | grep frontend || { echo "Container failed to start"; exit 1; } - docker ps | grep frontend || { echo "Container failed to start"; exit 1; }
# Display container logs # Display container logs and test nginx configuration
- docker logs frontend - docker logs frontend
# Test Nginx configuration - docker exec frontend nginx -t
- docker exec frontend nginx -t - docker exec frontend ps aux | grep nginx
# Check Nginx process
- docker exec frontend ps aux | grep nginx
# Check contents of /usr/share/nginx/html in the container
- docker exec frontend ls -la /usr/share/nginx/html
# Perform a simple HTTP request to check if the server is responding
- curl -I http://localhost:5173 || { echo "HTTP request failed"; exit 1; }
- echo "Deployment confirmed successfully"
cleanup: # Check contents of /usr/share/nginx/html in the container
image: appleboy/drone-ssh - docker exec frontend ls -la /usr/share/nginx/html
settings: # Perform a simple HTTP request to check if the server is responding
host: - curl -I http://localhost:5173 || { echo "HTTP request failed"; exit 1; }
from_secret: ssh_host - echo "Deployment confirmed successfully"
username:
from_secret: ssh_username cleanup:
key: image: appleboy/drone-ssh
from_secret: ssh_key settings:
port: 2332 host:
script: from_secret: ssh_host
- echo "Performing cleanup..." username:
# Clean up unused containers, images, and volumes from_secret: ssh_username
- docker system prune -f --volumes key:
# Remove older versions of the Docker image from_secret: ssh_key
- > port: 2332
for img in $(docker images frontend --format "{{.Tag}}" | grep -v $(cat /home/mnisyif/docker-containers/mnisyif/frontend/version.txt)); do script:
docker rmi frontend:$img || true; - echo "Performing cleanup..."
done # Clean up unused containers, images, and volumes
- echo "Cleanup completed" - docker system prune -f --volumes
# Remove older versions of the Docker image
- >
for img in $(docker images frontend --format "{{.Tag}}" | grep -v $(cat /home/mnisyif/docker-containers/mnisyif/frontend/version.txt)); do
docker rmi frontend:$img || true;
done
- echo "Cleanup completed"
trigger: trigger:
event: event:
- push - push
branch: branch:
- master - master