11name : Build and Push Docker Image
2- run-name : ${{ github.actor }} is building a Docker image
2+ run-name : ${{ github.actor }} is freaking out over TodoAPI
33
44on :
55 push :
66 paths :
7- - ' TodoAPI/**'
7+ - ' TodoAPI*/**'
8+ - ' .github/workflows/*.yml'
9+ pull_request :
10+ paths :
11+ - ' TodoAPI*/**'
812 - ' .github/workflows/*.yml'
913 workflow_dispatch :
1014
11-
12- env :
13- REGISTRY : ghcr.io
14- IMAGE_NAME : ${{ github.repository }}
15- SHORT_SHA : " $(echo ${{ github.sha }} | cut -c1-7)"
16- PATH_TO_PROJECT : ./TodoAPI
17- DOCKERFILE : ./TodoAPI/Dockerfile
18-
19-
2015jobs :
21- Build-Docker-Image :
16+ Test :
2217 runs-on : ubuntu-24.04
2318 steps :
2419 - name : Checkout repository
2520 uses : actions/checkout@v4
26-
27- - name : Set up Docker Buildx
28- uses : docker/setup-buildx-action@v3
29- with :
30- install : true
31- driver : docker-container
32- buildkitd-flags : --debug
33-
34- - name : Docker cache
35- id : docker-cache
36- env :
37- cache-name : cache-docker-image
38- uses : actions/cache@v4
39- with :
40- path : |
41- ./docker-cache
42- key : ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/*.csproj', '**/*.sln') }}
43- restore-keys : |
44- ${{ runner.os }}-build-${{ env.cache-name }}-
45- ${{ runner.os }}-build-
46- ${{ runner.os }}-
47-
48- - name : Build Docker image with cache
49- run : |
50- docker buildx build --cache-from type=local,src=./docker-cache \
51- --cache-to type=local,dest=./docker-cache \
52- -t $REGISTRY/$IMAGE_NAME:latest \
53- -f $DOCKERFILE --load $PATH_TO_PROJECT/.
54- docker image save $REGISTRY/$IMAGE_NAME:latest > ./$IMAGE_NAME.tar
55-
56- - name : Upload Docker image as artifact
57- uses : actions/upload-artifact@v4
58- with :
59- name : docker-image
60- path : $IMAGE_NAME.tar
61- retention-days : 1
62-
63- Push-Docker-Image :
64- runs-on : ubuntu-24.04
65- needs : Build-Docker-Image
66- steps :
67- - name : Download artifact
68- uses : actions/download-artifact@v4
69- with :
70- name : docker-image
71- - name : Load and tag Docker image
72- run : |
73- docker image load < $IMAGE_NAME.tar
74- docker tag $REGISTRY/$IMAGE_NAME:latest $REGISTRY/$IMAGE_NAME:${{ env.SHORT_SHA }}
75- - name : Login to GitHub Container Registry
76- uses : docker/login-action@v2
21+ - name : Set up .NET
22+ uses : actions/setup-dotnet@v4
7723 with :
78- registry : ${{ env.REGISTRY }}
79- username : ${{ github.actor }}
80- password : ${{ secrets.TOKEN }}
81- - name : Push Docker image
82- run : |
83- docker push $REGISTRY/$IMAGE_NAME:${{ env.SHORT_SHA }}
84- docker push $REGISTRY/$IMAGE_NAME:latest
24+ dotnet-version : ' 9.0.x '
25+ - name : Restore dependencies
26+ run : dotnet restore
27+ - name : Build project
28+ run : dotnet build --no-restore
29+ - name : Run tests
30+ run : dotnet test --no-build
0 commit comments