FIX test devops

This commit is contained in:
Jasder 2020-07-21 17:08:07 +08:00
parent 20d8f21ebb
commit 0f92aa7981
6 changed files with 213 additions and 137 deletions

318
README.md
View File

@ -2508,7 +2508,9 @@ GET /api/dev_ops/builds
*示例* *示例*
``` ```
curl -X GET http://localhost:3000/api/dev_ops/builds | jq curl -X GET \
-d 'id=4844' \
http://localhost:3000/api/dev_ops/builds | jq
``` ```
*请求参数说明:* *请求参数说明:*
@ -2530,29 +2532,31 @@ curl -X GET http://localhost:3000/api/dev_ops/builds | jq
``` ```
[ [
{ {
"id": 100207, "id": 1,
"repo_id": 296163, "repo_id": 8,
"number": 42, "trigger": "@hook",
"number": 1,
"status": "success", "status": "success",
"event": "pull_request", "event": "push",
"action": "sync", "action": "",
"link": "https://github.com/octoat/hello-world/compare/e3320539a4c0...9fc1ad6ebf12", "link": "",
"message": "updated README", "timestamp": 0,
"before": "e3320539a4c03ccfda992641646deb67d8bf98f3", "message": "更新 '.trustie-pipeline.yml'\n",
"after": "9fc1ad6ebf12462f3f9773003e26b4c6f54a772e", "before": "5e7c6f7dfd5ce6cc6e287fcbc000dadd9992b324",
"after": "5e52ce51a239f5c8dd0b489a8a71e94f976179b4",
"ref": "refs/heads/master", "ref": "refs/heads/master",
"source_repo": "spaceghost/hello-world", "source_repo": "",
"source": "develop", "source": "master",
"target": "master", "target": "master",
"author_login": "octocat", "author_login": "jasder",
"author_name": "The Octocat", "author_name": "jasder",
"author_email": "octocat@github.com", "author_email": "email.com",
"author_avatar": "http://www.gravatar.com/avatar/7194e8d48fa1d2b689f99443b767316c", "author_avatar": "",
"sender": "bradrydzewski", "sender": "jasder",
"started": 1564085874, "started": 1595317786,
"finished": 1564086343, "finished": 1595318426,
"created": 1564085874, "created": 1595317786,
"updated": 1564085874, "updated": 1595317786,
"version": 3 "version": 3
} }
] ]
@ -2566,13 +2570,16 @@ GET /api/dev_ops/builds/:number
*示例* *示例*
``` ```
curl -X GET http://localhost:3000/api/dev_ops/builds/42 | jq curl -X GET \
-d 'id=4844' \
http://ocalhost:3000/api/dev_ops/builds/1 | jq
``` ```
*请求参数说明:* *请求参数说明:*
|参数名|必选|类型|说明| |参数名|必选|类型|说明|
|-|-|-|-| |-|-|-|-|
|id |int|repository's id|
|number |是|int |build's number | |number |是|int |build's number |
*返回参数说明:* *返回参数说明:*
@ -2586,53 +2593,79 @@ curl -X GET http://localhost:3000/api/dev_ops/builds/42 | jq
返回值 返回值
``` ```
{ {
"id": 100207, "id": 1,
"repo_id": 296163, "repo_id": 8,
"number": 42, "trigger": "@hook",
"status": "pending", "number": 1,
"event": "pull_request", "status": "success",
"action": "sync", "event": "push",
"link": "https://github.com/octoat/hello-world/compare/e3320539a4c0...9fc1ad6ebf12", "action": "",
"message": "updated README", "link": "http://localhost:3000/jasder/forgeplus/compare/5e7c6f7dfd5ce6cc6e287fcbc000dadd9992b324...5e52ce51a239f5c8dd0b489a8a71e94f976179b4",
"before": "e3320539a4c03ccfda992641646deb67d8bf98f3", "timestamp": 0,
"after": "9fc1ad6ebf12462f3f9773003e26b4c6f54a772e", "message": "更新 '.trustie-pipeline.yml'\n",
"before": "5e7c6f7dfd5ce6cc6e287fcbc000dadd9992b324",
"after": "5e52ce51a239f5c8dd0b489a8a71e94f976179b4",
"ref": "refs/heads/master", "ref": "refs/heads/master",
"source_repo": "spaceghost/hello-world", "source_repo": "",
"source": "develop", "source": "master",
"target": "master", "target": "master",
"author_login": "octocat", "author_login": "jasder",
"author_name": "The Octocat", "author_name": "jasder",
"author_email": "octocat@github.com", "author_email": "2053003901@qq.com",
"author_avatar": "http://www.gravatar.com/avatar/7194e8d48fa1d2b689f99443b767316c", "author_avatar": "http://localhost:3000/user/avatar/jasder/-1",
"sender": "bradrydzewski", "sender": "jasder",
"started": 0, "started": 1595317786,
"finished": 0, "finished": 1595318426,
"created": 1564085874, "created": 1595317786,
"updated": 1564085874, "updated": 1595317786,
"version": 1, "version": 3,
"stages": [ "stages": [
{ {
"id": 199937, "id": 1,
"repo_id": 296163, "repo_id": 8,
"build_id": 100207, "build_id": 1,
"number": 1,
"name": "default",
"kind": "pipeline",
"type": "docker",
"status": "success",
"errignore": false,
"exit_code": 0,
"machine": "121.36.81.172",
"os": "linux",
"arch": "arm64",
"started": 1595317786,
"stopped": 1595318426,
"created": 1595317786,
"updated": 1595318426,
"version": 4,
"on_success": true,
"on_failure": false,
"steps": [
{
"id": 1,
"step_id": 1,
"number": 1, "number": 1,
"name": "default", "name": "clone",
"kind": "pipeline", "status": "success",
"type": "docker",
"status": "pending",
"errignore": false,
"exit_code": 0, "exit_code": 0,
"machine": "15e89c0f84f1", "started": 1595317786,
"os": "linux", "stopped": 1595318373,
"arch": "amd64", "version": 4
"started": 0, },
"stopped": 0, {
"created": 1564085874, "id": 2,
"updated": 1564086343, "step_id": 1,
"version": 1, "number": 2,
"on_success": true, "name": "test",
"on_failure": false "status": "success",
} "exit_code": 0,
"started": 1595318373,
"stopped": 1595318426,
"version": 4
}
]
}
] ]
} }
``` ```
@ -2645,13 +2678,16 @@ POST /api/dev_ops/builds/:number
*示例* *示例*
``` ```
curl -X POST http://localhost:3000/api/dev_ops/builds/42 | jq curl -X POST \
-d 'id=4844' \
http://localhost:3000/api/dev_ops/builds/1 | jq
``` ```
*请求参数说明:* *请求参数说明:*
|参数名|必选|类型|说明| |参数名|必选|类型|说明|
|-|-|-|-| |-|-|-|-|
|id |int|repository's id|
|number |是|int |build's number | |number |是|int |build's number |
*返回参数说明:* *返回参数说明:*
@ -2665,54 +2701,32 @@ curl -X POST http://localhost:3000/api/dev_ops/builds/42 | jq
返回值 返回值
``` ```
{ {
"id": 100207, "id": 2,
"repo_id": 296163, "repo_id": 8,
"number": 42, "trigger": "jasder",
"number": 2,
"status": "pending", "status": "pending",
"event": "pull_request", "event": "push",
"action": "sync", "action": "",
"link": "https://github.com/octoat/hello-world/compare/e3320539a4c0...9fc1ad6ebf12", "link": "http://localhost:3000/jasder/forgeplus/compare/5e7c6f7dfd5ce6cc6e287fcbc000dadd9992b324...5e52ce51a239f5c8dd0b489a8a71e94f976179b4",
"message": "updated README", "timestamp": 0,
"before": "e3320539a4c03ccfda992641646deb67d8bf98f3", "message": "更新 '.trustie-pipeline.yml'\n",
"after": "9fc1ad6ebf12462f3f9773003e26b4c6f54a772e", "before": "5e7c6f7dfd5ce6cc6e287fcbc000dadd9992b324",
"after": "5e52ce51a239f5c8dd0b489a8a71e94f976179b4",
"ref": "refs/heads/master", "ref": "refs/heads/master",
"source_repo": "spaceghost/hello-world", "source_repo": "",
"source": "develop", "source": "master",
"target": "master", "target": "master",
"author_login": "octocat", "author_login": "jasder",
"author_name": "The Octocat", "author_name": "jasder",
"author_email": "octocat@github.com", "author_email": "2053003901@qq.com",
"author_avatar": "http://www.gravatar.com/avatar/7194e8d48fa1d2b689f99443b767316c", "author_avatar": "http://localhost:3000/user/avatar/jasder/-1",
"sender": "bradrydzewski", "sender": "jasder",
"started": 0, "started": 0,
"finished": 0, "finished": 0,
"created": 1564085874, "created": 1595321350,
"updated": 1564085874, "updated": 1595321350,
"version": 1, "version": 1
"stages": [
{
"id": 199937,
"repo_id": 296163,
"build_id": 100207,
"number": 1,
"name": "default",
"kind": "pipeline",
"type": "docker",
"status": "pending",
"errignore": false,
"exit_code": 0,
"machine": "15e89c0f84f1",
"os": "linux",
"arch": "amd64",
"started": 0,
"stopped": 0,
"created": 1564085874,
"updated": 1564086343,
"version": 1,
"on_success": true,
"on_failure": false
}
]
} }
``` ```
--- ---
@ -2724,13 +2738,16 @@ DELETE /api/dev_ops/builds/:number
*示例* *示例*
``` ```
curl -X DELETE http://localhost:3000/api/dev_ops/builds/42 | jq curl -X DELETE \
-d 'id=4844' \
http://localhost:3000/api/dev_ops/builds/42 | jq
``` ```
*请求参数说明:* *请求参数说明:*
|参数名|必选|类型|说明| |参数名|必选|类型|说明|
|-|-|-|-| |-|-|-|-|
|id |int|repository's id|
|number |是|int |build's number | |number |是|int |build's number |
*返回参数说明:* *返回参数说明:*
@ -2743,6 +2760,82 @@ curl -X DELETE http://localhost:3000/api/dev_ops/builds/42 | jq
返回值 返回值
``` ```
{
"id": 2,
"repo_id": 8,
"trigger": "jasder",
"number": 2,
"status": "killed",
"event": "push",
"action": "",
"link": "http://localhost:3000/jasder/forgeplus/compare/5e7c6f7dfd5ce6cc6e287fcbc000dadd9992b324...5e52ce51a239f5c8dd0b489a8a71e94f976179b4",
"timestamp": 0,
"message": "更新 '.trustie-pipeline.yml'\n",
"before": "5e7c6f7dfd5ce6cc6e287fcbc000dadd9992b324",
"after": "5e52ce51a239f5c8dd0b489a8a71e94f976179b4",
"ref": "refs/heads/master",
"source_repo": "",
"source": "master",
"target": "master",
"author_login": "jasder",
"author_name": "jasder",
"author_email": "2053003901@qq.com",
"author_avatar": "http://localhost:3000/user/avatar/jasder/-1",
"sender": "jasder",
"started": 1595321352,
"finished": 1595321590,
"created": 1595321350,
"updated": 1595321352,
"version": 3,
"stages": [
{
"id": 2,
"repo_id": 8,
"build_id": 2,
"number": 1,
"name": "default",
"kind": "pipeline",
"type": "docker",
"status": "killed",
"errignore": false,
"exit_code": 0,
"machine": "121.36.81.172",
"os": "linux",
"arch": "arm64",
"started": 1595321352,
"stopped": 1595321590,
"created": 1595321350,
"updated": 1595321352,
"version": 4,
"on_success": true,
"on_failure": false,
"steps": [
{
"id": 3,
"step_id": 2,
"number": 1,
"name": "clone",
"status": "killed",
"exit_code": 130,
"started": 1595321353,
"stopped": 1595321590,
"version": 3
},
{
"id": 4,
"step_id": 2,
"number": 2,
"name": "test",
"status": "skipped",
"exit_code": 130,
"started": 1595321590,
"stopped": 1595321590,
"version": 2
}
]
}
]
}
``` ```
--- ---
@ -2753,14 +2846,17 @@ GET /api/dev_ops/builds/:number/logs/:stage/:step
*示例* *示例*
``` ```
curl -X GET http://localhost:3000/api/dev_ops/builds/42/logs/ | jq curl -X GET \
-d 'id=4844' \
http://localhost:3000/api/dev_ops/builds/42/logs/ | jq
``` ```
*请求参数说明:* *请求参数说明:*
|参数名|必选|类型|说明| |参数名|必选|类型|说明|
|-|-|-|-| |-|-|-|-|
|number |是|int |build's number | |id |int|repository's id|
|number |是|int |build's number |
|stage |是|int |build's stage id | |stage |是|int |build's stage id |
|step |是|int |build's step id | |step |是|int |build's step id |

View File

@ -1,6 +1,6 @@
class ::DevOps::BuildsController < ApplicationController class ::DevOps::BuildsController < ApplicationController
before_action :require_login, except: :test_webhook before_action :require_login
before_action :find_repo, except: :test_webhook before_action :find_repo
def index def index
cloud_account = @repo.dev_ops_cloud_account cloud_account = @repo.dev_ops_cloud_account
@ -31,16 +31,11 @@ class ::DevOps::BuildsController < ApplicationController
def logs def logs
cloud_account = @repo.dev_ops_cloud_account cloud_account = @repo.dev_ops_cloud_account
result = DevOps::Drone::API.new(cloud_account.drone_token, cloud_account.drone_url, @repo.user.login, @repo.identifier, build: params[:build], stage: params[:stage], step: sync_params[:step]).logs result = DevOps::Drone::API.new(cloud_account.drone_token, cloud_account.drone_url, @repo.user.login, @repo.identifier, build: params[:build], stage: params[:stage], step: params[:step]).logs
render json: result render json: result
end end
def test_webhook
logger.info "==============已经回调成功了....."
render_ok
end
private private
def find_repo def find_repo
@repo = ::Repository.find params[:id] @repo = ::Repository.find params[:id]

View File

@ -50,7 +50,7 @@ class DevOps::CloudAccountsController < ApplicationController
logger.info "######### result: #{result}" logger.info "######### result: #{result}"
redirect_url = "#{Gitea.gitea_config[:domain]}/login/oauth/authorize?client_id=#{oauth.client_id}&redirect_uri=#{cloud_account.drone_url}/login&response_type=code" redirect_url = "#{cloud_account.drone_url}/login&response_type=code"
logger.info "######### redirect_url: #{redirect_url}" logger.info "######### redirect_url: #{redirect_url}"
if result if result
render_ok(redirect_url: redirect_url) render_ok(redirect_url: redirect_url)

View File

@ -11,23 +11,10 @@ class DevOps::Drone::Client
@rpc_secret = rpc_secret @rpc_secret = rpc_secret
end end
def run
`docker run -d \
-v /var/run/docker.sock:/var/run/docker.sock \
-e DRONE_PRC_HOST=drone-server-#{client_id}:9000 \
-e DRONE_RPC_SECRET=#{rpc_secret} \
-e DRONE_RUNNER_NAME=#{drone_ip} \
--restart always \
--name drone-agent--#{client_id} \
--net="bridge" \
drone/drone-runner-docker:1
`
end
def generate_cmd def generate_cmd
"docker run -d \ "docker run -d \
-v /var/run/docker.sock:/var/run/docker.sock \ -v /var/run/docker.sock:/var/run/docker.sock \
-e DRONE_RPC_HOST=#{drone_ip}:9000 \ -e DRONE_RPC_HOST=#{drone_ip}:80 \
-e DRONE_RPC_SECRET=#{rpc_secret} \ -e DRONE_RPC_SECRET=#{rpc_secret} \
-e DRONE_RUNNER_NAME=#{drone_ip} \ -e DRONE_RUNNER_NAME=#{drone_ip} \
--restart always \ --restart always \

View File

@ -46,7 +46,6 @@ class DevOps::Drone::Server
-e DRONE_SERVER_HOST=#{drone_host} \ -e DRONE_SERVER_HOST=#{drone_host} \
-e DRONE_SERVER_PROTO=http \ -e DRONE_SERVER_PROTO=http \
-p '80:80' \ -p '80:80' \
-p '9000:9000' \
--restart=always \ --restart=always \
--detach=true \ --detach=true \
--name=drone-server-#{client_id} \ --name=drone-server-#{client_id} \

View File

@ -27,8 +27,7 @@ Rails.application.routes.draw do
get ':number', to: 'builds#detail', as: 'detail' get ':number', to: 'builds#detail', as: 'detail'
post ':number', to: 'builds#restart', as: 'restart' post ':number', to: 'builds#restart', as: 'restart'
delete ':number', to: 'builds#delete', as: 'delete' delete ':number', to: 'builds#delete', as: 'delete'
get ':number/logs/:stage/:step', to: 'builds#detail', as: 'logs' get ':number/logs/:stage/:step', to: 'builds#logs', as: 'logs'
post 'test_webhook', to: 'builds#test_webhook', as: 'test_webhook'
end end
end end
end end