Skip to content
61 changes: 61 additions & 0 deletions CICD/8.2-hw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Дополнительные материалы для выполнения домашнего задания "8.2. Что такое DevOps. СI/СD"

### Nexus, запуск образа в виртуальной машине Vagrant

Запуск:
```bash
docker run -d -p 192.168.56.10:8081:8081 -p 192.168.56.10:8082:8082 --name nexus -e INSTALL4J_ADD_VM_PARAMS="-Xms512m -Xmx512m -XX:MaxDirectMemorySize=273m" sonatype/nexus3
```

Вывести пароль администратора для первого логина в Nexus:
```
docker exec -t nexus bash -c 'cat /nexus-data/admin.password && echo'
```

### Jenkins

Jenkins url: http://192.168.56.10:8080/ (ссылка при установке Jenkins в ВМ Vagrant)
Repo для проекта в Jenkins: https://github.com/netology-code/sdvps-materials.git

### Install go (для установки руками)

https://go.dev/doc/install

```
wget https://go.dev/dl/go1.17.5.linux-amd64.tar.gz
rm -rf /usr/local/go && tar -C /usr/local -xzf go1.17.5.linux-amd64.tar.gz
echo 'export PATH=$PATH:/usr/local/go/bin' >> /etc/profile
```

### Freestyle job

1. `/usr/local/go/bin/go test .`
2. `docker build . -t ubuntu-bionic:8082/hello-world:v$BUILD_NUMBER`
3. `docker login ubuntu-bionic:8082 -u admin -p admin && docker push ubuntu-bionic:8082/hello-world:v$BUILD_NUMBER && docker logout`

### Pipeline

```
pipeline {
agent any
stages {
stage('Git') {
steps {git 'https://github.com/netology-code/sdvps-materials.git'}
}
stage('Test') {
steps {
sh 'go test .'
}
}
stage('Build') {
steps {
sh 'docker build . -t ubuntu-bionic:8082/hello-world:v$BUILD_NUMBER'
}
}
stage('Push') {
steps {
sh 'docker login ubuntu-bionic:8082 -u admin -p admin && docker push ubuntu-bionic:8082/hello-world:v$BUILD_NUMBER && docker logout' }
}
}
}
```
60 changes: 3 additions & 57 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,60 +1,6 @@
# Подсказки для выполнения ДЗ
# Nexus, запуск образа в виртуальной машине Vagrant
## Дополнительные материалы для выполнения домашних заданий из блока "Введение в DevOps"

Запуск:
```bash
docker run -d -p 192.168.56.10:8081:8081 -p 192.168.56.10:8082:8082 --name nexus -e INSTALL4J_ADD_VM_PARAMS="-Xms512m -Xmx512m -XX:MaxDirectMemorySize=273m" sonatype/nexus3
```

Вывести пароль администратора для первого логина в Nexus:
```
docker exec -t nexus bash -c 'cat /nexus-data/admin.password && echo'
```
- [Дополнительный материал для занятия "8.2. Что такое DevOps. СI/СD"](CICD/8.2-hw.md)

# Jenkins

Jenkins url: http://192.168.56.10:8080/ (ссылка при установке Jenkins в ВМ Vagrant)
Repo для проекта в Jenkins: https://github.com/netology-code/sdvps-materials.git

## Install go (для установки руками)

https://go.dev/doc/install

```
wget https://go.dev/dl/go1.17.5.linux-amd64.tar.gz
rm -rf /usr/local/go && tar -C /usr/local -xzf go1.17.5.linux-amd64.tar.gz
echo 'export PATH=$PATH:/usr/local/go/bin' >> /etc/profile
```

## Freestyle job

1. `/usr/local/go/bin/go test .`
2. `docker build . -t ubuntu-bionic:8082/hello-world:v$BUILD_NUMBER`
3. `docker login ubuntu-bionic:8082 -u admin -p admin && docker push ubuntu-bionic:8082/hello-world:v$BUILD_NUMBER && docker logout`

## Pipeline

```
pipeline {
agent any
stages {
stage('Git') {
steps {git 'https://github.com/kozl/netology-devops.git'}
}
stage('Test') {
steps {
sh 'go test .'
}
}
stage('Build') {
steps {
sh 'docker build . -t ubuntu-bionic:8082/hello-world:v$BUILD_NUMBER'
}
}
stage('Push') {
steps {
sh 'docker login ubuntu-bionic:8082 -u admin -p admin && docker push ubuntu-bionic:8082/hello-world:v$BUILD_NUMBER && docker logout' }
}
}
}
```
- [Дополнительный материал для занятия "8.3. GitLab"](https://github.com/netology-code/sdvps-materials/tree/main/gitlab)
163 changes: 163 additions & 0 deletions gitlab/GITLAB.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
# Gitlab

## Развернуть инсталляцию

1. Добавить запись в локальный /etc/hosts:

```bash
echo '192.168.56.10 gitlab.localdomain gitlab' >> /etc/hosts
```

2. Запустить инсталляцию:

```bash
VAGRANT_EXPERIMENTAL="disks" vagrant up
```
Переменная окружения VAGRANT_EXPERIMENTAL нужна для того, чтобы vagrant настроил виртуалку с нестандартным размером диска.

После успешного завершения Gitlab будет доступен по адресу http://gitlab.localdomain

Получить первичный пароль для пользователя root:

```bash
vagrant ssh -- sudo cat /etc/gitlab/initial_root_password
```

### Возможные проблемы

1. В Vagrantfile определено, что виртуальная машина, которая будет поднята — получит адрес 192.168.56.10. Возможна ситуация, когда сетевой интерфейс virtualbox, vboxnet0, использует другую подсеть:

```bash
$ ip -br -c a sh vboxnet2
vboxnet2 UP 192.168.100.1/24 fe80::800:27ff:fe00:2/64
```

В этом случае нужно исправить адрес на тот, что будет соответствовать сети интерфейса и добавить запись для него в /etc/hosts

2. Недостаточно ресурсов. Для разворачивания инсталляции потребуется как минимум 6GB памяти, следует учитывать это при запуске. Если ресурсов недостаточно — можно попробовать подправить Vagrantfile, но работоспособность инсталляции при этом не гарантируется.

## Gitlab runner

Регистрация раннера:
```bash
docker run -ti --rm --name gitlab-runner \
--network host \
-v /srv/gitlab-runner/config:/etc/gitlab-runner \
-v /var/run/docker.sock:/var/run/docker.sock \
gitlab/gitlab-runner:latest register
```

Конфигурация раннера для docker-in-docker:
```yaml
volumes = ["/cache", "/var/run/docker.sock:/var/run/docker.sock"]
extra_hosts = ["gitlab.localdomain:192.168.56.10"]
```

Запуск:
```bash
docker run -d --name gitlab-runner --restart always \
--network host \
-v /srv/gitlab-runner/config:/etc/gitlab-runner \
-v /var/run/docker.sock:/var/run/docker.sock \
gitlab/gitlab-runner:latest
```

## Pipeline

```yaml
stages:
- test
- build

test:
stage: test
image: golang:1.17
script:
- go test .

build:
stage: build
image: docker:latest
script:
- docker build .
```

## Sonarqube

```bash
vagrant ssh
cd /vagrant
docker-compose up -d
```

Sonarqube будет доступен по адресу http://gitlab.localdomain:9000

## Pipeline with sonarqube-check

```yaml
stages:
- test
- static-analysis
- build

test:
stage: test
image: golang:1.16
script:
- go test .

static-analysis:
stage: test
image:
name: sonarsource/sonar-scanner-cli
entrypoint: [""]
variables:
script:
- sonar-scanner -Dsonar.projectKey=netology-gitlab -Dsonar.sources=. -Dsonar.host.url=http://gitlab.localdomain:9000 -Dsonar.login=a778675a32f0d9d6455a3d502f4e2838e784994d

build:
stage: build
image: docker:latest
script:
- docker build .
```

## Pipeline with manual run

```yaml
stages:
- test
- build

test:
stage: test
image: golang:1.16
script:
- go test .

sonarqube-check:
stage: test
image:
name: sonarsource/sonar-scanner-cli
entrypoint: [""]
variables:
script:
- sonar-scanner -Dsonar.projectKey=netology-gitlab -Dsonar.sources=. -Dsonar.host.url=http://gitlab.localdomain:9000 -Dsonar.login=a778675a32f0d9d6455a3d502f4e2838e784994d

build:
stage: build
image: docker:latest
only:
- master
script:
- docker build .

build:
stage: build
image: docker:latest
when: manual
except:
- master
script:
- docker build .
```
90 changes: 90 additions & 0 deletions gitlab/Vagrantfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
# The most common configuration options are documented and commented below.
# For a complete reference, please see the online documentation at
# https://docs.vagrantup.com.

# Every Vagrant development environment requires a box. You can search for
# boxes at https://vagrantcloud.com/search.
config.vm.box = "ubuntu/bionic64"

# Disable automatic box update checking. If you disable this, then
# boxes will only be checked for updates when the user runs
# `vagrant box outdated`. This is not recommended.
# config.vm.box_check_update = false

# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
# NOTE: This will enable public access to the opened port
# config.vm.network "forwarded_port", guest: 80, host: 8080

# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine and only allow access
# via 127.0.0.1 to disable public access
# config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"

# Create a private network, which allows host-only access to the machine
# using a specific IP.
config.vm.network "private_network", ip: "192.168.56.10"

# Create a public network, which generally matched to bridged network.
# Bridged networks make the machine appear as another physical device on
# your network.
# config.vm.network "public_network"

config.vm.disk :disk, size: "15GB", primary: true

# Share an additional folder to the guest VM. The first argument is
# the path on the host to the actual folder. The second argument is
# the path on the guest to mount the folder. And the optional third
# argument is a set of non-required options.
# config.vm.synced_folder "../data", "/vagrant_data"

# Provider-specific configuration so you can fine-tune various
# backing providers for Vagrant. These expose provider-specific options.
# Example for VirtualBox:
#
config.vm.provider "virtualbox" do |vb|
# # Display the VirtualBox GUI when booting the machine
# vb.gui = true
#
# # Customize the amount of memory on the VM:
vb.memory = "6144"
end
#
# View the documentation for the provider you are using for more
# information on available options.

# Enable provisioning with a shell script. Additional provisioners such as
# Ansible, Chef, Docker, Puppet and Salt are also available. Please see the
# documentation for more information about their specific syntax and use.
config.vm.provision "shell", inline: <<-SHELL
export DEBIAN_FRONTEND=noninteractive
apt-get update
# install docker & docker-compose
apt-get install -y docker.io docker-compose
# install gitlab: https://about.gitlab.com/install/#ubuntu
apt-get install -y curl openssh-server ca-certificates tzdata perl
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ee/script.deb.sh | sudo bash
sudo EXTERNAL_URL="http://gitlab.localdomain" apt-get install gitlab-ee
# pull some images in advance
docker pull gitlab/gitlab-runner:latest
docker pull sonarsource/sonar-scanner-cli:latest
docker pull golang:1.17
docker pull docker:latest
# set sysctl for sonarqube
sysctl vm.max_map_count=262144
# run sonarqube
# cd /vagrant && docker-compose up -d
# add some records to /etc/hosts
echo -e "192.168.56.10\tubuntu-bionic\tubuntu-bionic" >> /etc/hosts
echo -e "192.168.56.10\tgitlab.localdomain\tgitlab" >> /etc/hosts
SHELL
end
Loading