Skip to content

Commit 932591a

Browse files
authored
Merge pull request #1420 from merico-dev/add-gitlab-ci-usecase-docs
feat: add use-cases for gitops-python-flask-tools
2 parents caf3be9 + 6f86709 commit 932591a

File tree

3 files changed

+255
-3
lines changed

3 files changed

+255
-3
lines changed
Lines changed: 127 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,129 @@
11
# GitLab + GitLab CI with DTM Tools
22

3-
TODO
3+
DevStream has two abstractions: [Tools](../../core-concepts/tools.md) and [Apps](../../core-concepts/apps.md).
4+
5+
[The previous use case](./4-gitlab-dtm-apps.md) uses `Apps`. We can also achieve the same result with `Tools`, and here's how:
6+
7+
## ENV Vars
8+
9+
The following environment variables are required for this to work:
10+
11+
```bash
12+
export GITLAB_TOKEN="YOUR_GITLAB_TOKEN"
13+
export IMAGE_REPO_PASSWORD="YOUR_DOCKERHUB_TOKEN_HERE"
14+
```
15+
16+
---
17+
18+
## Config File
19+
20+
```yaml
21+
config:
22+
state:
23+
backend: local
24+
options:
25+
stateFile: devstream.state
26+
27+
vars:
28+
gitlabUser: YOUR_GITLAB_USERNAME
29+
dockerUser: YOUR_DOCKERHUB_USERNAME
30+
app: testapp
31+
32+
tools:
33+
- name: helm-installer
34+
instanceID: argocd
35+
- name: repo-scaffolding
36+
instanceID: myapp
37+
options:
38+
destinationRepo:
39+
owner: [[ gitlabUser ]]
40+
name: [[ app ]]
41+
branch: master
42+
scmType: gitlab
43+
# set env GITLAB_TOKEN
44+
token: [[ env GITLAB_TOKEN ]]
45+
sourceRepo:
46+
org: devstream-io
47+
name: dtm-repo-scaffolding-python-flask
48+
scmType: github
49+
- name: gitlab-ci
50+
instanceID: flask
51+
dependsOn: [ "repo-scaffolding.myapp" ]
52+
options:
53+
scm:
54+
owner: [[ gitlabUser ]]
55+
name: [[ app ]]
56+
branch: master
57+
scmType: gitlab
58+
token: [[ env GITLAB_TOKEN ]]
59+
pipeline:
60+
language:
61+
framework: flask
62+
name: python
63+
imageRepo:
64+
user: [[ dockerUser ]]
65+
# set env IMAGE_REPO_PASSWORD
66+
password: [[ env IMAGE_REPO_PASSWORD ]]
67+
- name: argocdapp
68+
instanceID: default
69+
dependsOn: [ "helm-installer.argocd", "gitlab-ci.flask" ]
70+
options:
71+
app:
72+
name: [[ app ]]
73+
namespace: argocd
74+
destination:
75+
server: https://kubernetes.default.svc
76+
namespace: default
77+
source:
78+
valuefile: values.yaml
79+
path: helm/[[ app ]]
80+
repoURL: ${{repo-scaffolding.myapp.outputs.repoURL}}
81+
token: [[ env GITLAB_TOKEN ]]
82+
imageRepo:
83+
user: [[ dockerUser ]]
84+
password: [[ env IMAGE_REPO_PASSWORD ]]
85+
```
86+
87+
Update the "YOUR_GITLAB_USERNAME" and "YOUR_DOCKERHUB_USERNAME" in the above file accordingly.
88+
89+
**Notes:**
90+
91+
Your `GitLab` must have shared runners to run `gitlab-ci`, If you want to create a runner automatically, you can refer to [gitlab-ci plugin docs](../../plugins/gitlab-ci.md) about how to generate a runner by `DevStream`.
92+
93+
---
94+
95+
## Run
96+
97+
First, initialize:
98+
99+
```bash
100+
# this downloads the required plugins, according to the config file, automatically.
101+
dtm init -f config.yaml
102+
```
103+
104+
Then we apply it by running:
105+
106+
```bash
107+
dtm apply -f config.yaml -y
108+
```
109+
110+
Now we can see the repo has been created in `GitLab` and the image has been uploaded to `Dockerhub`.
111+
112+
<figure markdown>
113+
![GitLab CI ](./gitlab-tools/gitlab-tools-ci-page.png){ width="1000" }
114+
<figcaption>GitLab CI</figcaption>
115+
</figure>
116+
117+
In your `Kubernetes` cluster, the app pod is created in the default namespace.
118+
119+
```bash
120+
$ kubectl get application -n argocd
121+
NAME SYNC STATUS HEALTH STATUS
122+
testapp Synced Healthy
123+
$ kubectl get deploy -n default
124+
NAME READY UP-TO-DATE AVAILABLE AGE
125+
testapp 1/1 1 1 4m27s
126+
$ kubectl get pods -n default
127+
NAME READY STATUS RESTARTS AGE
128+
testapp-5f9c75b4f6-57d9p 1/1 Running 0 3m48s
129+
```
Lines changed: 128 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,129 @@
1-
# GitLab + GitLab CI with DTM Tools
1+
# 用 DTM Tools 实现基于 Gitlab,Argo CD 和 Gitlab CI 的 CICD 流程
22

3-
TODO
3+
DevStream 抽象了2个概念:[Tools](../../core-concepts/tools.md)[Apps](../../core-concepts/apps.md)
4+
5+
[前一个用户场景](./4-gitlab-dtm-apps.zh.md) 里介绍了 "Apps",你可以用 "Tools" 实现一样的效果。具体方法如下:
6+
7+
## 配置环境变量
8+
9+
你需要配置以下两个环境变量:
10+
11+
```bash
12+
export GITLAB_TOKEN="YOUR_GITLAB_TOKEN"
13+
export IMAGE_REPO_PASSWORD="YOUR_DOCKERHUB_TOKEN_HERE"
14+
```
15+
16+
---
17+
18+
## 配置文件
19+
20+
```yaml
21+
config:
22+
state:
23+
backend: local
24+
options:
25+
stateFile: devstream.state
26+
27+
vars:
28+
gitlabUser: YOUR_GITLAB_USERNAME
29+
dockerUser: YOUR_DOCKERHUB_USERNAME
30+
app: testapp
31+
32+
tools:
33+
- name: helm-installer
34+
instanceID: argocd
35+
- name: repo-scaffolding
36+
instanceID: myapp
37+
options:
38+
destinationRepo:
39+
owner: [[ gitlabUser ]]
40+
name: [[ app ]]
41+
branch: master
42+
scmType: gitlab
43+
# set env GITLAB_TOKEN
44+
token: [[ env GITLAB_TOKEN ]]
45+
sourceRepo:
46+
org: devstream-io
47+
name: dtm-repo-scaffolding-python-flask
48+
scmType: github
49+
- name: gitlab-ci
50+
instanceID: flask
51+
dependsOn: [ "repo-scaffolding.myapp" ]
52+
options:
53+
scm:
54+
owner: [[ gitlabUser ]]
55+
name: [[ app ]]
56+
branch: master
57+
scmType: gitlab
58+
token: [[ env GITLAB_TOKEN ]]
59+
pipeline:
60+
language:
61+
framework: flask
62+
name: python
63+
imageRepo:
64+
user: [[ dockerUser ]]
65+
# set env IMAGE_REPO_PASSWORD
66+
password: [[ env IMAGE_REPO_PASSWORD ]]
67+
- name: argocdapp
68+
instanceID: default
69+
dependsOn: [ "helm-installer.argocd", "gitlab-ci.flask" ]
70+
options:
71+
app:
72+
name: [[ app ]]
73+
namespace: argocd
74+
destination:
75+
server: https://kubernetes.default.svc
76+
namespace: default
77+
source:
78+
valuefile: values.yaml
79+
path: helm/[[ app ]]
80+
repoURL: ${{repo-scaffolding.myapp.outputs.repoURL}}
81+
token: [[ env GITLAB_TOKEN ]]
82+
imageRepo:
83+
user: [[ dockerUser ]]
84+
password: [[ env IMAGE_REPO_PASSWORD ]]
85+
```
86+
87+
需要相应更新上述配置文件里的 "YOUR_GITLAB_USERNAME" 和 "YOUR_DOCKERHUB_USERNAME"。
88+
89+
**注意:**
90+
91+
上述配置运行的前提是你的 `GitLab` 已经配置好了共享 runner 来执行 `ci` 操作,如果你想要自动创建一个 runner, 你可以参考 [gitlab插件文档](../../plugins/github-actions.zh.md) 来配置由 `DevStream` 来创建一个项目 runner。
92+
93+
---
94+
95+
## 执行
96+
97+
首先需要基于配置文件来初始化插件:
98+
99+
```bash
100+
# this downloads the required plugins, according to the config file, automatically.
101+
dtm init -f config.yaml
102+
```
103+
104+
然后运行如下命令让配置生效:
105+
106+
```bash
107+
dtm apply -f config.yaml -y
108+
```
109+
110+
现在我们就可以看到 `GitLab` 中已经创建了对应的仓库并且 `CI` 把生成的项目镜像推送到了 `Dockerhub` 仓库。
111+
112+
<figure markdown>
113+
![GitLab CI ](./gitlab-tools/gitlab-tools-ci-page.png){ width="1000" }
114+
<figcaption>GitLab CI</figcaption>
115+
</figure>
116+
117+
在你的 `Kubernetes` 集群中已经创建了对应的应用。
118+
119+
```bash
120+
$ kubectl get application -n argocd
121+
NAME SYNC STATUS HEALTH STATUS
122+
testapp Synced Healthy
123+
$ kubectl get deploy -n default
124+
NAME READY UP-TO-DATE AVAILABLE AGE
125+
testapp 1/1 1 1 4m27s
126+
$ kubectl get pods -n default
127+
NAME READY STATUS RESTARTS AGE
128+
testapp-5f9c75b4f6-57d9p 1/1 Running 0 3m48s
129+
```
Loading

0 commit comments

Comments
 (0)