Skip to content

Commit a68f52f

Browse files
committed
Protocal as README
1 parent fa77267 commit a68f52f

File tree

7 files changed

+203
-2
lines changed

7 files changed

+203
-2
lines changed

README.md

Lines changed: 203 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,203 @@
1-
# Exercise 4
2-
[![Build and Push Docker Image](https://github.com/DavidKalteis/cicd_ex4/actions/workflows/docker.yml/badge.svg)](https://github.com/DavidKalteis/cicd_ex4/actions/workflows/docker.yml)
1+
2+
> [!NOTE]
3+
> All the files can be found in my public GitHub repository: https://github.com/DavidKalteis/cicd_ex4
4+
5+
# Part 1
6+
## Clone repositor
7+
8+
```bash
9+
git clone https://github.com/mrckurz/cd2020-ex04.git
10+
```
11+
## Check correct go installation
12+
13+
**Start main file**
14+
```bash
15+
go run main.go
16+
```
17+
18+
**Successful respond for localhost:8888**
19+
```bash
20+
curl localhost:8888
21+
22+
Hello, it is 15:21
23+
```
24+
25+
**Test passes**
26+
```bash
27+
go test -v
28+
29+
=== RUN TestGetMinuteUp
30+
--- PASS: TestGetMinuteUp (0.00s)
31+
=== RUN TestGetMinuteDown
32+
--- PASS: TestGetMinuteDown (0.00s)
33+
PASS
34+
ok github.com/DavidKalteis/cicd/ex3/cd2020-ex04 0.002s
35+
```
36+
37+
**Added label to dockerfile (MAINTAINER is depreciated and LABLE should be used instead)**
38+
```
39+
LABEL author="[email protected]"
40+
```
41+
42+
**Build the docker image**
43+
```bash
44+
docker image build -f Dockerfile -t davidkfh/my-first-image:0.0.1 ./
45+
```
46+
47+
**and view the built one (3rd line):**
48+
49+
```bash
50+
docker images
51+
52+
REPOSITORY TAG IMAGE ID CREATED SIZE
53+
cicd-jenkins latest 6b16002443dc 11 days ago 563MB
54+
postgres latest f49abb9855df 2 months ago 438MB
55+
davidkfh/my-first-image 0.0.1 56349b2c27f8 15 months ago 255MB
56+
jupyter/pyspark-notebook latest 384d65133c51 19 months ago 4.86GB
57+
confluentinc/cp-kafka 6.2.0 6b8633010002 3 years ago 772MB
58+
confluentinc/cp-zookeeper 6.2.0 3cf98185738a 3 years ago 772MB
59+
```
60+
61+
**login to docker via CLI:**
62+
63+
```bash
64+
docker login -u davidkfh
65+
66+
i Info → A Personal Access Token (PAT) can be used instead.
67+
To create a PAT, visit https://app.docker.com/settings
68+
69+
70+
Password:
71+
72+
WARNING! Your credentials are stored unencrypted in '/home/david/.docker/config.json'.
73+
Configure a credential helper to remove this warning. See
74+
https://docs.docker.com/go/credential-store/
75+
76+
Login Succeeded
77+
```
78+
79+
**Push to dockerhub:**
80+
```bash
81+
docker image push davidkfh/my-first-image:0.0.1
82+
The push refers to repository [docker.io/davidkfh/my-first-image]
83+
5f70bf18a086: Mounted from library/golang
84+
00168ea4cecf: Mounted from library/golang
85+
f89b05d5c0b7: Mounted from library/golang
86+
6dd5a23a5acc: Mounted from library/golang
87+
d4fc045c9e3a: Mounted from library/golang
88+
0.0.1: digest: sha256:48850eac9f79901ba2d536779f4d4603a8878dd17e8fa2fd7a3f4557e92e464f size: 1364
89+
```
90+
91+
**Verified on https://hub.docker.com/repositories/davidkfh**
92+
![Dockerhub](img/Dockerhub1.png)
93+
94+
# Part 2
95+
96+
**adapted Docker file:**
97+
98+
```dockerfile
99+
FROM golang:1.20-alpine
100+
LABEL author="[email protected]"
101+
WORKDIR /src
102+
COPY . .
103+
RUN ls -la
104+
RUN go build -o /usr/myapp main.go
105+
EXPOSE 8888
106+
CMD ["/usr/myapp"]
107+
```
108+
109+
**build image:**
110+
111+
```bash
112+
docker image build -t davidkfh/myhello:0.0.1 ./
113+
```
114+
115+
**run container:**
116+
```bash
117+
docker container run -p 9090:8888 davidkfh/myhello:0.0.1
118+
```
119+
120+
**Check running container:**
121+
```bash
122+
docker ps
123+
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
124+
9be1e5dd2d34 davidkfh/myhello:0.0.1 "/usr/myapp" 9 minutes ago Up 9 minutes 0.0.0.0:9090->8888/tcp, [::]:9090->8888/tcp peaceful_chebyshev
125+
```
126+
127+
**Check ports:**
128+
```bash
129+
curl localhost:8888
130+
curl: (7) Failed to connect to localhost port 8888 after 0 ms: Couldn't connect to server
131+
```
132+
133+
```bash
134+
curl localhost:9090
135+
Hello, it is 14:8%
136+
```
137+
138+
**stop the container:**
139+
```
140+
docker stop peaceful_chebyshev
141+
```
142+
# Part 3
143+
144+
**For this part, i created a new GitHub repository and moved the files from the previous parts there -> https://github.com/DavidKalteis/cicd_ex4**
145+
**Generate docker access token https://app.docker.com/settings/personal-access-tokens/create**
146+
147+
![PAT](img/DockerPAT.png)
148+
149+
> [!IMPORTANT]
150+
>The scope has to be set to Read & Write in order to push images
151+
152+
**Add token to GitHub secrets**
153+
154+
![PAT](img/GitHubSec.png)
155+
156+
**Trigger pipeline with commit:**
157+
The pipeline can be found in the public repository [here](https://github.com/DavidKalteis/cicd_ex4)
158+
159+
![Pipeline](img/Pipe.png)
160+
161+
**Veryfied on [Dockerhub](https://hub.docker.com/repository/docker/davidkfh/myhello/general)**
162+
163+
![Dockerhub](img/Dockerhub2.png)
164+
165+
# Part 4
166+
167+
**Added Trivy to GitHub pipeline:**
168+
```yml
169+
scan:
170+
runs-on: ubuntu-latest
171+
needs: build
172+
173+
steps:
174+
- name: Checkout code
175+
uses: actions/checkout@v3
176+
177+
- name: Trivy Image Scan
178+
uses: aquasecurity/trivy-action@master
179+
with:
180+
image-ref: ${{ secrets.DOCKERHUB_USERNAME }}/myhello:${{ needs.build.outputs.image-tag }}
181+
format: 'table'
182+
exit-code: '1'
183+
ignore-unfixed: true
184+
vuln-type: 'os,library'
185+
severity: 'CRITICAL,HIGH'
186+
scan-type: image
187+
188+
189+
- name: Trivy FS Scan (source & Dockerfile)
190+
uses: aquasecurity/trivy-action@master
191+
with:
192+
scan-type: 'fs'
193+
scan-ref: '.'
194+
format: 'table'
195+
exit-code: '1'
196+
ignore-unfixed: true
197+
vuln-type: 'os,library'
198+
severity: 'CRITICAL,HIGH'
199+
```
200+
201+
**The pipeline will not succeed due to found vulnerabilities in go. Details can be found in the pipeline logs of the repository. https://github.com/DavidKalteis/cicd_ex4/actions/runs/15086341171/job/42409335201**
202+
203+
![SecScan](img/SecScan.png)

img/DockerPAT.png

71.6 KB
Loading

img/Dockerhub1.png

29.4 KB
Loading

img/Dockerhub2.png

73.4 KB
Loading

img/GitHubSec.png

18.8 KB
Loading

img/Pipe.png

45 KB
Loading

img/SecScan.png

139 KB
Loading

0 commit comments

Comments
 (0)