Skip to content

trial

trial #202

Workflow file for this run

# This workflow will build a .NET project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net
name: .NET Ci with tests
on:
push:
branches: ["main", "dev"]
pull_request:
branches: ["main", "dev"]
jobs:
build-and-test:
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v4
- name: Set up Docker
uses: docker/setup-buildx-action@v3
- name: Install Docker Compose
run: |
sudo apt-get update
sudo apt-get install -y docker-compose
- name: Cache Docker layers
uses: actions/cache@v4
with:
path: /tmp/.docker
key: ${{ runner.os }}-docker-${{ github.sha }}
restore-keys: |
${{ runner.os }}-docker-
- name: Start services
run: docker-compose up -d
- name: Cache .NET packages
uses: actions/cache@v4
with:
path: ~/.nuget/packages
key: nuget-${{ runner.os }}-${{ hashFiles('**/*.csproj') }}
restore-keys: |
nuget-${{ runner.os }}-
- name: Install .NET Core SDK
uses: actions/setup-dotnet@v4
with:
dotnet-version: "8.0.x"
- name: Restore NuGet packages
run: dotnet restore
- name: Build
run: dotnet build -c Release --no-restore
- name: Test backend
run: dotnet test -c Release --no-build
- name: Test frontend
run: |
cd Frontend
npm install
npm test
deploy:
runs-on: ubuntu-latest
needs: build-and-test
if: github.ref == 'refs/heads/main'
steps:
- name: Check out code
uses: actions/checkout@v4
- name: Set up Docker
uses: docker/setup-buildx-action@v3
- name: Install Docker Compose
run: |
sudo apt-get update
sudo apt-get install -y docker-compose
- name: Cache Docker layers
uses: actions/cache@v4
with:
path: /tmp/.docker
key: ${{ runner.os }}-docker-${{ github.sha }}
restore-keys: |
${{ runner.os }}-docker-
- name: Log in to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.HUB_USER }}
password: ${{ secrets.HUB_PASSWORD }}
- name: Build and Tag Docker Images
run: |
docker-compose -f docker-compose.yml build
docker images
docker tag devops-fullstack_backend:latest ${{ secrets.HUB_USER }}/backend:latest
docker tag devops-fullstack_frontend:latest ${{ secrets.HUB_USER }}/frontend:latest
docker images
- name: Push Docker Images
run: |
docker push ${{ secrets.HUB_USER }}/backend:latest
docker push ${{ secrets.HUB_USER }}/frontend:latest
docker-compose -f docker-compose.yml push
- name: Set up SSH
uses: webfactory/ssh-agent@v0.5.1
with:
ssh-private-key: ${{ secrets.DEPLOY_KEY }}
- name: Deploy to Server
run: |
ssh -o StrictHostKeyChecking=no ${{secrets.SERVER_USER}}@${{ secrets.SERVER_HOST }} << 'EOF'
cd /home/azureuser/DevOps-fullstack
docker-compose -f docker-compose.prod.yml pull
docker-compose -f docker-compose.prod.yml up -d
EOF
# chmod +x deploy.sh
# ./deploy.sh