Skip to content

Commit 558ecbd

Browse files
committed
Add deployment scripts for automatical deployment.
Now all changes from prod branch will be deployed to server automatically by TravisCI. See also: - https://docs.travis-ci.com/user/deployment/ - https://docs.travis-ci.com/user/deployment/script - https://docs.travis-ci.com/user/encrypting-files/ - https://docs.travis-ci.com/user/encryption-keys/ - https://docs.travis-ci.com/user/environment-variables/ Fix #329
1 parent 611b825 commit 558ecbd

File tree

5 files changed

+110
-0
lines changed

5 files changed

+110
-0
lines changed

.travis.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@ env:
44
matrix:
55
- SPRING_PROFILES_ACTIVE=test
66
- SPRING_PROFILES_ACTIVE=travis
7+
global:
8+
- secure: "fBav56BzY+A+Vs1g9YSfo1oLDCO1rFrXl49lJkOA5/XpnsKEEs4lI2RcOzz0wiJKXFNgcliiAJWoYMI8Esqz+lkyFWan4ij5Co0UzJcytDuY+2o+jaqwx45DuDYPogABzT+hWjLCxQLLG46gUkChzT8kcvOOn6JxC7Ff8q5MnoM="
79

810
before_script:
911
- if [ "$SPRING_PROFILES_ACTIVE" = 'travis' ]; then
1012
mysql -u travis -e 'CREATE DATABASE mystamps CHARACTER SET utf8;';
13+
if [ "$TRAVIS_BRANCH" = 'prod' ]; then
14+
pip install --user ansible;
15+
fi;
1116
else
1217
npm install -g bootlint;
1318
pip install --user html5validator;
@@ -25,6 +30,14 @@ after_success:
2530
./src/main/scripts/ci/publish-code-coverage.sh;
2631
fi
2732

33+
deploy:
34+
provider: script
35+
script: ./src/main/scripts/ci/deploy.sh
36+
on:
37+
branch: prod
38+
condition: $SPRING_PROFILES_ACTIVE = 'travis'
39+
skip_cleanup: true
40+
2841
jdk:
2942
- oraclejdk8
3043

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
---
2+
3+
- hosts: all
4+
gather_facts: no
5+
remote_user: mystamps
6+
vars:
7+
remote_war_dir: /data/mystamps
8+
uptimerobot:
9+
monitorid: 'MyStamps'
10+
apikey: "{{ lookup('env', 'UPTIMEROBOT_APIKEY') }}"
11+
tasks:
12+
13+
- name: Getting info about WAR file
14+
stat:
15+
path: 'target/mystamps.war'
16+
get_checksum: no
17+
get_md5: no
18+
register: war_file
19+
become: no
20+
delegate_to: 127.0.0.1
21+
22+
- name: Ensuring that WAR file exists
23+
assert:
24+
that:
25+
war_file.stat.exists
26+
become: no
27+
delegate_to: 127.0.0.1
28+
29+
- name: Stopping monitoring
30+
uptimerobot:
31+
monitorid: "{{ uptimerobot.monitorid }}"
32+
apikey: "{{ uptimerobot.apikey }}"
33+
state: paused
34+
when: uptimerobot is defined and uptimerobot.monitorid != '' and uptimerobot.apikey != ''
35+
36+
- name: Stopping service
37+
service:
38+
name: mystamps
39+
state: stopped
40+
41+
- name: Copying WAR file
42+
copy:
43+
src: '../../../../../target/mystamps.war'
44+
dest: "{{ remote_war_dir }}/mystamps.war"
45+
owner: mystamps
46+
group: mystamps
47+
mode: '0755'
48+
backup: yes
49+
50+
- name: Starting service
51+
service:
52+
name: mystamps
53+
enabled: yes
54+
state: started
55+
56+
- name: Starting monitoring
57+
uptimerobot:
58+
monitorid: "{{ uptimerobot.monitorid }}"
59+
apikey: "{{ uptimerobot.apikey }}"
60+
state: started
61+
when: uptimerobot is defined and uptimerobot.monitorid != '' and uptimerobot.apikey != ''
62+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
my-stamps.ru
1.64 KB
Binary file not shown.

src/main/scripts/ci/deploy.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/bash
2+
3+
# Treat unset variables and parameters as an error when performing parameter expansion
4+
set -o nounset
5+
6+
# Exit immediately if command returns a non-zero status
7+
set -e errexit
8+
9+
# Return value of a pipeline is the value of the last command to exit with a non-zero status
10+
set -o pipefail
11+
12+
13+
CURRENT_DIR="$(dirname "${0:-.}")"
14+
INVENTORY="$CURRENT_DIR/ansible/mystamps.inventory"
15+
PLAYBOOK="$CURRENT_DIR/ansible/deploy.yml"
16+
PRIVATE_KEY="$CURRENT_DIR/ansible/mystamps_rsa"
17+
18+
cleanup() {
19+
rm -f "$PRIVATE_KEY"
20+
exit
21+
}
22+
trap 'cleanup' EXIT SIGHUP SIGINT SIGTERM
23+
24+
# Disable host key checking to suppress interactive prompt.
25+
# See: http://docs.ansible.com/ansible/intro_getting_started.html#host-key-checking
26+
export ANSIBLE_HOST_KEY_CHECKING=False
27+
28+
# Decrypt private key
29+
openssl aes-256-cbc -K $encrypted_bf07cb25089f_key -iv $encrypted_bf07cb25089f_iv -in "$PRIVATE_KEY.enc" -out "$PRIVATE_KEY" -d
30+
chmod 600 "$PRIVATE_KEY"
31+
32+
ansible-playbook --inventory-file="$INVENTORY" "$PLAYBOOK" --syntax-check
33+
34+
exec ansible-playbook --inventory-file="$INVENTORY" "$PLAYBOOK" --private-key="$PRIVATE_KEY"

0 commit comments

Comments
 (0)