Fix failing CI/CD #58
Workflow file for this run
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: Deploy Website | |
| on: | |
| push: | |
| tags: | |
| - '*.*.*' # Triggers on version tags like 1.0.0, 2.1.3, etc. | |
| workflow_dispatch: | |
| inputs: | |
| version: | |
| description: 'Version to deploy (e.g., 1.0.0)' | |
| required: true | |
| default: '1.0.0' | |
| env: | |
| DOCKER_REGISTRY: ghcr.io | |
| IMAGE_NAME: safeturned/website | |
| jobs: | |
| deploy: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| packages: write | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v5 | |
| with: | |
| token: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Determine version | |
| id: version | |
| run: | | |
| if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then | |
| VERSION="${{ github.event.inputs.version }}" | |
| else | |
| VERSION=${GITHUB_REF#refs/tags/} | |
| fi | |
| if [[ ! $VERSION =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then | |
| echo "❌ Invalid SemVer format: $VERSION" | |
| exit 1 | |
| fi | |
| MAJOR=$(echo $VERSION | cut -d. -f1) | |
| MINOR=$(echo $VERSION | cut -d. -f2) | |
| PATCH=$(echo $VERSION | cut -d. -f3) | |
| echo "version=$VERSION" >> $GITHUB_OUTPUT | |
| echo "major=$MAJOR" >> $GITHUB_OUTPUT | |
| echo "minor=$MINOR" >> $GITHUB_OUTPUT | |
| echo "patch=$PATCH" >> $GITHUB_OUTPUT | |
| echo "full_version=$VERSION" >> $GITHUB_OUTPUT | |
| - name: Determine branch and environment | |
| id: branch | |
| run: | | |
| if [ "${{ github.ref }}" = "refs/heads/master" ] || [ "${{ github.ref }}" = "refs/heads/master" ]; then | |
| echo "branch=master" >> $GITHUB_OUTPUT | |
| echo "environment=production" >> $GITHUB_OUTPUT | |
| elif [ "${{ github.ref }}" = "refs/heads/dev" ]; then | |
| echo "branch=dev" >> $GITHUB_OUTPUT | |
| echo "environment=development" >> $GITHUB_OUTPUT | |
| else | |
| echo "branch=master" >> $GITHUB_OUTPUT | |
| echo "environment=production" >> $GITHUB_OUTPUT | |
| fi | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: '20' | |
| cache: 'npm' | |
| cache-dependency-path: src/package-lock.json | |
| - name: Setup Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Login to GHCR | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ghcr.io | |
| username: ${{ github.repository_owner }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Build and push Docker image | |
| run: | | |
| cd src | |
| VERSION="${{ steps.version.outputs.version }}" | |
| BRANCH="${{ steps.branch.outputs.branch }}" | |
| docker build --no-cache -t "ghcr.io/safeturned/website:v${VERSION}" . | |
| docker tag "ghcr.io/safeturned/website:v${VERSION}" "ghcr.io/safeturned/website:${BRANCH}" | |
| docker push "ghcr.io/safeturned/website:v${VERSION}" | |
| docker push "ghcr.io/safeturned/website:${BRANCH}" | |
| - name: Create deployment package | |
| run: | | |
| VERSION="${{ steps.version.outputs.version }}" | |
| BRANCH="${{ steps.branch.outputs.branch }}" | |
| ENVIRONMENT="${{ steps.branch.outputs.environment }}" | |
| # Copy docker-compose.yaml and replace version placeholder | |
| sed "s/VERSION_PLACEHOLDER/v${VERSION}/g" ./src/docker-compose.yaml > ./docker-compose.yaml | |
| cat > ./docker-compose.override.yaml << EOF | |
| services: | |
| website: | |
| image: ghcr.io/safeturned/website:v${VERSION} | |
| container_name: ${{ steps.branch.outputs.environment == 'production' && 'safeturned-website' || 'dev-safeturned-website' }} | |
| environment: | |
| - NODE_ENV=${ENVIRONMENT} | |
| - APP_VERSION=${VERSION} | |
| - APP_BRANCH=${BRANCH} | |
| - IS_PRODUCTION=${{ steps.branch.outputs.environment == 'production' && 'true' || 'false' }} | |
| env_file: | |
| - .env | |
| networks: | |
| - ${{ secrets.DOCKER_NETWORK_NAME }} | |
| labels: | |
| - "version=${VERSION}" | |
| - "branch=${BRANCH}" | |
| - "environment=${ENVIRONMENT}" | |
| - "major=${{ steps.version.outputs.major }}" | |
| - "minor=${{ steps.version.outputs.minor }}" | |
| - "patch=${{ steps.version.outputs.patch }}" | |
| networks: | |
| ${{ secrets.DOCKER_NETWORK_NAME }}: | |
| external: true | |
| EOF | |
| cat > ./deploy.sh << 'EOF' | |
| #!/bin/bash | |
| set -e | |
| VERSION="$1" | |
| BRANCH="$2" | |
| ENVIRONMENT="$3" | |
| if [ -z "$VERSION" ] || [ -z "$BRANCH" ] || [ -z "$ENVIRONMENT" ]; then | |
| echo "Usage: $0 <version> <branch> <environment>" | |
| exit 1 | |
| fi | |
| echo "Deploying version: $VERSION, branch: $BRANCH, environment: $ENVIRONMENT" | |
| docker compose -f docker-compose.yaml -f docker-compose.override.yaml down || true | |
| docker pull ghcr.io/safeturned/website:v${VERSION} | |
| docker compose -f docker-compose.yaml -f docker-compose.override.yaml up -d | |
| sleep 10 | |
| EOF | |
| chmod +x ./deploy.sh | |
| - name: Copy files to server | |
| uses: appleboy/scp-action@v1 | |
| with: | |
| host: ${{ secrets.SERVER_HOST }} | |
| port: ${{ secrets.SSH_PORT }} | |
| username: ${{ secrets.SERVER_USER }} | |
| key: ${{ secrets.SSH_PRIVATE_KEY }} | |
| source: "./docker-compose.yaml,./docker-compose.override.yaml,./deploy.sh" | |
| target: ${{ steps.branch.outputs.branch == 'dev' && '/opt/dev-safeturned-website/deploy' || '/opt/safeturned-website/deploy' }} | |
| - name: Deploy to server | |
| uses: appleboy/ssh-action@v1 | |
| with: | |
| host: ${{ secrets.SERVER_HOST }} | |
| port: ${{ secrets.SSH_PORT }} | |
| username: ${{ secrets.SERVER_USER }} | |
| key: ${{ secrets.SSH_PRIVATE_KEY }} | |
| script: | | |
| echo "=== Starting website deployment ===" | |
| cd ${{ steps.branch.outputs.branch == 'dev' && '/opt/dev-safeturned-website/deploy' || '/opt/safeturned-website/deploy' }} | |
| chmod +x deploy.sh | |
| ./deploy.sh "${{ steps.version.outputs.version }}" "${{ steps.branch.outputs.branch }}" "${{ steps.branch.outputs.environment }}" | |
| echo "=== Website deployment completed ===" |