Refactor GitHub CI/CD workflow to enable .NET installation directory … #34
Workflow file for this run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | ||
name: Clean Architecture the template CI/CD | ||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
branches: | ||
- main | ||
env: | ||
Check failure on line 11 in .github/workflows/github-cicd.yml
|
||
DOTNET_INSTALL_DIR: "./.dotnet" | ||
jobs: | ||
build-and-deploy: | ||
name: Build and Deploy to GHCR | ||
runs-on: self-hosted | ||
permissions: | ||
contents: read | ||
packages: write | ||
attestations: write | ||
id-token: write | ||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
- name: Setup .NET | ||
uses: actions/setup-dotnet@v4 | ||
with: | ||
dotnet-version: "8.0.408" | ||
- name: Cache NuGet packages | ||
id: cache-nuget | ||
uses: actions/cache@v4 | ||
with: | ||
path: ~/.nuget/packages | ||
key: ${{ runner.os }}-nuget-${{ hashFiles('src/**/packages.lock.json','tests/**/packages.lock.json') }} | ||
restore-keys: | | ||
${{ runner.os }}-nuget- | ||
- name: Install dependencies | ||
run: dotnet restore --locked-mode | ||
- name: Build | ||
run: dotnet build --configuration Release --no-restore | ||
- name: Test | ||
run: dotnet dotnet test --no-restore --verbosity normal -e ASPNETCORE_ENVIRONMENT=Deployment | ||
- name: Publish Application | ||
run: dotnet publish -c Release -o app/publish | ||
- uses: actions/upload-artifact@v4 | ||
with: | ||
name: dotnet-app | ||
path: | | ||
app/publish | ||
app/output/test-results | ||
retention-days: 3 | ||
- name: Log in to GitHub Container Registry | ||
uses: docker/login-action@v3 | ||
with: | ||
registry: ${{ vars.REGISTRY }} | ||
username: ${{ github.actor }} | ||
password: ${{ secrets.PWD_TOKEN }} | ||
- name: Build and tag Docker image | ||
run: > | ||
docker build . -t ${{ vars.REGISTRY }}/${{ github.actor }}/${{ | ||
vars.IMAGE_NAME }}:${{ github.sha }} | ||
docker tag ${{ vars.REGISTRY }}/${{ github.actor }}/${{ vars.IMAGE_NAME }}:${{ github.sha }} ${{ vars.REGISTRY }}/${{ github.actor }}/${{ vars.IMAGE_NAME }}:latest | ||
- name: Push Docker image to GHCR | ||
run: > | ||
docker push ${{ vars.REGISTRY }}/${{ github.actor }}/${{ vars.IMAGE_NAME | ||
}}:${{ github.sha }} | ||
docker push ${{ vars.REGISTRY }}/${{ github.actor }}/${{ vars.IMAGE_NAME }}:latest | ||
- name: Deploy | ||
uses: appleboy/ssh-action@v1.2.0 | ||
with: | ||
host: ${{ vars.SSH_HOST }} | ||
username: ${{ secrets.SSH_USER }} | ||
key: ${{ secrets.SERVER_SSH_KEY }} | ||
port: 22 | ||
script: > | ||
docker login ${{ vars.REGISTRY }} -u ${{ github.actor }} -p ${{ | ||
secrets.MY_PERSONAL_PAT }} | ||
docker pull ${{ vars.REGISTRY }}/${{ github.actor }}/${{ vars.IMAGE_NAME }}:latest | ||
cd ${{ secrets.APP_PATH }} | ||
docker compose down | ||
docker compose up -d --build |