# Self Hosted

First you need to obtain token allows you to access Adapptio repository. For this, please, contact us.

### Building image with GitLab CI/CD pipeline

#### Create new repositry

Once you have created a new repository, you also need to add several variables to your pipeline. Go to the CI/CD Gitlab settings (Settings -> CI/CD -> Variables) and add:

1. ARTIFACTS\_LOGIN = \<your-app-id>
2. ARTIFACTS\_TOKEN = \<obtained-token>

#### Dockerfile

Add to your repository this dockerfile.

{% code title="prod.Dockerfile" lineNumbers="true" %}

```docker
ARG BASE_TAG

FROM ${BASE_IMAGE}:${BASE_TAG} as runtime

ARG BLUEPRINTS_DIR

# add blueprints
RUN rm -rf ./repo
ADD ${BLUEPRINTS_DIR} ./repo

```

{% endcode %}

#### CI/CD pipeline

And finaly add the CI/CD files and fill all the variables in the `.param.yaml` and `.gitlab-ci.yaml` file (section between lines 16 and 24).

{% code title=".params.yaml" lineNumbers="true" %}

```yaml
variables:
  # Change this to update Adapptio runtime image
  # Example "v9597de1b-master"
  BASE_TAG: "<adapptio-image-version>"
  # Change this to update application version
  # Example: "v3"
  BLUEPRINT_TAG: "<application-version>"
```

{% endcode %}

{% code title=".gitlab-ci.yml" lineNumbers="true" %}

```yaml
 include: ".params.yaml"
 
 services:
   - docker:20.10.2-dind

stages:
  - download-blueprints
  - build-image

variables:
  IMAGE_NAME: ${CI_REGISTRY}/${CI_PROJECT_NAMESPACE}/${CI_PROJECT_NAME}
  IMAGE_TAG: v${CI_COMMIT_SHORT_SHA}-${CI_COMMIT_REF_SLUG}
  BASE_IMAGE: "artifacts.adapptio.app:5000/adapptio/hae/app"
  REPO_BASE_URL: "https://artifacts.adapptio.app:5001/artifacts"
  
  # ---
  # SETUP THIS VARIABLES
  # Set UUID of your organization
  # Example: "b9fbdcb2-fe44-45a6-836e-585715b3457a"
  REPO_ORG_ID: "<your-org-id>"
  # Set UUID of your application
  # Example: "83110cc7-ea98-4a8f-a2ef-68f9d6fe1b27"
  REPO_APP_ID: "<your-app-id>"
  # ---
  
download-blueprints:
  stage: download-blueprints
  image:
    name: ubuntu:latest
  script:
    - apt-get update && apt-get -y install wget
    - |
      wget \
        --user="${ARTIFACTS_LOGIN}" \
        --password="${ARTIFACTS_TOKEN}" \
        -O ./blueprints.tar.gz \
        ${REPO_BASE_URL}/${REPO_ORG_ID}/${REPO_APP_ID}/${BLUEPRINT_TAG}.tar.gz
    - tar -xf ./blueprints.tar.gz
  artifacts:
    paths:
      - ./${REPO_APP_ID}
    expire_in: 1 hour

build-image:
  stage: build-image
  image:
    name: gcr.io/kaniko-project/executor:v1.9.0-debug
    entrypoint: [""]
  before_script:
    - echo "Building ${IMAGE_NAME}:${IMAGE_TAG}"
    - echo -n ${IMAGE_TAG} > version
    - mkdir -p /kaniko/.docker
    - |
      echo "{ \"auths\": { \"$CI_REGISTRY\": { \"username\": \"${CI_REGISTRY_USER}\", \"password\": \"${CI_REGISTRY_PASSWORD}\" }, \"artifacts.adapptio.app:5000\": { \"username\": \"${ARTIFACTS_LOGIN}\", \"password\": \"${ARTIFACTS_TOKEN}\" }} }" > /kaniko/.docker/config.json
  script:
    - |
      /kaniko/executor  --context ${CI_PROJECT_DIR} \
                        --dockerfile ${CI_PROJECT_DIR}/prod.Dockerfile \
                        --destination ${IMAGE_NAME}:${IMAGE_TAG} \
                        --build-arg BASE_IMAGE="${BASE_IMAGE}" \
                        --build-arg BASE_TAG="${BASE_TAG}" \
                        --build-arg BLUEPRINTS_DIR="./${REPO_APP_ID}"
```

{% endcode %}

### Testing deployment using Docker compose

Create docker compose file in a workspace on your computer.

{% code title="docker-compose.yaml" lineNumbers="true" %}

```yaml
services:
  adapptio:
    image: "registry.gitlab.com/<your-gitlab-project>:<app-image-version>"
    ports:
      - "8080:8080"
    environment:
      - NODE_ENV=production
      - APP_MANIFEST_PATH=/app/hae-bld-app/repo
      - APP_ID=<your-application-id>
      - APP_ENV_ID=<your-application-environment-id>
      - APP_NAME=<application-name>
      - APP_URL=localhost:8080
      - APP_URL_PROTOCOL=<http|https>
      - SESSION_SECRET=<session-secret>
      - ...<YOUR_OWN_ENV_VARS>
```

{% endcode %}

Run docker and type:

```
cd <your-workspace-folder>
docker-compose up
```

The application should be available at URL:

```
https://localhost:8080
```
