为 AWS Ubuntu 实例中的 NodeJS 部署设置 GitLab CI
Setting GitLab CI for NodeJS deployment in AWS Ubuntu Instance
我有一个配置了 GitLab CE 的 AWS Ubuntu 实例。现在我想配置 GitLab CI 以在每个 commit.I 之后部署我的 NodeJS 应用程序对此没有任何适当的逐步解决方案。
我的 NodeJS 应用 运行 在 /var/www/mean/my-app
上 http://myapp.mydomain.com
并且托管由 Apache Proxy 处理,
<VirtualHost *:80>
ServerAdmin anshad@mydomain.com
ServerName gitlab.mydomain.com
ServerAlias www.gitlab.mydomain.com
ServerSignature Off
ProxyPreserveHost On
AllowEncodedSlashes NoDecode
<Location />
Require all granted
ProxyPassReverse http://localhost:8080
ProxyPassReverse http://gitlab.mydomain.com/
</Location>
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_URI} ^/uploads/.*
RewriteRule .* http://127.0.0.1:8080%{REQUEST_URI} [P,QSA,NE]
DocumentRoot /home/git/gitlab/public
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b" common_forwarded
ErrorLog /var/log/apache2/gitlab_error.log
CustomLog /var/log/apache2/gitlab_forwarded.log common_forwarded
CustomLog /var/log/apache2/gitlab_access.log combined env=!dontlog
CustomLog /var/log/apache2/gitlab.log combined
</VirtualHost>
并且该应用程序使用 forever 模块进行引导
forever start app.js
git实验室配置检查 sudo -u git -H bundle exec rake gitlab:check RAILS_ENV=production
给出,
Checking GitLab Shell ...
GitLab Shell version >= 4.0.0 ? ... OK (4.0.0)
Repo base directory exists?
default... yes
Repo storage directories are symlinks?
default... no
Repo paths owned by git:git?
default... yes
Repo paths access is drwxrws---?
default... yes
hooks directories in repos are links: ...
dev / my-app ... ok
Running /home/git/gitlab-shell/bin/check
Check GitLab API access: OK
Access to /home/git/.ssh/authorized_keys: OK
Send ping to redis server: OK
gitlab-shell self-check successful
Checking GitLab Shell ... Finished
Checking Sidekiq ...
Running? ... yes
Number of Sidekiq processes ... 1
Checking Sidekiq ... Finished
Checking Reply by email ...
Reply by email is disabled in config/gitlab.yml
Checking Reply by email ... Finished
Checking LDAP ...
LDAP is disabled in config/gitlab.yml
Checking LDAP ... Finished
Checking GitLab ...
Git configured with autocrlf=input? ... yes
Database config exists? ... yes
All migrations up? ... yes
Database contains orphaned GroupMembers? ... no
GitLab config exists? ... yes
GitLab config outdated? ... no
Log directory writable? ... yes
Tmp directory writable? ... yes
Uploads directory setup correctly? ... yes
Init script exists? ... yes
Init script up-to-date? ... yes
projects have namespace: ...
dev / my-app ... yes
Redis version >= 2.8.0? ... yes
Ruby version >= 2.1.0 ? ... yes (2.3.1)
Your git bin path is "/usr/bin/git"
Git version >= 2.7.3 ? ... yes (2.7.4)
Active users: 1
Checking GitLab ... Finished
我曾经使用 SSH 从我的系统登录实例,
ssh -i API-Key.pem ubuntu@ec2-XX-XX-XXX-XXX.ap-south-1.compute.amazonaws.com
使用命令创建密钥
ssh-keygen -t rsa
/etc/gitlab-runner/config.toml
上的运行器配置
concurrent = 1
check_interval = 0
[[runners]]
name = "Production Runner"
url = "http://gitlab.mydomain.com/ci"
token = "xxxxxxxxxxxxxxxxxxxxxxxxxxx"
executor = "ssh"
[runners.ssh]
user = "ubuntu"
host = "ip-XXX-XX-XX-XXX"
identity_file = "/home/ubuntu/.ssh/id_rsa"
[runners.cache]
.gitlab-ci.yml
上的代码
test_async:
script:
- npm install
因为我配置不好,运行报错,
Running with gitlab-ci-multi-runner 1.7.1 (f896af7)
Using SSH executor...
ERROR: Preparation failed: ssh: handshake failed: ssh: unable to authenticate, attempted methods [none publickey], no supported methods remain
Will be retried in 3s ...
我的困惑是:
要将提交的代码部署到应用程序位置,.gitlab-ci.yml
文件的内容应该是什么?
以及如何为此配置运行器?如果我必须使用ssh
转轮,那那边的配置应该是什么?
更新:
将 .pem
文件作为 identity_file
提供后,出现以下错误
Running with gitlab-ci-multi-runner 1.7.1 (f896af7)
Using SSH executor...
Running on ip-xxx-xx-xx-xxx via ip-xxx-xx-xx-xxx...
Cloning repository...
Cloning into 'builds/a92f1b91/0/dev/my-app'...
fatal: unable to access 'http://gitlab-ci-token:xxxxxxxxxxxxxxxxxxxx@gitlab.mydomain.com/dev/my-app.git/': The requested URL returned error: 500
ERROR: Build failed: Process exited with: 1. Reason was: ()
现在有一个问题,git 使用 http 克隆不起作用,但 ssh 克隆有效。
注意:git实验室和构建环境都是同一个主机(同一个aws实例)
Bug 也在 GitLab 中报告(http 克隆问题)。
在你的 /etc/gitlab-runner/config.toml
concurrent = 1
check_interval = 0
[[runners]]
name = "Production Runner"
url = "http://gitlab.mydomain.com/ci"
token = "xxxxxxxxxxxxxxxxxxxxxxxxxxx"
executor = "ssh"
[runners.ssh]
user = "ubuntu"
host = "ip-XXX-XX-XX-XXX"
identity_file = "/home/ubuntu/.ssh/id_rsa"
[runners.cache]
你来定义
- 主机
- 用户
- 和身份文件
host 应该是您的构建主机 IP(换句话说,您将在其中执行构建)
user 应该是您在 Build host 上的用户。 不在 git实验室主机上。
您可以测试无密码 ssh 的工作方式
- 以 root 身份登录 gitlab 主机
- ssh -i /home/ubuntu/.ssh/id_rsa ubuntu@ip-XXX-XX-XX-XXX
如果可行并且不要求您输入密码 - 一切都很好。
如果出现问题 - 表示您没有正确设置无密码身份验证。
设置密码的最简单方法 public 基于密钥的身份验证是使用名为
的命令
ssh-copy-id
例如,我想在我的 gitlab 和我的构建主机之间设置无密码的 ssh 身份验证。
我的构建主机 ip 是 192.168.0.42,主机名是 build.home
我已经在 gitlab 主机上的 /home/ubuntu/.ssh 下生成了 id_rsa 和 id_rsa.pub。
现在让我们将 public 密钥从 gitlab 主机推送到我们的构建主机。第一次它会要求您输入密码。
[root@gitlab ~]# ssh-copy-id -i /home/ubuntu/.ssh/id_rsa.pub ubuntu@build.home
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
ubuntu@build.home's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'ubuntu@build.home'"
and check to make sure that only the key(s) you wanted were added.
请注意,在上面的示例中,我将 public 密钥推送到远程主机。
但是当我现在连接到此远程主机时 - 我将指定我的私钥。
[root@gitlab ~]# ssh -i /home/ubuntu/.ssh/id_rsa ubuntu@build.home
[ubuntu@build ~]$ hostname
build.home
尝试在 git 实验室主机和远程主机之间测试您的 public 密钥验证并更新您的问题。
资源:
https://gitlab.com/gitlab-org/gitlab-ci-multi-runner/blob/master/docs/executors/ssh.md
P.S: 我会post我今天晚些时候回家配置我的工作环境。
编辑 1:
这是我的配置。
我的 git 实验室主机名为 gitlab.home 192.168.0.41
我还有另一个名为 sshbuild.home 192.168.0.43
的虚拟机
下面是我添加ssh的方法运行ner
第 1 步。在我的 gitlab.home 上安装
yum install gitlab-ci-multi-runner
并将我的远程 sshbuild.home VM 注册为 ssh 运行ner
我还需要确保无密码身份验证在我的 gitlab.home 和 sshbuild.home 之间有效,所以
[root@gitlab gitlab-runner]# ssh-copy-id 192.168.0.43
The authenticity of host '192.168.0.43 (192.168.0.43)' can't be established.
ECDSA key fingerprint is b4:6a:1b:72:d1:7d:1f:34:f7:bb:ef:ad:69:42:11:13.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.0.43's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '192.168.0.43'"
and check to make sure that only the key(s) you wanted were added.
[root@gitlab gitlab-runner]# ssh 192.168.0.43
Last login: Fri Nov 18 17:05:06 2016 from 192.168.0.101
[root@sshbuild ~]# exit
然后我禁用了我的其他 运行ner (shell 运行ner) 并制作了新的 SSH 运行ner 项目特定只是为了确保当我提交 -它将在 ssh 运行ner
上执行
我确实提交了,瞧 - 我们在 sshbuild.home 主机
上进行了 运行 的成功测试
这里有几个链接可能有助于更好地理解这个主题
- https://about.gitlab.com/2016/07/29/the-basics-of-gitlab-ci/
- https://docs.gitlab.com/ce/ci/runners/README.html
- http://docs.gitlab.com/runner/commands/README.html
- https://docs.gitlab.com/ce/ci/yaml/README.html
P.S:这是我的 /etc/gitlab-runner/config.toml 文件
[root@gitlab gitlab-runner]# cat /etc/gitlab-runner/config.toml
concurrent = 1
check_interval = 0
[[runners]]
name = "sshbuild"
url = "http://gitlab.home/"
token = "2bc1825d8fbde09fd632637c60e9e7"
executor = "ssh"
[runners.ssh]
user = "root"
host = "192.168.0.43"
port = "22"
identity_file = "/root/.ssh/id_rsa"
[runners.cache]
P.S:如果我在 Web 界面的“设置”下为我的存储库禁用 HTTP,我会遇到与您类似的错误。但是错误不是 500,而是 403。
编辑 2:
现在我将介绍 .gitlab-ci.yml 基于一个简单的 HelloWorld 项目
在我的 HelloWorld 中,我有一个名为 server.js 的文件,当从节点 运行 时 - 将简单地在端口 3000 上创建 Web 服务器 运行ning 并在 GET 请求上回复“Hello World”。
1 const http = require('http');
2
3 const hostname = '0.0.0.0';
4 const port = 3000;
5
6 const server = http.createServer((req, res) => {
7 res.statusCode = 200;
8 res.setHeader('Content-Type', 'text/plain');
9 res.end('Hello World!\n');
10 });
11
12 server.listen(port, hostname, () => {
13 console.log(`Server running at http://${hostname}:${port}/`);
14 });
我的目标是能够运行 一个针对它的测试用例。在这种情况下,我将 运行 简单
curl localhost:3000 | grep "Hello World"
但我需要将其放入一个单独的脚本中,成功时退出状态为 0,失败时退出状态为非零
cat -n simpletest.sh
1 #!/bin/bash
2
3 cleanup ()
4 {
5 count=`netstat -anp|grep ":3000"|grep LISTEN|awk '{print $NF}'|cut -d\/ -f1|wc -l`
6 if [ $count -ne 0 ]
7 then
8 pid=`netstat -anp|grep ":3000"|grep LISTEN|awk '{print $NF}'|cut -d\/ -f1`;
9 echo "Need to kill PID $pid";
10 kill $pid
11 fi
12 }
13
14 echo "Running simple test"
15 curl localhost:3000|grep "Hello World"
16 if [ $? -eq 0 ]
17 then
18 echo "Test was successfull"
19 echo "Clean up node.js process"
20 cleanup
21 exit 0
22 else
23 echo "Test failed"
24 echo "Clean up node.js process"
25 cleanup
26 exit 1
27 fi
现在让我们来介绍我的 .gitlab-ci.yml
cat -n .gitlab-ci.yml
1 test:
2
3 before_script:
4 - echo "Before script"
5 - hostname
6 - /bin/bash cleanup.sh
7
8 script:
9 - echo "Main Script"
10 - node server.js &
11 - sleep 3
12 - /bin/bash simpletest.sh
我有一个名为 test 的作业。
在 before_script 中,它 运行s cleanup.sh 脚本可以简单地杀死监听端口 3000 的 PID,以防发现。
cat -n cleanup.sh
1 #!/bin/bash
2 count=`netstat -anp|grep ":3000"|grep LISTEN|awk '{print $NF}'|cut -d\/ -f1|wc -l`
3 if [ $count -ne 0 ]
4 then
5 pid=`netstat -anp|grep ":3000"|grep LISTEN|awk '{print $NF}'|cut -d\/ -f1`;
6 echo "Need to kill PID $pid";
7 kill $pid
8 fi
9 exit 0
然后在脚本下:它 运行s 与我的 server.js 的节点,给它 3 秒启动然后 运行s 测试它。
这个测试测试完成后还将负责杀死节点 PID。
那么让我们提交并检查构建的状态
现在让我们将 server.js 更改为输出“Hello World”而不是“Hello World”,因此中间没有 space。我希望我的测试用例会失败,因为它期望字面上的“Hello World”。它确实失败了。
这是我能想到的最简单的 CI 用例。
现在,如果基于测试用例的状态,您想将代码部署到另一个环境 - 您将不得不开始使用
- 阶段
和
- 环境
所以你的 .gitlab-ci.yml 会变成这样的东西(实际工作示例)
cat -n .gitlab-ci.yml
1 stages:
2 - test
3 - deploy
4
5 run_test_case:
6 stage: test
7 before_script:
8 - echo "Before script"
9 - hostname
10 - /bin/bash cleanup.sh
11
12 script:
13 - echo "Main Script"
14 - node server.js &
15 - sleep 3
16 - /bin/bash simpletest.sh
17
18 deploy_to_production:
19 stage: deploy
20 script:
21 - echo "Run code here to do production deployment"
22 environment:
23 name: production
git 推送将成功。
在第 21 行我只是 运行 回显,但这可以替换为将推送到您的远程暂存或生产环境。
我有一个配置了 GitLab CE 的 AWS Ubuntu 实例。现在我想配置 GitLab CI 以在每个 commit.I 之后部署我的 NodeJS 应用程序对此没有任何适当的逐步解决方案。
我的 NodeJS 应用 运行 在 /var/www/mean/my-app
上 http://myapp.mydomain.com
并且托管由 Apache Proxy 处理,
<VirtualHost *:80>
ServerAdmin anshad@mydomain.com
ServerName gitlab.mydomain.com
ServerAlias www.gitlab.mydomain.com
ServerSignature Off
ProxyPreserveHost On
AllowEncodedSlashes NoDecode
<Location />
Require all granted
ProxyPassReverse http://localhost:8080
ProxyPassReverse http://gitlab.mydomain.com/
</Location>
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_URI} ^/uploads/.*
RewriteRule .* http://127.0.0.1:8080%{REQUEST_URI} [P,QSA,NE]
DocumentRoot /home/git/gitlab/public
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b" common_forwarded
ErrorLog /var/log/apache2/gitlab_error.log
CustomLog /var/log/apache2/gitlab_forwarded.log common_forwarded
CustomLog /var/log/apache2/gitlab_access.log combined env=!dontlog
CustomLog /var/log/apache2/gitlab.log combined
</VirtualHost>
并且该应用程序使用 forever 模块进行引导
forever start app.js
git实验室配置检查 sudo -u git -H bundle exec rake gitlab:check RAILS_ENV=production
给出,
Checking GitLab Shell ...
GitLab Shell version >= 4.0.0 ? ... OK (4.0.0)
Repo base directory exists?
default... yes
Repo storage directories are symlinks?
default... no
Repo paths owned by git:git?
default... yes
Repo paths access is drwxrws---?
default... yes
hooks directories in repos are links: ...
dev / my-app ... ok
Running /home/git/gitlab-shell/bin/check
Check GitLab API access: OK
Access to /home/git/.ssh/authorized_keys: OK
Send ping to redis server: OK
gitlab-shell self-check successful
Checking GitLab Shell ... Finished
Checking Sidekiq ...
Running? ... yes
Number of Sidekiq processes ... 1
Checking Sidekiq ... Finished
Checking Reply by email ...
Reply by email is disabled in config/gitlab.yml
Checking Reply by email ... Finished
Checking LDAP ...
LDAP is disabled in config/gitlab.yml
Checking LDAP ... Finished
Checking GitLab ...
Git configured with autocrlf=input? ... yes
Database config exists? ... yes
All migrations up? ... yes
Database contains orphaned GroupMembers? ... no
GitLab config exists? ... yes
GitLab config outdated? ... no
Log directory writable? ... yes
Tmp directory writable? ... yes
Uploads directory setup correctly? ... yes
Init script exists? ... yes
Init script up-to-date? ... yes
projects have namespace: ...
dev / my-app ... yes
Redis version >= 2.8.0? ... yes
Ruby version >= 2.1.0 ? ... yes (2.3.1)
Your git bin path is "/usr/bin/git"
Git version >= 2.7.3 ? ... yes (2.7.4)
Active users: 1
Checking GitLab ... Finished
我曾经使用 SSH 从我的系统登录实例,
ssh -i API-Key.pem ubuntu@ec2-XX-XX-XXX-XXX.ap-south-1.compute.amazonaws.com
使用命令创建密钥
ssh-keygen -t rsa
/etc/gitlab-runner/config.toml
concurrent = 1
check_interval = 0
[[runners]]
name = "Production Runner"
url = "http://gitlab.mydomain.com/ci"
token = "xxxxxxxxxxxxxxxxxxxxxxxxxxx"
executor = "ssh"
[runners.ssh]
user = "ubuntu"
host = "ip-XXX-XX-XX-XXX"
identity_file = "/home/ubuntu/.ssh/id_rsa"
[runners.cache]
.gitlab-ci.yml
test_async:
script:
- npm install
因为我配置不好,运行报错,
Running with gitlab-ci-multi-runner 1.7.1 (f896af7)
Using SSH executor...
ERROR: Preparation failed: ssh: handshake failed: ssh: unable to authenticate, attempted methods [none publickey], no supported methods remain
Will be retried in 3s ...
我的困惑是:
要将提交的代码部署到应用程序位置,.gitlab-ci.yml
文件的内容应该是什么?
以及如何为此配置运行器?如果我必须使用ssh
转轮,那那边的配置应该是什么?
更新:
将 .pem
文件作为 identity_file
提供后,出现以下错误
Running with gitlab-ci-multi-runner 1.7.1 (f896af7)
Using SSH executor...
Running on ip-xxx-xx-xx-xxx via ip-xxx-xx-xx-xxx...
Cloning repository...
Cloning into 'builds/a92f1b91/0/dev/my-app'...
fatal: unable to access 'http://gitlab-ci-token:xxxxxxxxxxxxxxxxxxxx@gitlab.mydomain.com/dev/my-app.git/': The requested URL returned error: 500
ERROR: Build failed: Process exited with: 1. Reason was: ()
现在有一个问题,git 使用 http 克隆不起作用,但 ssh 克隆有效。
注意:git实验室和构建环境都是同一个主机(同一个aws实例)
Bug 也在 GitLab 中报告(http 克隆问题)。
在你的 /etc/gitlab-runner/config.toml
concurrent = 1
check_interval = 0
[[runners]]
name = "Production Runner"
url = "http://gitlab.mydomain.com/ci"
token = "xxxxxxxxxxxxxxxxxxxxxxxxxxx"
executor = "ssh"
[runners.ssh]
user = "ubuntu"
host = "ip-XXX-XX-XX-XXX"
identity_file = "/home/ubuntu/.ssh/id_rsa"
[runners.cache]
你来定义
- 主机
- 用户
- 和身份文件
host 应该是您的构建主机 IP(换句话说,您将在其中执行构建)
user 应该是您在 Build host 上的用户。 不在 git实验室主机上。
您可以测试无密码 ssh 的工作方式
- 以 root 身份登录 gitlab 主机
- ssh -i /home/ubuntu/.ssh/id_rsa ubuntu@ip-XXX-XX-XX-XXX
如果可行并且不要求您输入密码 - 一切都很好。
如果出现问题 - 表示您没有正确设置无密码身份验证。
设置密码的最简单方法 public 基于密钥的身份验证是使用名为
的命令ssh-copy-id
例如,我想在我的 gitlab 和我的构建主机之间设置无密码的 ssh 身份验证。
我的构建主机 ip 是 192.168.0.42,主机名是 build.home
我已经在 gitlab 主机上的 /home/ubuntu/.ssh 下生成了 id_rsa 和 id_rsa.pub。
现在让我们将 public 密钥从 gitlab 主机推送到我们的构建主机。第一次它会要求您输入密码。
[root@gitlab ~]# ssh-copy-id -i /home/ubuntu/.ssh/id_rsa.pub ubuntu@build.home
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
ubuntu@build.home's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'ubuntu@build.home'"
and check to make sure that only the key(s) you wanted were added.
请注意,在上面的示例中,我将 public 密钥推送到远程主机。
但是当我现在连接到此远程主机时 - 我将指定我的私钥。
[root@gitlab ~]# ssh -i /home/ubuntu/.ssh/id_rsa ubuntu@build.home
[ubuntu@build ~]$ hostname
build.home
尝试在 git 实验室主机和远程主机之间测试您的 public 密钥验证并更新您的问题。
资源:
https://gitlab.com/gitlab-org/gitlab-ci-multi-runner/blob/master/docs/executors/ssh.md
P.S: 我会post我今天晚些时候回家配置我的工作环境。
编辑 1:
这是我的配置。
我的 git 实验室主机名为 gitlab.home 192.168.0.41
我还有另一个名为 sshbuild.home 192.168.0.43
下面是我添加ssh的方法运行ner
第 1 步。在我的 gitlab.home 上安装
yum install gitlab-ci-multi-runner
并将我的远程 sshbuild.home VM 注册为 ssh 运行ner
我还需要确保无密码身份验证在我的 gitlab.home 和 sshbuild.home 之间有效,所以
[root@gitlab gitlab-runner]# ssh-copy-id 192.168.0.43
The authenticity of host '192.168.0.43 (192.168.0.43)' can't be established.
ECDSA key fingerprint is b4:6a:1b:72:d1:7d:1f:34:f7:bb:ef:ad:69:42:11:13.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.0.43's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '192.168.0.43'"
and check to make sure that only the key(s) you wanted were added.
[root@gitlab gitlab-runner]# ssh 192.168.0.43
Last login: Fri Nov 18 17:05:06 2016 from 192.168.0.101
[root@sshbuild ~]# exit
然后我禁用了我的其他 运行ner (shell 运行ner) 并制作了新的 SSH 运行ner 项目特定只是为了确保当我提交 -它将在 ssh 运行ner
上执行我确实提交了,瞧 - 我们在 sshbuild.home 主机
上进行了 运行 的成功测试这里有几个链接可能有助于更好地理解这个主题
- https://about.gitlab.com/2016/07/29/the-basics-of-gitlab-ci/
- https://docs.gitlab.com/ce/ci/runners/README.html
- http://docs.gitlab.com/runner/commands/README.html
- https://docs.gitlab.com/ce/ci/yaml/README.html
P.S:这是我的 /etc/gitlab-runner/config.toml 文件
[root@gitlab gitlab-runner]# cat /etc/gitlab-runner/config.toml
concurrent = 1
check_interval = 0
[[runners]]
name = "sshbuild"
url = "http://gitlab.home/"
token = "2bc1825d8fbde09fd632637c60e9e7"
executor = "ssh"
[runners.ssh]
user = "root"
host = "192.168.0.43"
port = "22"
identity_file = "/root/.ssh/id_rsa"
[runners.cache]
P.S:如果我在 Web 界面的“设置”下为我的存储库禁用 HTTP,我会遇到与您类似的错误。但是错误不是 500,而是 403。
编辑 2:
现在我将介绍 .gitlab-ci.yml 基于一个简单的 HelloWorld 项目
在我的 HelloWorld 中,我有一个名为 server.js 的文件,当从节点 运行 时 - 将简单地在端口 3000 上创建 Web 服务器 运行ning 并在 GET 请求上回复“Hello World”。
1 const http = require('http');
2
3 const hostname = '0.0.0.0';
4 const port = 3000;
5
6 const server = http.createServer((req, res) => {
7 res.statusCode = 200;
8 res.setHeader('Content-Type', 'text/plain');
9 res.end('Hello World!\n');
10 });
11
12 server.listen(port, hostname, () => {
13 console.log(`Server running at http://${hostname}:${port}/`);
14 });
我的目标是能够运行 一个针对它的测试用例。在这种情况下,我将 运行 简单
curl localhost:3000 | grep "Hello World"
但我需要将其放入一个单独的脚本中,成功时退出状态为 0,失败时退出状态为非零
cat -n simpletest.sh
1 #!/bin/bash
2
3 cleanup ()
4 {
5 count=`netstat -anp|grep ":3000"|grep LISTEN|awk '{print $NF}'|cut -d\/ -f1|wc -l`
6 if [ $count -ne 0 ]
7 then
8 pid=`netstat -anp|grep ":3000"|grep LISTEN|awk '{print $NF}'|cut -d\/ -f1`;
9 echo "Need to kill PID $pid";
10 kill $pid
11 fi
12 }
13
14 echo "Running simple test"
15 curl localhost:3000|grep "Hello World"
16 if [ $? -eq 0 ]
17 then
18 echo "Test was successfull"
19 echo "Clean up node.js process"
20 cleanup
21 exit 0
22 else
23 echo "Test failed"
24 echo "Clean up node.js process"
25 cleanup
26 exit 1
27 fi
现在让我们来介绍我的 .gitlab-ci.yml
cat -n .gitlab-ci.yml
1 test:
2
3 before_script:
4 - echo "Before script"
5 - hostname
6 - /bin/bash cleanup.sh
7
8 script:
9 - echo "Main Script"
10 - node server.js &
11 - sleep 3
12 - /bin/bash simpletest.sh
我有一个名为 test 的作业。
在 before_script 中,它 运行s cleanup.sh 脚本可以简单地杀死监听端口 3000 的 PID,以防发现。
cat -n cleanup.sh
1 #!/bin/bash
2 count=`netstat -anp|grep ":3000"|grep LISTEN|awk '{print $NF}'|cut -d\/ -f1|wc -l`
3 if [ $count -ne 0 ]
4 then
5 pid=`netstat -anp|grep ":3000"|grep LISTEN|awk '{print $NF}'|cut -d\/ -f1`;
6 echo "Need to kill PID $pid";
7 kill $pid
8 fi
9 exit 0
然后在脚本下:它 运行s 与我的 server.js 的节点,给它 3 秒启动然后 运行s 测试它。
这个测试测试完成后还将负责杀死节点 PID。
那么让我们提交并检查构建的状态
现在让我们将 server.js 更改为输出“Hello World”而不是“Hello World”,因此中间没有 space。我希望我的测试用例会失败,因为它期望字面上的“Hello World”。它确实失败了。
这是我能想到的最简单的 CI 用例。
现在,如果基于测试用例的状态,您想将代码部署到另一个环境 - 您将不得不开始使用
- 阶段 和
- 环境
所以你的 .gitlab-ci.yml 会变成这样的东西(实际工作示例)
cat -n .gitlab-ci.yml
1 stages:
2 - test
3 - deploy
4
5 run_test_case:
6 stage: test
7 before_script:
8 - echo "Before script"
9 - hostname
10 - /bin/bash cleanup.sh
11
12 script:
13 - echo "Main Script"
14 - node server.js &
15 - sleep 3
16 - /bin/bash simpletest.sh
17
18 deploy_to_production:
19 stage: deploy
20 script:
21 - echo "Run code here to do production deployment"
22 environment:
23 name: production
git 推送将成功。
在第 21 行我只是 运行 回显,但这可以替换为将推送到您的远程暂存或生产环境。