-
Notifications
You must be signed in to change notification settings - Fork 1
/
.drone.yml
159 lines (136 loc) · 6.4 KB
/
.drone.yml
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
kind: pipeline
type: docker
name: Re-deploy Swarm Stack
# After you push the changes to the GitHub repository, the preconfigured automated build process is started in the Docker Hub. When the container are successfully built from the chosen Dockerfiles, the pipeline can then proceed with the next deployment step. In that step the new versions of the containers are being pulled from the Docker Registry and re-deployed in the swarm stack.
# Any pipeline step can be triggered on the so-called 'custom' event coming from the docker hub using it's outgoing webhook (or from any other CI tool or container registry, such as GitHub registry or similar: https://github.com/features/packages):
# http://IP_OR_DNS:PORT/api/repos/<github-user>/<github-repo-name>/builds?branch=<branch-name>&access_token=<32-characters-long-hash>&your_custom_key=value
# The personal bearer auth token can be found in your Drone account: http://IP_OR_DNS:PORT/account
# There are more pipeline parameters, including own custom parameters (&your_custom_key=value), and other details on this page: https://docs.drone.io/api/builds/build_create/
# E.g. 'build_tag' is a custom parameter being passed along with the Docker Hub's Webhook in this URL: http://IP:PORT/api/repos/<github-user>/<github-repo-name>/builds?access_token=<32-characters-long-hash>&build_tag=frontend-docker-build
# This parameter will be available in every pipeline step as an environment variable (see below).
# Finally, set up that url in the docker hub webhook's tab in the repository that's being built last of all: https://hub.docker.com/repository/docker/<organization/repo-name>/webhooks
# Deployments can also be triggered using GitHub's actions and/or plugins:
# https://probot.github.io/docs/deployment/
# https://developer.github.com/webhooks/#events
# https://developer.github.com/v3/repos/deployments/
steps:
# global environment variables could (also) be defined here:
# global-variables:
# https://laszlo.cloud/drone-environment-variables-three-tips#reusing-common-variable-groups-with-yaml-anchors
# - name: build
# to build and publish docker images use: http://plugins.drone.io/banzaicloud/drone-kaniko/
# the next step is: ssh into the machine and deploy the pulled docker containers.
- name: deploy
# http://plugins.drone.io/appleboy/drone-ssh/
image: appleboy/drone-ssh
when:
event:
- custom
# other events:
# - promote
# - cron
# - push
# - pull_request
# - tag
# - rollback
# branch:
# - dockerized
# target:
# - production
# https://docs.drone.io/pipeline/docker/syntax/conditions/#by-status
# https://docs.drone.io/promote/
# environment variables that are accessible in the scope of the current step:
environment:
FILE_PATH:
from_secret: file_path
FILE_NAME:
from_secret: file_name
STACK_NAME:
from_secret: stack_name
# This parameter is available in a pipeline step as an environment variable:
build_tag: build_tag
settings:
host:
from_secret: ssh_host
username:
from_secret: ssh_username
password:
from_secret: ssh_password
# https://github.com/appleboy/drone-ssh/issues/130
# internal plugin-related environment variables:
# key:
# from_secret: ssh_key
# passphrase: 1234
port: 22
envs: [FILE_PATH, FILE_NAME, STACK_NAME, build_tag]
script:
- echo "The deployment has started."
- echo "Docker Compose file path is:"
- echo $FILE_PATH/$FILE_NAME
- echo "The name of the stack file:"
- echo $STACK_NAME
- echo "Build tag as a parameter expression:"
- echo ${build_tag}
- export SERVICE=$${STACK_NAME}_${build_tag}
# - docker stack services $STACK_NAME
# - docker stop $SERVICE
# - docker stack rm $STACK_NAME # if you need to shut down & remove the stack first
# - docker pull company/project:${DRONE_COMMIT}
# either only update one service using this command:
# - docker service update --force $STACK_NAME_$build_tag
# then check its status:
- docker service ps $SERVICE
# https://docs.docker.com/engine/reference/commandline/service_update/
# escaping environment variables: https://laszlo.cloud/drone-environment-variables-three-tips
# or update the whole stack:
- docker stack deploy -c $FILE_PATH/$FILE_NAME $STACK_NAME
# clean the unused docker images:
- docker image prune -f
# https://discourse.drone.io/t/triggering-promotion-event/6634/7
# Notify the pre-configured Telegram BOT about the deployment status and github commit
- name: telgram_notify
image: appleboy/drone-telegram
when:
status:
- success
- failure
environment:
build_tag: build_tag
settings:
# The secrets below can be entered in the Drone UI > repo > settings
token:
from_secret: telegram_token
to:
from_secret: telegram_chat_id
format: html
template_vars:
docker_build_tag: ${build_tag}
message: >
{{#success build.status}}
✅ Build #{{build.number}} of <i>{{repo.name}}</i> triggered on <i>{{build.event}}</i> event succeeded.
Build tag: <b>{{tpl.docker_build_tag}}</b>
Commit Ref: <i>{{commit.ref}}</i>
📝 Commit by <i>{{commit.author}}</i> on branch <i>{{commit.branch}}</i>:
<pre>{{commit.message}}</pre>
🌐 <a href="{{ commit.link }}">Commit link</a>.
🌐 Build link: {{ build.link }}.
{{else}}
❌ Build #{{build.number}} of <i>{{repo.name}}</i> triggered on <i>{{build.event}}</i> event failed.
Build tag: <b>{{tpl.docker_build_tag}}</b>
Commit Ref: <i>{{commit.ref}}</i>
📝 Commit by <i>{{commit.author}}</i> on branch <i>{{commit.branch}}</i>:
<pre>{{commit.message}}</pre>
🌐 <a href="{{ commit.link }}">Commit link</a>.
🌐 Build link: {{ build.link }}.
{{/success}}
# https://core.telegram.org/bots/api#markdownv2-style
# https://docs.drone.io/pipeline/triggers/#by-event
trigger:
event:
exclude: [push]
# [ tag, pull_request, promote, rollback, custom ]
# ---
# kind: secret
# name: foo
# data: EUgsPFbn0xyNdsxKtbB6nwR2w7RdG1pzo299qmI4zQ==
# https://gist.github.com/phildionne/790d90a390c7d345bba71c20f56c0026