This repository was archived by the owner on Apr 26, 2021. It is now read-only.
forked from capistrano/github
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathgithub.rake
More file actions
92 lines (74 loc) · 2.51 KB
/
Copy pathgithub.rake
File metadata and controls
92 lines (74 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
require 'capistrano/github/api'
set_if_empty :github_deployment_payload, -> do
{
}
end
set_if_empty :github_deployment, -> do
{
auto_merge: false,
environment: fetch(:stage)
}
end
set_if_empty :github_deployment_api, -> do
Capistrano::Github::API.new(fetch(:repo_url), fetch(:github_access_token))
end
set :github_deployment_required, false
set :github_deployment_enabled, -> do
begin
fetch(:github_deployment_api)
rescue Capistrano::Github::API::MissingAccessToken
false
end
end
set :github_deployment_skip, -> do
!fetch(:github_deployment_enabled) && !fetch(:github_deployment_required)
end
namespace :github do
namespace :deployment do
desc 'Create new deployment'
task :create do
next if fetch(:github_deployment_skip) || fetch(:current_github_deployment)
gh = fetch(:github_deployment_api)
payload = fetch(:github_deployment_payload)
config = fetch(:github_deployment).merge(payload: payload)
branch = fetch(:branch)
set :current_github_deployment, deployment = gh.create_deployment(branch, config)
run_locally do
info("Created GitHub Deployment #{deployment}")
end
end
[:pending, :success, :error, :failure].each do |status|
desc "Mark current deployment as #{status}"
task status => 'github:deployment:create' do
next if fetch(:github_deployment_skip)
gh = fetch(:github_deployment_api)
deployment = fetch(:current_github_deployment)
run_locally do
if deployment
gh.create_deployment_status(deployment, status)
info("Marked GitHub Deployment #{deployment} as #{status}")
else
info("No GitHub Deployment found, could not mark as #{status}")
end
end
end
end
end
print_deployment = -> d { puts "Deployment##{d.id} (#{d.last_state}): #{d.created_at} #{d.ref}@#{d.sha} to #{d.environment} by @#{d.creator_login}" }
print_status = -> s { puts "\t#{s.created_at} state: #{s.state}" }
desc 'List Github deployments'
task :deployments do
gh = fetch(:github_deployment_api)
env = fetch(:github_deployment)[:environment]
gh.deployments(environment: env).each do |deployment|
deployment.tap(&print_deployment)
deployment.statuses.each(&print_status)
end
end
desc 'Show last Github deploy'
task :last_deploy do
gh = fetch(:github_deployment_api)
env = fetch(:github_deployment)[:environment]
gh.deployments(environment: env).first.tap(&print_deployment)
end
end