-
Notifications
You must be signed in to change notification settings - Fork 12
/
Jenkinsfile
114 lines (108 loc) · 5.06 KB
/
Jenkinsfile
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
/*
Prerequisites for the host machine:
- Make sure jenkins is an user of docker.
Run `grep /etc/group -e docker` and make sure "jenkins" is in there.
- Give jenkins administrator access. This is necessary as restoring changes from previous deployments and initializing
ssl requires sudo access.
Add `jenkins ALL=(ALL) NOPASSWD: ALL` to `/etc/sudoers`.
- Make sure that the host machine has credentials (autolab.bot) to pull and rebase the latest Autolab repos.
*/
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building..'
sh 'ls -al'
echo "user is: $USER"
sh 'pwd'
// need to restore the schema.db which is changed from the previous deployment
sh 'git config --global user.email "[email protected]"'
sh 'git config --global user.name "jenkinsBot"'
sh "cd Autolab && sudo git stash && cd .. || true "
sh "sudo git submodule update --remote --rebase --force"
// sh 'cd Autolab && sudo chown $USER db/schema.rb && sudo git restore db/schema.rb && cd ..'
sh 'grep /etc/group -e "docker"'
sh 'make clean && make'
// nuke any previous certificates, typically not necessary
// openSSL only allows 5 new certificates for a domain in a week
// sh 'sudo rm -rf /var/lib/jenkins/workspace/autolab-demo-test/ssl/certbot/conf/live/nightly.autolabproject.com*'
sh 'docker stop autolab || true && docker rm autolab || true'
sh 'docker stop tango || true && docker rm tango || true'
sh 'docker stop redis || true && docker rm redis || true'
sh 'docker stop mysql || true && docker rm mysql || true'
sh 'docker stop certbot || true && docker rm certbot || true'
// add google analytics ID to Nightly
sh 'python3 ci_script.py -g ./Autolab/config/environments/production.rb'
sh 'docker compose build'
}
}
stage('Configure SSL') {
steps {
echo 'Configuring SSL...'
sh 'docker compose up -d'
sh 'make set-perms'
sh 'make db-migrate'
// create initial user
sh 'docker exec autolab env RAILS_ENV=production bundle exec rails admin:create_root_user[[email protected],"adminfoobar","Admin","Foo"] || true'
// change the Tango volume path
sh 'python3 ci_script.py -v .env'
sh 'docker compose stop'
// configure SSL
sh "python3 ci_script.py -a nginx/app.conf"
sh 'python3 ci_script.py -s ./ssl/init-letsencrypt.sh'
// do not replace existing certificate
sh "echo 'n' | echo 'N' | sudo bash ./ssl/init-letsencrypt.sh"
}
}
stage('Deploy') {
steps {
echo 'Deploying nightly.autolabproject.com...'
// build autograding images
sh "docker build -t autograding_image Tango/vmms/"
// prune old images
echo "Dangling images:"
sh 'docker images -f "dangling=true"'
echo "Removing dangling images..."
sh 'docker rmi $(docker images -f "dangling=true" -q) || true'
// bring everything up!
sh "docker compose up -d"
}
}
stage('Update Repository Submodules') {
steps {
echo 'Updating Autolab Docker Github submodules...'
sh 'ls -al'
echo "user is: $USER"
sh 'pwd'
sh 'git branch'
sh 'git checkout master'
sh 'git pull --rebase origin master'
sh 'git merge -m "Fast-forward merge"'
sh 'cd Autolab && sudo git add db/schema.rb && sudo git stash && cd ..'
sh 'cd Autolab && sudo git pull origin master && cd ..'
sh 'cd Tango && sudo git pull origin master && cd ..'
sh 'git config --global user.email "[email protected]"'
sh 'git config --global user.name "AutolabJenkinsBot"'
sh 'git add Autolab'
sh 'git add Tango'
// may fail if no actual changes
sh 'git commit -m "Update Autolab and Tango submodules" | true'
sh 'git push origin master | true'
}
}
stage('Update Docs') {
steps {
echo 'Updating Autolab Docs...'
script {
if (env.STARTED_BY_UPSTREAM_BUILD.toBoolean()) {
echo 'Started by upstream build, deploying docs...'
sh 'cd Autolab && sudo mkdocs gh-deploy --no-history'
} else {
echo 'Not started by upstream build, not deploying docs...'
}
}
}
}
}
}