-
Notifications
You must be signed in to change notification settings - Fork 0
147 lines (131 loc) · 4.26 KB
/
deploy_network.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
name: Deploy with Ansible
on:
push:
branches:
- main
env:
ANSIBLE_USER: ${{ secrets.ANSIBLE_USER }}
ANSIBLE_PASSWORD: ${{ secrets.ANSIBLE_PASSWORD }}
GF_SECURITY_ADMIN_USER: ${{ secrets.GF_SECURITY_ADMIN_USER }}
GF_SECURITY_ADMIN_PASSWORD: ${{ secrets.GF_SECURITY_ADMIN_PASSWORD }}
permissions:
contents: read
jobs:
#Job Build
build:
name: Build
runs-on: self-hosted
environment: production
if: contains(github.event.head_commit.message, 'deploy')
steps:
#Checkout the code
- name: Checkout
uses: actions/checkout@v3
# Install Ansible
- name: Install Ansible
run: |
sudo apt update
sudo apt install -y ansible
# Install python and modules
- name: Install Modules
run: |
sudo apt install python3 python3-pip -y
pip3 install paramiko --no-input
sudo pip3 install ansible-lint
sudo pip3 install --upgrade ansible
sudo pip3 install --upgrade ansible-core
#Job monitor
monitoring:
name: Monitor
runs-on: self-hosted
environment: production
if: contains(github.event.head_commit.message, 'monitor')
steps:
#Checkout the code
- name: Checkout
uses: actions/checkout@v3
# Install Docker
- name: Install Docker
run: |
sudo apt update
sudo apt install apt-transport-https ca-certificates curl software-properties-common -y
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"
sudo apt install docker-ce -y
sudo usermod -aG docker ${USER}
# Set Grafana secrets in .grafana.secret
- name: Set Grafana secrets in .grafana.secret
run: |
cd Monitoring
echo "GF_SECURITY_ADMIN_USER=${{ secrets.GF_SECURITY_ADMIN_USER }}" > .grafana.secret
echo "GF_SECURITY_ADMIN_PASSWORD=${{ secrets.GF_SECURITY_ADMIN_PASSWORD }}" >> .grafana.secret
# Install Monitoring Server
- name: Install Monitoring Server
run: |
cd Monitoring
sudo docker compose up -d
sudo docker compose start
cd snmp-exporter
sudo docker run --rm -v $(pwd)/mibs:/opt/mibs -v $(pwd)/generator.yml:/opt/generator.yml -v $(pwd):/opt prom/snmp-generator:main generate
cd ..
sudo docker compose restart
#Job Lint
lint:
name: Lint
runs-on: self-hosted
needs: build
environment: production
steps:
#Checkout the code
- name: Checkout
uses: actions/checkout@v3
# Run ansible-lint
- name: Run ansible-lint
run: |
cd Network
ansible-lint --fix playbook_all.yml
ansible-lint --fix playbook_ISP.yml
ansible-lint --fix playbook_CORE.yml
ansible-lint --fix playbook_DIST.yml
ansible-lint --fix playbook_ACC.yml
#Job Test
test:
name: Test
runs-on: self-hosted
needs: lint
environment: production
steps:
#Checkout the code
- name: Checkout
uses: actions/checkout@v3
# Job Deploy
deploy:
name: Deploy
runs-on: self-hosted
needs: test
steps:
# Checkout code from repository
- name: Checkout repository
uses: actions/checkout@v3
# Get commit message
- name: Get Commit Message
id: commit_message
run: |
echo "COMMIT_MSG=$(git log -1 --pretty=format:'%s')" >> $GITHUB_ENV
# Run Ansible Playbook based on commit message
- name: Run Ansible Playbook
run: |
cd Network
if [[ "${{ env.COMMIT_MSG }}" == *"all devices"* ]]; then
ansible-playbook -i inventory playbook_all.yml
elif [[ "${{ env.COMMIT_MSG }}" == *"ISP"* ]]; then
ansible-playbook -i inventory playbook_ISP.yml
elif [[ "${{ env.COMMIT_MSG }}" == *"CORE"* ]]; then
ansible-playbook -i inventory playbook_CORE.yml
elif [[ "${{ env.COMMIT_MSG }}" == *"DIST"* ]]; then
ansible-playbook -i inventory playbook_DIST.yml
elif [[ "${{ env.COMMIT_MSG }}" == *"ACC"* ]]; then
ansible-playbook -i inventory playbook_ACC.yml
else
echo "No matching playbook found for commit message: ${{ env.COMMIT_MSG }}"
fi