Backend Build & Push #1
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: Backend Build & Push | |
| # Migrated from GitLab CI - supports manual dispatch | |
| "on": | |
| workflow_dispatch: | |
| workflow_call: | |
| permissions: | |
| contents: write | |
| issues: write | |
| pull-requests: write | |
| packages: write | |
| jobs: | |
| # Build and push backend container - migrated from build-container-backend | |
| build-and-push-backend: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write | |
| issues: write | |
| pull-requests: write | |
| packages: write | |
| steps: | |
| - uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| token: ${{ secrets.GITHUB_TOKEN }} | |
| - uses: actions/setup-python@v5 | |
| with: | |
| python-version: '3.12' | |
| - name: Install Poetry | |
| run: pip install --root-user-action=ignore poetry --quiet | |
| - name: Install Python dependencies | |
| working-directory: backend | |
| run: poetry install --quiet | |
| - name: Setup Node.js for semantic-release | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: '20' | |
| cache: 'npm' | |
| cache-dependency-path: backend/package-lock.json | |
| - name: Install Node.js dependencies for semantic-release | |
| run: | | |
| cd backend | |
| npm ci | |
| - name: Set initial version (pre-build) | |
| id: pre-version | |
| run: | | |
| echo "SHORT_SHA=${GITHUB_SHA:0:8}" >> $GITHUB_ENV | |
| # Set image name for backend | |
| BACKEND_IMAGE="ghcr.io/${{ github.repository }}/backend" | |
| echo "BACKEND_IMAGE=${BACKEND_IMAGE}" >> $GITHUB_ENV | |
| # Set initial version for build | |
| echo "VERSION=${{ github.sha }}" >> $GITHUB_ENV | |
| - uses: docker/setup-buildx-action@v3 | |
| - name: Login to GitHub Container Registry | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ghcr.io | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Build and push backend container | |
| uses: docker/build-push-action@v6 | |
| with: | |
| context: backend/ | |
| file: backend/Dockerfile | |
| build-args: | | |
| VERSION=${{ env.VERSION }} | |
| push: >- | |
| ${{ github.event_name == 'push' && | |
| github.ref == 'refs/heads/main' }} | |
| tags: | | |
| ${{ env.BACKEND_IMAGE }}:${{ env.SHORT_SHA }} | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max | |
| # Only run semantic release after successful build on main branch | |
| - name: Run semantic-release | |
| if: github.ref == 'refs/heads/main' && github.event_name == 'push' | |
| id: semantic | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| run: | | |
| cd backend | |
| npm run semantic-release | |
| NEW_VERSION=$(node -p "require('./package.json').version") | |
| BACKEND_VERSION="backend-v$NEW_VERSION" | |
| # Tag the existing image with the backend-prefixed semantic version | |
| docker pull ${{ env.BACKEND_IMAGE }}:${{ env.SHORT_SHA }} | |
| docker tag ${{ env.BACKEND_IMAGE }}:${{ env.SHORT_SHA }} ${{ env.BACKEND_IMAGE }}:$BACKEND_VERSION | |
| docker push ${{ env.BACKEND_IMAGE }}:$BACKEND_VERSION | |
| echo "Tagged and pushed image with backend version: $BACKEND_VERSION" |