Repository navigation
Dev to Lab #5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Identity Lab Docker Image | |
| on: | |
| pull_request: | |
| paths: | |
| - "apps/identity-lab/**" | |
| - "src/**" | |
| - "samples/**" | |
| - ".docker/Dockerfile.identitylab" | |
| - ".github/workflows/identitylab-image.yml" | |
| push: | |
| branches: | |
| - lab | |
| paths: | |
| - "apps/identity-lab/**" | |
| - "src/**" | |
| - "samples/**" | |
| - ".docker/Dockerfile.identitylab" | |
| - ".github/workflows/identitylab-image.yml" | |
| workflow_dispatch: | |
| permissions: | |
| contents: read | |
| packages: write | |
| concurrency: | |
| group: identitylab-deploy | |
| cancel-in-progress: false | |
| jobs: | |
| build: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Log in to GHCR | |
| if: github.event_name != 'pull_request' | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ghcr.io | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Build and push Identity Lab | |
| uses: docker/build-push-action@v6 | |
| with: | |
| context: . | |
| file: .docker/Dockerfile.identitylab | |
| push: ${{ github.event_name != 'pull_request' }} | |
| tags: ghcr.io/codebeamorg/ultimateauth-identitylab:${{ github.sha }} | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max | |
| deploy: | |
| name: Deploy Identity Lab | |
| needs: build | |
| if: github.event_name != 'pull_request' && github.ref == 'refs/heads/lab' | |
| runs-on: ubuntu-latest | |
| environment: identitylab | |
| steps: | |
| - name: Deploy to production server | |
| uses: appleboy/ssh-action@v1.0.3 | |
| env: | |
| IMAGE_TAG: ghcr.io/codebeamorg/ultimateauth-identitylab:${{ github.sha }} | |
| with: | |
| host: ${{ secrets.SERVER_HOST }} | |
| username: ${{ secrets.SERVER_USER }} | |
| key: ${{ secrets.SSH_PRIVATE_KEY }} | |
| envs: IMAGE_TAG | |
| command_timeout: 10m | |
| script: | | |
| set -eu | |
| cd /opt/identitylab | |
| ENV_FILE=/opt/identitylab/.env.identitylab | |
| COMPOSE_FILE=/opt/identitylab/docker-compose.yml | |
| sudo -n test -f "$ENV_FILE" | |
| sudo -n test -f "$COMPOSE_FILE" | |
| # Preserve current deployment configuration. | |
| sudo -n cp "$ENV_FILE" "$ENV_FILE.rollback" | |
| rollback() { | |
| echo "Deployment failed. Restoring previous image." | |
| sudo -n cp "$ENV_FILE.rollback" "$ENV_FILE" | |
| sudo -n docker compose \ | |
| --env-file "$ENV_FILE" \ | |
| -f "$COMPOSE_FILE" \ | |
| up -d --remove-orphans | |
| } | |
| trap rollback EXIT | |
| # Update only the image reference. | |
| sudo -n sed -i \ | |
| "s|^IDENTITYLAB_IMAGE=.*|IDENTITYLAB_IMAGE=$IMAGE_TAG|" \ | |
| "$ENV_FILE" | |
| sudo -n docker compose \ | |
| --env-file "$ENV_FILE" \ | |
| -f "$COMPOSE_FILE" \ | |
| pull identitylab | |
| sudo -n docker compose \ | |
| --env-file "$ENV_FILE" \ | |
| -f "$COMPOSE_FILE" \ | |
| up -d --remove-orphans | |
| # Wait for the application to respond. | |
| READY=0 | |
| for attempt in $(seq 1 30); do | |
| if curl -fsSL --max-time 10 \ | |
| https://lab.ultimateauth.com/login \ | |
| -o /dev/null; then | |
| READY=1 | |
| break | |
| fi | |
| sleep 5 | |
| done | |
| if [ "$READY" -ne 1 ]; then | |
| echo "Identity Lab readiness check failed." | |
| exit 1 | |
| fi | |
| trap - EXIT | |
| sudo -n rm -f "$ENV_FILE.rollback" | |
| echo "Identity Lab deployed: $IMAGE_TAG" |