1+ name : Docker CI
2+
3+ on :
4+ pull_request :
5+ branches :
6+ - main
7+
8+ push :
9+ branches :
10+ - main
11+
12+ env :
13+ REGISTRY : ghcr.io
14+ IMAGE_NAME : ${{ github.repository }}
15+
16+ jobs :
17+
18+ test :
19+ name : Build and Smoke Test
20+ runs-on : ubuntu-latest
21+
22+ permissions :
23+ contents : read
24+
25+ steps :
26+
27+ - name : Checkout repository
28+ uses : actions/checkout@v7
29+
30+ - name : Validate Docker Compose
31+ run : docker compose config
32+
33+ - name : Build containers
34+ run : docker compose build
35+
36+ - name : Start containers
37+ run : docker compose up -d
38+
39+ - name : Wait for API
40+ run : |
41+ for i in {1..30}; do
42+ if curl --fail http://localhost:3000/ > /dev/null 2>&1; then
43+ echo "API is ready"
44+ exit 0
45+ fi
46+
47+ echo "Waiting for API..."
48+ sleep 2
49+ done
50+
51+ echo "API did not become ready"
52+ docker compose logs
53+ exit 1
54+
55+ - name : GET tasks
56+ run : |
57+ curl --fail http://localhost:3000/tasks
58+
59+ - name : POST task
60+ run : |
61+ response=$(curl --fail \
62+ -X POST \
63+ -H "Content-Type: application/json" \
64+ -d '{"title":"GitHub Actions Smoke Test"}' \
65+ http://localhost:3000/tasks)
66+
67+ echo "$response"
68+
69+ task_id=$(echo "$response" | jq -r '.id')
70+
71+ if [ -z "$task_id" ] || [ "$task_id" = "null" ]; then
72+ echo "Task was not created correctly"
73+ exit 1
74+ fi
75+
76+ echo "TASK_ID=$task_id" >> "$GITHUB_ENV"
77+
78+ - name : PUT task
79+ run : |
80+ curl --fail \
81+ -X PUT \
82+ -H "Content-Type: application/json" \
83+ -d '{"completed":true}' \
84+ http://localhost:3000/tasks/$TASK_ID
85+
86+ - name : GET created task
87+ run : |
88+ curl --fail \
89+ http://localhost:3000/tasks/$TASK_ID
90+
91+ - name : DELETE task
92+ run : |
93+ curl --fail \
94+ -X DELETE \
95+ http://localhost:3000/tasks/$TASK_ID
96+
97+ - name : Show container status
98+ if : always()
99+ run : docker compose ps
100+
101+ - name : Show logs on failure
102+ if : failure()
103+ run : docker compose logs
104+
105+ - name : Stop containers
106+ if : always()
107+ run : docker compose down -v
108+
109+
110+ publish :
111+ name : Publish Docker Image
112+ runs-on : ubuntu-latest
113+
114+ needs :
115+ - test
116+
117+ if : github.event_name == 'push' && github.ref == 'refs/heads/main'
118+
119+ permissions :
120+ contents : read
121+ packages : write
122+
123+ steps :
124+
125+ - name : Checkout repository
126+ uses : actions/checkout@v7
127+
128+ - name : Set up Docker Buildx
129+ uses : docker/setup-buildx-action@v4
130+
131+ - name : Login to GitHub Container Registry
132+ uses : docker/login-action@v4
133+ with :
134+ registry : ${{ env.REGISTRY }}
135+ username : ${{ github.actor }}
136+ password : ${{ secrets.GITHUB_TOKEN }}
137+
138+ - name : Docker metadata
139+ id : meta
140+ uses : docker/metadata-action@v6
141+ with :
142+ images : ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
143+ tags : |
144+ type=raw,value=latest
145+ type=sha
146+
147+ - name : Build and publish image
148+ uses : docker/build-push-action@v7
149+ with :
150+ context : .
151+ push : true
152+ tags : ${{ steps.meta.outputs.tags }}
153+ labels : ${{ steps.meta.outputs.labels }}
0 commit comments