|
| 1 | +name: staging-pipeline |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + push: |
| 6 | + branches: |
| 7 | + - develop |
| 8 | + pull_request: |
| 9 | + types: [closed] |
| 10 | + branches: |
| 11 | + - develop |
| 12 | + |
| 13 | +jobs: |
| 14 | + publish: |
| 15 | + runs-on: ubuntu-latest |
| 16 | + steps: |
| 17 | + - name: Checkout |
| 18 | + uses: actions/checkout@v4 |
| 19 | + - name: Set up QEMU |
| 20 | + uses: docker/setup-qemu-action@v3 |
| 21 | + - name: Set up Docker Buildx |
| 22 | + uses: docker/setup-buildx-action@v3 |
| 23 | + - name: Login to GitHub Container Registry |
| 24 | + uses: docker/login-action@v3 |
| 25 | + with: |
| 26 | + registry: ghcr.io |
| 27 | + username: ${{ github.actor }} |
| 28 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 29 | + - name: Build and push |
| 30 | + id: build_and_push |
| 31 | + uses: docker/build-push-action@v6 |
| 32 | + with: |
| 33 | + context: . |
| 34 | + push: true |
| 35 | + tags: | |
| 36 | + ghcr.io/generalmagicio/worldview-be:staging |
| 37 | +
|
| 38 | + deploy: |
| 39 | + needs: publish |
| 40 | + runs-on: ubuntu-latest |
| 41 | + steps: |
| 42 | + - name: SSH and Redeploy Staging |
| 43 | + uses: appleboy/ssh-action@v1.2.0 |
| 44 | + with: |
| 45 | + host: ${{ secrets.STAGING_HOST }} |
| 46 | + username: ${{ secrets.STAGING_USERNAME }} |
| 47 | + key: ${{ secrets.STAGING_PRIVATE_KEY }} |
| 48 | + port: ${{ secrets.SSH_PORT }} |
| 49 | + command_timeout: 200m |
| 50 | + script: | |
| 51 | + set -e |
| 52 | + docker system prune -f |
| 53 | + # Authenticate with GHCR |
| 54 | + echo ${{ secrets.GITHUB_TOKEN }} | docker login ghcr.io -u ${{ github.actor }} --password-stdin |
| 55 | + cd worldview-be |
| 56 | + git checkout develop |
| 57 | + git fetch origin develop |
| 58 | + git reset --hard origin/develop |
| 59 | + git pull origin develop |
| 60 | + docker compose pull |
| 61 | +
|
| 62 | + rollout-deploy-1: |
| 63 | + needs: deploy |
| 64 | + runs-on: ubuntu-latest |
| 65 | + env: |
| 66 | + CONTAINER: 'worldview-be1' |
| 67 | + steps: |
| 68 | + - name: SSH and Redeploy Staging |
| 69 | + uses: appleboy/ssh-action@v1.2.0 |
| 70 | + with: |
| 71 | + host: ${{ secrets.STAGING_HOST }} |
| 72 | + username: ${{ secrets.STAGING_USERNAME }} |
| 73 | + key: ${{ secrets.STAGING_PRIVATE_KEY }} |
| 74 | + port: ${{ secrets.SSH_PORT }} |
| 75 | + envs: CONTAINER |
| 76 | + script: | |
| 77 | + ## Update each backend service one by one |
| 78 | + ## First Deployment |
| 79 | + export CONTAINER="${CONTAINER}" |
| 80 | + cd worldview-be |
| 81 | + docker compose rm -fs $CONTAINER |
| 82 | + docker compose up --force-recreate -d $CONTAINER |
| 83 | +
|
| 84 | + # Wait for $CONTAINER to be healthy (timeout after 5 minutes) |
| 85 | + echo "Waiting for $CONTAINER to become healthy..." |
| 86 | + if ! timeout 300 bash -c 'until [ "$(docker inspect --format="{{json .State.Health.Status}}" $CONTAINER)" == "\"healthy\"" ]; do echo "Waiting for $CONTAINER to be healthy..."; sleep 5; done'; then |
| 87 | + echo "Timeout waiting for $CONTAINER to become healthy" |
| 88 | + echo "Container logs:" |
| 89 | + docker logs $CONTAINER |
| 90 | + exit 1 |
| 91 | + fi |
| 92 | + # Check if $CONTAINER is healthy |
| 93 | + if [ "$(docker inspect --format='{{json .State.Health.Status}}' $CONTAINER)" != "\"healthy\"" ]; then |
| 94 | + echo "$CONTAINER is not healthy, stopping deployment" |
| 95 | + echo "Container logs:" |
| 96 | + docker logs $CONTAINER |
| 97 | + exit 1 |
| 98 | + fi |
| 99 | + echo "First deployment phase completed successfully" |
| 100 | +
|
| 101 | + rollout-deploy-2: |
| 102 | + needs: rollout-deploy-1 |
| 103 | + runs-on: ubuntu-latest |
| 104 | + env: |
| 105 | + CONTAINER: 'worldview-be2' |
| 106 | + steps: |
| 107 | + - name: SSH and Redeploy Staging |
| 108 | + uses: appleboy/ssh-action@v1.2.0 |
| 109 | + with: |
| 110 | + host: ${{ secrets.STAGING_HOST }} |
| 111 | + username: ${{ secrets.STAGING_USERNAME }} |
| 112 | + key: ${{ secrets.STAGING_PRIVATE_KEY }} |
| 113 | + port: ${{ secrets.SSH_PORT }} |
| 114 | + envs: CONTAINER |
| 115 | + script: | |
| 116 | + ## Update each backend service one by one |
| 117 | + ## Second Deployment |
| 118 | + export CONTAINER="${CONTAINER}" |
| 119 | + cd worldview-be |
| 120 | + docker compose rm -fs $CONTAINER |
| 121 | + docker compose up --force-recreate -d $CONTAINER |
| 122 | +
|
| 123 | + # Wait for $CONTAINER to be healthy (timeout after 5 minutes) |
| 124 | + echo "Waiting for $CONTAINER to become healthy..." |
| 125 | + if ! timeout 300 bash -c 'until [ "$(docker inspect --format="{{json .State.Health.Status}}" $CONTAINER)" == "\"healthy\"" ]; do echo "Waiting for $CONTAINER to be healthy..."; sleep 5; done'; then |
| 126 | + echo "Timeout waiting for $CONTAINER to become healthy" |
| 127 | + echo "Container logs:" |
| 128 | + docker logs $CONTAINER |
| 129 | + exit 1 |
| 130 | + fi |
| 131 | + # Check if $CONTAINER is healthy |
| 132 | + if [ "$(docker inspect --format='{{json .State.Health.Status}}' $CONTAINER)" != "\"healthy\"" ]; then |
| 133 | + echo "$CONTAINER is not healthy, stopping deployment" |
| 134 | + echo "Container logs:" |
| 135 | + docker logs $CONTAINER |
| 136 | + exit 1 |
| 137 | + fi |
| 138 | + echo "Second deployment phase completed successfully" |
0 commit comments