Skip to content

Build and Deploy Angular App #15

Build and Deploy Angular App

Build and Deploy Angular App #15

Workflow file for this run

name: Build and Deploy Angular App
on:
push:
branches:
- Production
workflow_dispatch:
permissions:
contents: write
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '16'
cache: 'yarn'
- name: Install Yarn
run: npm install -g yarn
- name: Install dependencies with Yarn
run: yarn install
- name: Generate build timestamp
id: timestamp
run: echo "timestamp=$(date +'%Y%m%d_%H%M%S')" >> $GITHUB_OUTPUT
- name: Create logs directory
run: mkdir -p deployment_logs
- name: Check Angular output path in angular.json
id: check_config
run: |
if [ -f "angular.json" ]; then
DEFAULT_OUTPUT_PATH=$(grep -o '"outputPath": *"[^"]*"' angular.json | head -1 | cut -d'"' -f4)
if [ -n "$DEFAULT_OUTPUT_PATH" ]; then
echo "output_path=$DEFAULT_OUTPUT_PATH" >> $GITHUB_OUTPUT
echo "Using output path from angular.json: $DEFAULT_OUTPUT_PATH"
else
echo "output_path=dist" >> $GITHUB_OUTPUT
echo "No outputPath found in angular.json, using default: dist"
fi
else
echo "output_path=dist" >> $GITHUB_OUTPUT
echo "No angular.json found, using default: dist"
fi
- name: Build Angular app
id: build
run: |
yarn build --configuration production --base-href="/${{ github.event.repository.name }}/" 2>&1 | tee deployment_logs/build_log_${{ steps.timestamp.outputs.timestamp }}.txt
continue-on-error: true
- name: Check build status
id: check_build
run: |
if [ "${{ steps.build.outcome }}" == "success" ]; then
echo "build_status=success" >> $GITHUB_OUTPUT
echo "Build completed successfully"
else
echo "build_status=failure" >> $GITHUB_OUTPUT
echo "Build failed"
exit 1
fi
- name: Ensure docs directory exists
if: steps.check_build.outputs.build_status == 'success'
run: |
if [ ! -d "docs" ]; then
if [ -d "${{ steps.check_config.outputs.output_path }}" ]; then
echo "Build directory found at ${{ steps.check_config.outputs.output_path }}, copying to docs/"
mkdir -p docs
cp -r ${{ steps.check_config.outputs.output_path }}/* docs/
else
echo "Build directory not found at ${{ steps.check_config.outputs.output_path }}, listing contents of current directory:"
ls -la
echo "Creating empty docs directory"
mkdir -p docs
fi
fi
- name: Add .nojekyll file
if: steps.check_build.outputs.build_status == 'success'
run: touch docs/.nojekyll
- name: Deploy to GitHub Pages
if: steps.check_build.outputs.build_status == 'success'
id: deploy
uses: JamesIves/github-pages-deploy-action@v4
with:
folder: docs
branch: gh-pages
clean: true
- name: Log deployment result
if: always()
run: |
echo "Deployment completed at $(date)" >> deployment_logs/deploy_summary_${{ steps.timestamp.outputs.timestamp }}.txt
echo "Commit: ${{ github.sha }}" >> deployment_logs/deploy_summary_${{ steps.timestamp.outputs.timestamp }}.txt
echo "Build status: ${{ steps.check_build.outputs.build_status }}" >> deployment_logs/deploy_summary_${{ steps.timestamp.outputs.timestamp }}.txt
if [ "${{ steps.check_build.outputs.build_status }}" == "success" ]; then
echo "Deployment status: ${{ steps.deploy.outcome }}" >> deployment_logs/deploy_summary_${{ steps.timestamp.outputs.timestamp }}.txt
fi
- name: Archive deployment logs
if: always()
uses: actions/upload-artifact@v4
with:
name: deployment-logs-${{ steps.timestamp.outputs.timestamp }}
path: deployment_logs/
retention-days: 30
- name: Send email notification on success
if: success()
uses: dawidd6/action-send-mail@v3
with:
server_address: ${{ secrets.MAIL_SERVER }}
server_port: ${{ secrets.MAIL_PORT }}
secure: true
username: ${{ secrets.MAIL_USERNAME }}
password: ${{ secrets.MAIL_PASSWORD }}
subject: ✅ Successful Build and Deploy for ${{ github.repository }}
html_body: |
Build and deployment for ${{ github.repository }} completed successfully.
<b>Details:</b>
<ul>
<li>Repository: ${{ github.repository }}</li>
<li>Commit: ${{ github.sha }}</li>
<li>Commit message: ${{ github.event.head_commit.message }}</li>
<li>Workflow: ${{ github.workflow }}</li>
<li>Timestamp: $(date)</li>
</ul>
<p>
<a href="https://${{ github.repository_owner }}.github.io/${{ github.event.repository.name }}/">View the deployment</a><br>
<a href="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}">View workflow run</a>
</p>
from: GitHub Actions <${{ secrets.MAIL_USERNAME }}>
to: ${{ secrets.MAIL_RECIPIENT }}
- name: Send email notification on failure
if: failure()
uses: dawidd6/action-send-mail@v3
with:
server_address: ${{ secrets.MAIL_SERVER }}
server_port: ${{ secrets.MAIL_PORT }}
secure: true
username: ${{ secrets.MAIL_USERNAME }}
password: ${{ secrets.MAIL_PASSWORD }}
subject: ❌ Failed Build for ${{ github.repository }}
html_body: |
Build or deployment for ${{ github.repository }} failed.
<b>Details:</b>
<ul>
<li>Repository: ${{ github.repository }}</li>
<li>Commit: ${{ github.sha }}</li>
<li>Commit message: ${{ github.event.head_commit.message }}</li>
<li>Workflow: ${{ github.workflow }}</li>
<li>Timestamp: $(date)</li>
</ul>
<p>
<a href="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}">View logs and details</a>
</p>
from: GitHub Actions <${{ secrets.MAIL_USERNAME }}>
to: ${{ secrets.MAIL_RECIPIENT }}