feat: add auto-deploy to staging after successful build
This commit is contained in:
@@ -283,5 +283,167 @@ jobs:
|
||||
echo " Tag: $IMAGE_TAG"
|
||||
echo " URL: $IMAGE_URL"
|
||||
echo ""
|
||||
echo "💡 Image is ready for deployment!"
|
||||
echo " Run the 'Deploy to Production' workflow to deploy this image."
|
||||
|
||||
REF_NAME="${{ github.ref_name }}"
|
||||
if [ -z "$REF_NAME" ]; then
|
||||
REF_NAME=$(cd /workspace/repo && git rev-parse --abbrev-ref HEAD)
|
||||
fi
|
||||
|
||||
if [ "$REF_NAME" = "staging" ]; then
|
||||
echo "🚀 Staging branch detected - will auto-deploy after build"
|
||||
else
|
||||
echo "💡 Image is ready for deployment!"
|
||||
echo " Run the 'Deploy to Production' or 'Deploy to Staging' workflow to deploy this image."
|
||||
fi
|
||||
|
||||
# Job 3: Auto-deploy to Staging (only for staging branch)
|
||||
deploy-staging:
|
||||
name: Auto-deploy to Staging
|
||||
needs: build
|
||||
runs-on: ubuntu-latest
|
||||
if: github.ref_name == 'staging' || github.head_ref == 'staging' || (github.ref_name == '' && contains(github.ref, 'staging'))
|
||||
environment:
|
||||
name: staging
|
||||
url: https://staging.michaelschiemer.de
|
||||
env:
|
||||
DEPLOYMENT_HOST: 94.16.110.151
|
||||
steps:
|
||||
- name: Determine branch name
|
||||
id: branch
|
||||
shell: bash
|
||||
run: |
|
||||
REF_NAME="${{ github.ref_name }}"
|
||||
if [ -z "$REF_NAME" ]; then
|
||||
REF_NAME=$(echo "${{ github.ref }}" | sed 's/refs\/heads\///')
|
||||
fi
|
||||
if [ -z "$REF_NAME" ]; then
|
||||
REF_NAME="staging"
|
||||
fi
|
||||
echo "BRANCH=$REF_NAME" >> $GITHUB_OUTPUT
|
||||
echo "📋 Branch: $REF_NAME"
|
||||
|
||||
- name: Checkout deployment scripts
|
||||
run: |
|
||||
REF_NAME="${{ steps.branch.outputs.BRANCH }}"
|
||||
REPO="${{ github.repository }}"
|
||||
|
||||
if [ -n "${{ secrets.CI_TOKEN }}" ]; then
|
||||
git clone --depth 1 --branch "$REF_NAME" \
|
||||
"https://${{ secrets.CI_TOKEN }}@git.michaelschiemer.de/${REPO}.git" \
|
||||
/workspace/repo
|
||||
else
|
||||
git clone --depth 1 --branch "$REF_NAME" \
|
||||
"https://git.michaelschiemer.de/${REPO}.git" \
|
||||
/workspace/repo || \
|
||||
git clone --depth 1 \
|
||||
"https://git.michaelschiemer.de/${REPO}.git" \
|
||||
/workspace/repo
|
||||
fi
|
||||
|
||||
cd /workspace/repo
|
||||
|
||||
- name: Setup SSH key
|
||||
run: |
|
||||
mkdir -p ~/.ssh
|
||||
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/production
|
||||
chmod 600 ~/.ssh/production
|
||||
ssh-keyscan -H ${{ env.DEPLOYMENT_HOST }} >> ~/.ssh/known_hosts
|
||||
|
||||
- name: Deploy to Staging Server
|
||||
run: |
|
||||
set -e
|
||||
|
||||
DEPLOYMENT_HOST="${{ env.DEPLOYMENT_HOST }}"
|
||||
REGISTRY="${{ env.REGISTRY }}"
|
||||
IMAGE_NAME="${{ env.IMAGE_NAME }}"
|
||||
IMAGE_TAG="latest"
|
||||
|
||||
FULL_IMAGE="${REGISTRY}/${IMAGE_NAME}:${IMAGE_TAG}"
|
||||
STACK_PATH="~/deployment/stacks/staging"
|
||||
|
||||
echo "🚀 Starting staging deployment..."
|
||||
echo " Image: ${FULL_IMAGE}"
|
||||
echo " Tag: ${IMAGE_TAG}"
|
||||
echo " Host: ${DEPLOYMENT_HOST}"
|
||||
echo " Stack: ${STACK_PATH}"
|
||||
|
||||
ssh -i ~/.ssh/production \
|
||||
-o StrictHostKeyChecking=no \
|
||||
-o UserKnownHostsFile=/dev/null \
|
||||
deploy@${DEPLOYMENT_HOST} <<EOF
|
||||
set -e
|
||||
|
||||
# Ensure staging stack directory exists
|
||||
mkdir -p ${STACK_PATH}
|
||||
cd ${STACK_PATH}
|
||||
|
||||
echo "🔐 Logging in to Docker registry..."
|
||||
echo "${{ secrets.REGISTRY_PASSWORD }}" | docker login ${REGISTRY} \
|
||||
-u "${{ secrets.REGISTRY_USER }}" \
|
||||
--password-stdin || echo "⚠️ Registry login failed, continuing..."
|
||||
|
||||
echo "📥 Pulling image ${FULL_IMAGE}..."
|
||||
docker pull ${FULL_IMAGE} || {
|
||||
echo "❌ Failed to pull image ${FULL_IMAGE}"
|
||||
exit 1
|
||||
}
|
||||
|
||||
# If docker-compose.yml doesn't exist, it will be created from repo
|
||||
if [ ! -f docker-compose.yml ]; then
|
||||
echo "⚠️ docker-compose.yml not found, copying from repo..."
|
||||
cp /workspace/repo/deployment/stacks/staging/docker-compose.yml . || {
|
||||
echo "❌ Failed to copy docker-compose.yml"
|
||||
exit 1
|
||||
}
|
||||
fi
|
||||
|
||||
# Update docker-compose.yml with new image tag
|
||||
echo "📝 Updating docker-compose.yml..."
|
||||
sed -i "s|image:.*/${IMAGE_NAME}:.*|image: ${FULL_IMAGE}|g" docker-compose.yml
|
||||
|
||||
echo "✅ Updated docker-compose.yml:"
|
||||
grep "image:" docker-compose.yml | head -5
|
||||
|
||||
# Ensure networks exist
|
||||
echo "🔗 Ensuring Docker networks exist..."
|
||||
docker network create traefik-public 2>/dev/null || true
|
||||
docker network create staging-internal 2>/dev/null || true
|
||||
|
||||
echo "🔄 Starting/updating services..."
|
||||
docker compose up -d --pull always --force-recreate || {
|
||||
echo "❌ Failed to start services"
|
||||
exit 1
|
||||
}
|
||||
|
||||
echo "⏳ Waiting for services to start..."
|
||||
sleep 10
|
||||
|
||||
echo "📊 Container status:"
|
||||
docker compose ps
|
||||
|
||||
echo "✅ Staging deployment completed!"
|
||||
EOF
|
||||
|
||||
- name: Wait for deployment to stabilize
|
||||
run: sleep 30
|
||||
|
||||
- name: Health check
|
||||
id: health
|
||||
run: |
|
||||
for i in {1..10}; do
|
||||
if curl -f -k https://staging.michaelschiemer.de/health; then
|
||||
echo "✅ Health check passed"
|
||||
exit 0
|
||||
fi
|
||||
echo "⏳ Waiting for staging service... (attempt $i/10)"
|
||||
sleep 10
|
||||
done
|
||||
echo "❌ Health check failed"
|
||||
exit 1
|
||||
|
||||
- name: Notify deployment success
|
||||
if: success()
|
||||
run: |
|
||||
echo "🚀 Staging deployment successful!"
|
||||
echo "URL: https://staging.michaelschiemer.de"
|
||||
echo "Image: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest"
|
||||
|
||||
Reference in New Issue
Block a user