-
Notifications
You must be signed in to change notification settings - Fork 0
139 lines (115 loc) · 3.51 KB
/
ci_cd.yml
File metadata and controls
139 lines (115 loc) · 3.51 KB
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
name: CI/CD Pipeline
on:
push:
branches: [main]
pull_request:
branches: [main]
permissions:
contents: read
packages: write
jobs:
# =========================
# 🧱 1. BUILD JOB
# =========================
build:
name: Build
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore --configuration Release
- name: Lint Code
run: dotnet format --verify-no-changes || true
- name: Static Analysis (Roslyn)
run: dotnet build --no-restore
#- name: SonarCloud Analysis
# uses: SonarSource/sonarcloud-github-action@master
# with:
# args: >
# -Dsonar.projectKey=dlopes-se_dotnet-qrshop
# -Dsonar.organization=dlopes-se
# env:
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
- name: Upload Build Artifacts
uses: actions/upload-artifact@v4
with:
name: build-artifacts
path: |
**/bin/Release/
**/obj/project.assets.json
# =========================
# 🧪 2. TEST JOB
# =========================
test:
name: Test
runs-on: ubuntu-latest
needs: build
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x
- name: Download Build Artifacts
uses: actions/download-artifact@v4
with:
name: build-artifacts
path: .
- name: Run Tests
run: dotnet test --no-build --verbosity normal
- name: Check for vulnerable dependencies
run: dotnet list package --vulnerable
- name: Upload Test Results
if: always()
uses: actions/upload-artifact@v4
with:
name: test-results
path: TestResults/
# =========================
# 🐳 3. PUBLISH JOB
# =========================
publish:
name: Build and Publish Docker Image
runs-on: ubuntu-latest
needs: test
if: github.ref == 'refs/heads/main'
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GHCR_PAT }}
- name: Build and push backend image
uses: docker/build-push-action@v5
with:
context: ./src
file: ./src/Dockerfile
push: true
tags: |
ghcr.io/dlopes-se/dotnet-qrshop:latest
ghcr.io/dlopes-se/dotnet-qrshop:${{ github.sha }}
labels: |
org.opencontainers.image.source=${{ github.repository }}
org.opencontainers.image.description=Public .NET Backend for QRShop Demo
- name: Set image visibility to public
run: |
curl -s -L \
-X PATCH \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.GHCR_PAT }}" \
https://api.github.com/user/packages/container/dotnet-qrshop/visibility \
-d '{"visibility":"public"}'
- name: Output Image Tag
run: echo "Docker image built and published ✅"