-
Notifications
You must be signed in to change notification settings - Fork 2
150 lines (126 loc) · 4.83 KB
/
dotnet.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# 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: Create .env file
run: |
echo VITE_API_URL=${{ secrets.VITE_API_URL }} >> .env
echo CONNECTION_STRING=${{ secrets.CONNECTION_STRING }} >> .env
echo POSTGRES_DB=${{ secrets.POSTGRES_DB }} >> .env
echo POSTGRES_USER=${{ secrets.POSTGRES_USER }} >> .env
echo POSTGRES_PASSWORD=${{ secrets.POSTGRES_PASSWORD }} >> .env
- name: Start services
run: |
export $(grep -v '^#' .env | xargs)
docker-compose --env-file .env -f docker-compose.yml up --build -d
- name: Cache Docker layers
uses: actions/cache@v4
with:
path: /tmp/.docker
key: ${{ runner.os }}-docker-${{ github.sha }}
restore-keys: |
${{ runner.os }}-docker-
- 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: 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: |
echo VITE_API_URL=${{ secrets.VITE_API_URL }} >> .env
echo CONNECTION_STRING=${{ secrets.CONNECTION_STRING }} >> .env
echo POSTGRES_DB=${{ secrets.POSTGRES_DB }} >> .env
echo POSTGRES_USER=${{ secrets.POSTGRES_USER }} >> .env
echo POSTGRES_PASSWORD=${{ secrets.POSTGRES_PASSWORD }} >> .env
export $(grep -v '^#' .env | xargs)
docker-compose --env-file .env -f docker-compose.yml up --build -d
docker tag devops-fullstack_backend:latest ${{ secrets.HUB_USER }}/backend:latest
docker tag devops-fullstack_frontend:latest ${{ secrets.HUB_USER }}/frontend:latest
- 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
export $(grep -v '^#' .env.prod | xargs)
docker-compose --env-file .env.prod -f docker-compose.prod.yml up -d
EOF
create-issue-on-failure:
runs-on: ubuntu-latest
needs: build-and-test
if: failure()
steps:
- name: Create issue
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const labels = context.job === 'build-and-test' ? ['backend'] : ['frontend'];
const issueTitle = `Workflow failed: ${context.workflow}`;
const issueBody = `The workflow **${context.workflow}** failed for job **${context.job}**. Please check the logs for more details.`;
const { data: issue } = await github.issues.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: issueTitle,
body: issueBody,
labels: labels,
});
console.log(`Created issue ${issue.number}: ${issue.title}`);