Create GitHub Actions workflow for .NET CI/CD#728
Conversation
# 🚀 GitHub Actions Workflow for .NET: `dotnet.yml`
Berikut adalah file workflow GitHub Actions untuk proyek .NET yang siap digunakan. Simpan file ini sebagai `.github/workflows/dotnet.yml` di repository Anda.
## 📁 Lokasi File
```
.github/workflows/dotnet.yml
```
## 📄 Isi File `dotnet.yml`
```yaml
name: .NET CI
# Kapan workflow ini dijalankan
on:
push:
branches: [ main, develop ] # Jalankan pada push ke branch main/develop
pull_request:
branches: [ main ] # Jalankan pada PR ke branch main
jobs:
build:
runs-on: ubuntu-latest # Bisa juga windows-latest / macos-latest
steps:
# 1. Checkout kode dari repository
- name: Checkout repository
uses: actions/checkout@v4
# 2. Setup .NET SDK
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x # Sesuaikan dengan versi .NET project Anda
# 3. Restore dependencies (NuGet packages)
- name: Restore dependencies
run: dotnet restore
# 4. Build project
- name: Build
run: dotnet build --configuration Release --no-restore
# 5. Jalankan unit tests
- name: Test
run: dotnet test --configuration Release --no-build --verbosity normal
```
## 🔧 Fitur Tambahan (Opsional)
### **Caching NuGet Packages** (percepat build)
Tambahkan langkah ini setelah `actions/checkout`:
```yaml
- name: Cache NuGet packages
uses: actions/cache@v3
with:
path: ~/.nuget/packages
key: ${{ runner.os }}-nuget-${{ hashFiles('**/*.csproj', '**/*.sln') }}
restore-keys: |
${{ runner.os }}-nuget-
```
### **Matrix Build** (uji dengan beberapa versi .NET)
Ubah bagian `jobs` menjadi:
```yaml
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
dotnet-version: [ '6.0.x', '7.0.x', '8.0.x' ] # Versi yang akan diuji
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup .NET ${{ matrix.dotnet-version }}
uses: actions/setup-dotnet@v4
with:
dotnet-version: ${{ matrix.dotnet-version }}
# ... langkah lainnya (restore, build, test) ...
```
### **Publish dan Upload Artifact** (hanya pada push ke main)
Tambahkan langkah ini setelah `Test`:
```yaml
- name: Publish (hanya jika push ke main)
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
run: dotnet publish --configuration Release --output ./publish
- name: Upload artifact
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
uses: actions/upload-artifact@v4
with:
name: my-app-${{ matrix.dotnet-version || '8.0.x' }} # Nama file artifact
path: ./publish
```
## ✅ Workflow Lengkap dengan Semua Fitur
```yaml
name: .NET CI/CD
on:
push:
branches: [ main, develop ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
dotnet-version: [ '6.0.x', '7.0.x', '8.0.x' ]
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Cache NuGet packages
uses: actions/cache@v3
with:
path: ~/.nuget/packages
key: ${{ runner.os }}-nuget-${{ hashFiles('**/*.csproj', '**/*.sln') }}
restore-keys: |
${{ runner.os }}-nuget-
- name: Setup .NET ${{ matrix.dotnet-version }}
uses: actions/setup-dotnet@v4
with:
dotnet-version: ${{ matrix.dotnet-version }}
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --configuration Release --no-restore
- name: Test
run: dotnet test --configuration Release --no-build --verbosity normal
- name: Publish (hanya jika push ke main)
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
run: dotnet publish --configuration Release --output ./publish
- name: Upload artifact
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
uses: actions/upload-artifact@v4
with:
name: my-app-${{ matrix.dotnet-version }}
path: ./publish
```
## 📝 Catatan Penting
- Pastikan file workflow disimpan dengan ekstensi `.yml` (bukan `.yaml` – keduanya valid, tapi konsisten).
- Ganti `dotnet-version` sesuai dengan target framework project Anda (misal: `8.0.x`, `7.0.x`, `6.0.x`).
- Jika menggunakan Windows runner (`windows-latest`), perhatikan path cache dan command line.
- Untuk project yang menggunakan file `.sln` yang tidak berada di root, sesuaikan perintah `dotnet restore` dan `dotnet build` dengan path yang tepat (misal: `dotnet restore src/MySolution.sln`).
Setelah menambahkan file ini, commit dan push ke GitHub. Workflow akan otomatis berjalan. Cek tab **Actions** di repository Anda untuk melihat hasilnya.
Selamat mencoba! 🎉
Create dotnet.yml
|
@Sazwan96 please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement ( “Agreement” ) is agreed to by the party signing below ( “You” ), 1. Definitions. “Code” means the computer software code, whether in human-readable or machine-executable form, “Project” means any of the projects owned or managed by .NET Foundation and offered under a license “Submit” is the act of uploading, submitting, transmitting, or distributing code or other content to any “Submission” means the Code and any other copyrightable material Submitted by You, including any 2. Your Submission. You must agree to the terms of this Agreement before making a Submission to any 3. Originality of Work. You represent that each of Your Submissions is entirely Your 4. Your Employer. References to “employer” in this Agreement include Your employer or anyone else 5. Licenses. a. Copyright License. You grant .NET Foundation, and those who receive the Submission directly b. Patent License. You grant .NET Foundation, and those who receive the Submission directly or c. Other Rights Reserved. Each party reserves all rights not expressly granted in this Agreement. 6. Representations and Warranties. You represent that You are legally entitled to grant the above 7. Notice to .NET Foundation. You agree to notify .NET Foundation in writing of any facts or 8. Information about Submissions. You agree that contributions to Projects and information about 9. Governing Law/Jurisdiction. This Agreement is governed by the laws of the State of Washington, and 10. Entire Agreement/Assignment. This Agreement is the entire agreement between the parties, and .NET Foundation dedicates this Contribution License Agreement to the public domain according to the Creative Commons CC0 1. |
Sempurna! Berikut adalah template GitHub Actions workflow lengkap untuk .NET CI/CD:
📋 Workflow .NET CI/CD Lengkap
Simpan file ini sebagai
.github/workflows/dotnet-ci.yml:📝 Penjelasan Setiap Bahagian
1. Triggers (on:)
push- Berjalan apabila push ke branch main/developpull_request- Berjalan apabila ada PRworkflow_dispatch- Boleh dijalankan secara manual2. Jobs (6 Jobs):
3. Environment Variables
🔧 Customization Tips
Untuk ASP.NET Core:
Untuk Entity Framework:
Untuk multiple .NET versions:
🔐 Secrets yang Perlu Diset
Di Settings → Secrets and Variables → Actions, tambah:
NUGET_API_KEY- Jika publish ke NuGetDOCKER_USERNAME- Jika push Docker imageDOCKER_PASSWORD- Jika push Docker image📦 File Dockerfile (Opsional)
Jika anda ingin Docker support, buat
Dockerfile:Adakah anda ingin saya:
Beritahu saya! 😊