# Lab 13
Albert Akmukhametov
a.akmukhametov@innopolis.university
## Question 1
### Create and obtain public key
1. `ssh-keygen` --- I will not execute because I already have keys which are shared along my other machines
2. `cat ~/.ssh/id_rsa.pub` --- print public key

### Add key to GitLab
1. Open Preferences

2. Open SSH keys section

3. Put key into textbox and prpress add key

## Question 2
**Squash commits** means take all changes among commits and combine them into one complex commit. Usually this action is performed during so called "interactive rebase".
## Question 3
Generally, merge is about merging, rebase about rebasing (lol).
Merge of branch A into branch B means creation of new merge commit with changes of A on branch on B with connection to original branch A.
On the other hand, rebase is about reconstruction of commit chain. You are able to put all you changes over other branch, squash changes, mutate their order.
## Question 4
### Create repo, init repo, add remote and fetch existing data



### Make 3 commits
0. Create branch

1.

2.

3.

### Remove first commit
Since terms in assignment are not clear, I'll asume that "remove" means total annihilation from history

Need to remove commit with messave "commitA" (23f1971). I'll use interactive rebase for that.
```
git rebase -i 23f1971~
```
or
```
git rebase -i HEAD~3
```
Before:

After:

Resulting history:

File A really disappeared, as expected:

### Create a commit into main

### Rebase testbranch against main

### Merge tesbranch into main

## Question 5
### Prepare Hosting
I choose Heroku Cloud with free runner (which will be disabled in Monday :D)

### Prepare Docker Hub

### Prepare GitLab CI/CD variables
Settings -> CI/CD -> Variables

- `DOCKER_HUB_TOKEN` --- access token created at hub.docker.com
- `HEROKU_TOKEN` --- access token created at heroku.com
### Project
I took hello-world application from python Flast hello-world
#### `main.py`
```python
from flask import Flask
import os
app = Flask(__name__)
@app.route('/')
def index():
return 'Some text'
app.run(host='0.0.0.0', port=int(os.environ.get("PORT", 60000)))
```
#### `test.py`
```python
def test_something():
assert 5 == 5
```
#### `requirements.txt`
```
flask
pytest
```
#### `Dockerfile`
```dockerfile
FROM python:3.11.0-alpine3.15
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY main.py .
COPY test.py .
CMD python main.py
```
#### `.gitlab-ci.yml`
```
image: docker:latest
services:
- docker:dind
stages:
- build
- test
- deploy
build-image:
stage: build
script:
- docker build . -t app
- mkdir image
- docker save app > image/app.tar
artifacts:
paths:
- image
test:
stage: test
script:
- docker load -i image/app.tar
- docker run --name app -d app
- docker exec app python -m pytest test.py --junitxml=/tmp/report.xml
- docker cp app:/tmp/report.xml ./report.xml
- cat ./report.xml
artifacts:
when: always
reports:
junit: report.xml
expire_in: 2 weeks
deploy-dockerhub:
stage: deploy
script:
- docker load -i image/app.tar
- docker login -u kinjalik -p "$DOCKER_HUB_TOKEN"
- docker tag app kinjalik/sna-fucking-lab:latest
- docker push kinjalik/sna-fucking-lab:latest
deploy-heroku:
stage: deploy
script:
- apk add --no-cache curl
- docker load -i image/app.tar
- echo $(docker inspect app --format={{.Id}}) > imageid.txt
- docker tag app registry.heroku.com/sna-fucking-lab/web
- docker login -u _ -p "$HEROKU_TOKEN" registry.heroku.com
- docker push registry.heroku.com/sna-fucking-lab/web
- echo "Docker Image ID is $(cat imageid.txt)"
- |-
curl -X PATCH https://api.heroku.com/apps/sna-fucking-lab/formation --header "Content-Type: application/json" --header "Accept: application/vnd.heroku+json; version=3.docker-releases" --header "Authorization: Bearer ${HEROKU_TOKEN}" --data '{ "updates": [ { "type": "web", "docker_image": "'$(cat imageid.txt)'" } ] }'
- history
```
## Pipeline results

Pipeline results:
- `build-image`: https://cdn.artifacts.gitlab-static.net/f7/8a/f78a86b5f3f6a8bd1db4d216d9225501c09414b0e4a1dfebbb26ac3da2b4aa39/2022_11_27/3383947880/3694886239/job.log?response-content-type=text%2Fplain%3B%20charset%3Dutf-8&response-content-disposition=inline&Expires=1669509020&KeyName=gprd-artifacts-cdn&Signature=qHAV7ZuETFbmJrch6z-myc_NXM4=

- `test`: https://cdn.artifacts.gitlab-static.net/f7/8a/f78a86b5f3f6a8bd1db4d216d9225501c09414b0e4a1dfebbb26ac3da2b4aa39/2022_11_27/3383947881/3694887098/job.log?response-content-type=text%2Fplain%3B%20charset%3Dutf-8&response-content-disposition=inline&Expires=1669509057&KeyName=gprd-artifacts-cdn&Signature=677mqFFNm3kR0OLZTjlUEbK1f-0=


- `deploy-dockerhub`: https://cdn.artifacts.gitlab-static.net/f7/8a/f78a86b5f3f6a8bd1db4d216d9225501c09414b0e4a1dfebbb26ac3da2b4aa39/2022_11_27/3383947882/3694888259/job.log?response-content-type=text%2Fplain%3B%20charset%3Dutf-8&response-content-disposition=inline&Expires=1669509177&KeyName=gprd-artifacts-cdn&Signature=I0nQDXT23_CG8Mf0sMi97mqJ0OU=


- `deploy-heroku`: https://cdn.artifacts.gitlab-static.net/f7/8a/f78a86b5f3f6a8bd1db4d216d9225501c09414b0e4a1dfebbb26ac3da2b4aa39/2022_11_27/3383947883/3694888486/job.log?response-content-type=text%2Fplain%3B%20charset%3Dutf-8&response-content-disposition=inline&Expires=1669509229&KeyName=gprd-artifacts-cdn&Signature=_j0JGmV8mhUkA7-yLj1p_4YsQY4=


## Resulting App
https://sna-fucking-lab.herokuapp.com
Mostly probable this link will not work since Heroku's free hosting ends at 28th November. I used it because no any other alternative were provided by assignment statement.
