another fix
Some checks failed
Debug Secrets / check-secrets (push) Successful in 1s
Deploy to Production / build (push) Failing after 3s
Deploy to Production / deploy (push) Has been skipped
Simple Build Test / test-docker (push) Successful in 1s
Test Build-Deploy Pattern / build (push) Successful in 1s
Test SHA Variable / check-sha (push) Successful in 1s
Test Runner / test (push) Successful in 1s
Test Build-Deploy Pattern / deploy (push) Successful in 1s
Some checks failed
Debug Secrets / check-secrets (push) Successful in 1s
Deploy to Production / build (push) Failing after 3s
Deploy to Production / deploy (push) Has been skipped
Simple Build Test / test-docker (push) Successful in 1s
Test Build-Deploy Pattern / build (push) Successful in 1s
Test SHA Variable / check-sha (push) Successful in 1s
Test Runner / test (push) Successful in 1s
Test Build-Deploy Pattern / deploy (push) Successful in 1s
This commit is contained in:
parent
e0803a3007
commit
7bc02dd257
1 changed files with 15 additions and 11 deletions
|
|
@ -46,8 +46,11 @@ jobs:
|
|||
uses: actions/checkout@v4
|
||||
|
||||
- name: Blue-Green Deploy to server
|
||||
env:
|
||||
NEW_IMAGE: ${{ needs.build.outputs.image_tag }}
|
||||
REGISTRY: ${{ secrets.FORGEJO_REGISTRY }}
|
||||
REGISTRY_USER: ${{ secrets.FORGEJO_USER }}
|
||||
run: |
|
||||
|
||||
# Setup SSH key
|
||||
mkdir -p ~/.ssh
|
||||
echo "${{ secrets.DEPLOY_SSH_KEY }}" > ~/.ssh/deploy_key
|
||||
|
|
@ -61,27 +64,28 @@ jobs:
|
|||
docker-compose.yml ${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_IP }}:/opt/czsk/
|
||||
|
||||
# SSH into server and perform blue-green deployment
|
||||
ssh -i ~/.ssh/deploy_key -o StrictHostKeyChecking=no ${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_IP }} << 'EOF'
|
||||
ssh -i ~/.ssh/deploy_key -o StrictHostKeyChecking=no ${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_IP }} bash -s "$NEW_IMAGE" "$REGISTRY" "$REGISTRY_USER" << 'DEPLOY_SCRIPT'
|
||||
set -e
|
||||
NEW_IMAGE="$1"
|
||||
REGISTRY="$2"
|
||||
REGISTRY_USER="$3"
|
||||
|
||||
cd /opt/czsk
|
||||
|
||||
# Export new image tag
|
||||
export NEW_IMAGE="${{ needs.build.outputs.image_tag }}"
|
||||
export FALLBACK_IMAGE="${{ secrets.FORGEJO_REGISTRY }}/${{ secrets.FORGEJO_USER }}/czsk:latest"
|
||||
|
||||
# Save current running image as backup
|
||||
CURRENT_IMAGE=$(docker inspect czsk --format='{{ "{{" }}.Config.Image{{ "}}" }}' 2>/dev/null || echo "none")
|
||||
CURRENT_IMAGE=$(docker inspect czsk --format='{{.Config.Image}}' 2>/dev/null || echo "none")
|
||||
echo "Current image: $CURRENT_IMAGE"
|
||||
echo "New image: $NEW_IMAGE"
|
||||
|
||||
# Pull new image
|
||||
echo "Pulling new image..."
|
||||
if ! docker pull $NEW_IMAGE; then
|
||||
if ! docker pull "$NEW_IMAGE"; then
|
||||
echo "Failed to pull new image, aborting deployment"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Create blue-green docker-compose with new image
|
||||
sed "s|image:.*czsk.*|image: $NEW_IMAGE|" docker-compose.yml > docker-compose.new.yml
|
||||
sed "s|image:.*|image: $NEW_IMAGE|" docker-compose.yml > docker-compose.new.yml
|
||||
|
||||
# Deploy new version (green)
|
||||
echo "Deploying new version..."
|
||||
|
|
@ -93,7 +97,7 @@ jobs:
|
|||
mv docker-compose.new.yml docker-compose.yml
|
||||
|
||||
# Cleanup old images (keep last 3 versions)
|
||||
docker images ${{ secrets.FORGEJO_REGISTRY }}/${{ secrets.FORGEJO_USER }}/czsk --format "{{.Tag}}" | grep -v latest | tail -n +4 | xargs -r -I {} docker rmi ${{ secrets.FORGEJO_REGISTRY }}/${{ secrets.FORGEJO_USER }}/czsk:{} || true
|
||||
docker images "${REGISTRY}/${REGISTRY_USER}/czsk" --format '{{.Tag}}' | grep -v latest | tail -n +4 | xargs -r -I {} docker rmi "${REGISTRY}/${REGISTRY_USER}/czsk:{}" || true
|
||||
else
|
||||
echo "Health check failed! Rolling back..."
|
||||
docker compose -f docker-compose.yml up -d
|
||||
|
|
@ -106,4 +110,4 @@ jobs:
|
|||
rm -f docker-compose.new.yml
|
||||
exit 1
|
||||
fi
|
||||
EOF
|
||||
DEPLOY_SCRIPT
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue