Skip to content

Automate migrations via API

Before we do automated migrations via the API, you need to prepare your AAGM and set git as the single point of truth for your API.

Work with git

Jenkins

Set up Jenkins

Copy this docker-compose.yml, adjust the ports and boot up your Jenkins!

https://www.jenkins.io/doc/book/installing/docker/

https://hub.docker.com/r/jenkins/jenkins

version: '3.6'
services:
  jenkins:
    image: jenkins/jenkins:lts
    extra_hosts:
      - "host.docker.internal:host-gateway"
    privileged: true
    user: root
    container_name: jenkins.apiida
    ports:
      - 50000:50000
      - 8033:8080
    volumes:
      - jenkins.apiida:/var/jenkins_home
      - ./secrets:/var/jenkins_home/secrets/
volumes:
  jenkins.apiida:

The initial password of the admin can be found either in the logs of the container or in the mounted folder secrets.

Needed Plugins

For our project we still need a few plugins.

http://localhost:8033/pluginManager/

grafik-20220324-102043.png

https://plugins.jenkins.io/remote-file/

https://plugins.jenkins.io/pipeline-utility-steps/

https://plugins.jenkins.io/http_request/

Connect to GitLab

http://localhost:8033/credentials/

Add your GitLab credentials as global Credentials

grafik-20220324-102020.png

Setup Pipeline

Create a Multibranch Pipeline

grafik-20220324-102205.png

Insert the Repository URL of ur API at “Branch Sources” and select your credentials.

grafik-20230913-125328.png

If you want to have your Jenkinsfile in another repository, select "by Remote Jenkinsfile Provider Plugin" in "Build Configuration" instead of "by Jenkinsfile", then git in "Jenkinsfile SCM" and enter the repository URL of your Jenkinsfile there.

In "Scan Multibranch Pipeline Triggers", set the intervals at which Jenkins should check for updates in your repository.

Jenkinsfile

pipeline {

    agent any

    stages {

        stage("is AAGM alive?") {

            steps {
                script {
                    echo '------------------------------------------------'
                    echo '---------------  is AAGM alive?  ---------------'
                    echo '------------------------------------------------'

                    def response = httpRequest "http://host.docker.internal:8090/api/v1/alive"
                    println('Response: '+response.content)
                }
            }
        }

        stage("is AAGM ready?") {

            steps {
                script {
                    echo '------------------------------------------------'
                    echo '---------------  is AAGM ready?  ---------------'
                    echo '------------------------------------------------'

                    def response = httpRequest "http://host.docker.internal:8090/api/v1/ready"
                    println('Response: '+response.content)
                }
            }
        }

        stage("Login and Deploy") {

            steps {
                script {
                    echo '------------------------------------------'
                    echo '---------------  Login...  ---------------'
                    echo '------------------------------------------'

                    def bodyLogin = '{"user": "mirco.hoffmann@apiida.com","pass": "myPassword"}'
                    def response = httpRequest contentType: 'APPLICATION_JSON', httpMode: 'POST', requestBody: "${bodyLogin}" , url: "http://host.docker.internal:8090/api/v1/login"
                    println('Response Content: '+response.content)

                    echo '-------------------------------------------'
                    echo '---------------  Deploy...  ---------------'
                    echo '-------------------------------------------'

                    def jsonObj = readJSON text: response.content
                    echo 'Token: '+jsonObj.token 

                    def body = '{"source": {"type": "git"},"targets": ["myTargetNode"],"comment": "Test Migration over API"}'

                    def migResponse = httpRequest customHeaders:[[name:'X-Token', value:"${jsonObj.token}"]] ,contentType: 'APPLICATION_JSON', httpMode: 'GET', requestBody: "${body}" , url: "http://host.docker.internal:8090/api/v1/apis/myApiId/migrate"
                    println('Response: '+migResponse.content)
                }
            }
        }
    }

}

In line 54 you have to define the targets of the migration.

In line 56 you have to insert the id of the API you want to migrate.

That's it! Push something to git! And try it out!