Jenkinsfileを書く

# Overview

- Jenkins Pipelineというのを選択肢して作成する -> [Pipeline Syntax](https://www.jenkins.io/doc/book/pipeline/syntax/)
- Qiitaでは [【Jenkins】declarative pipeline サンプル集](https://qiita.com/ys1230415/items/51b36fedf1434e877765) の記事が全体像掴むのにはとても役立つ
- Gitlab連携には https://github.com/jenkinsci/gitlab-plugin

# 基本
Pipelineでは、 `build`, `test`, `deploy` などを一連の流れとして、`Jenkinsfile`を使って定義できる


# Gitlab連携

## Credentials

GlobalのCredentialsにTokenを設定して`gitLabConnection('gitlab')` をoptionsに入れて呼び出す (ここでは、 `gitlab` という名前でcredentialsを保存してある)

## Skipped statusがない

- https://updates.jenkins.io/current/update-center.json の中では1.5.13となっている
- 1.5.13では https://github.com/jenkinsci/gitlab-plugin/blob/gitlab-plugin-1.5.13/src/main/java/com/dabsquared/gitlabjenkins/gitlab/api/model/BuildState.java#L7 に skippedがないので、Jenkinsfileで `skipped` をgitlabに伝えようとしても落ちる

- 1.5.16で戻ってるのでVersionを挙げないといけない
https://github.com/jenkinsci/gitlab-plugin/pull/1015/files#diff-2bda40a19e289b692cd855bf109115a4

- Jenkins manage pluginでは最新ぽく出る


# Jenkinsfile

```groovy
pipeline {

    agent any

    options {
        gitLabConnection('gitlab')
        gitlabBuilds(builds: ['build', 'test', 'deploy'])
    }

    triggers {
        gitlab(
                triggerOnPush: true,
                triggerOnMergeRequest: true,
                branchFilterType: 'All',
                addNoteOnMergeRequest: true,
                addCiMessage: true
        )
    }

    environment {
        PASSWORD = credentials('TEST_PASSWORD')
    }

    stages {
        stage('test') {
            agent {
                docker {
                    image 'maven:3.6'
                    args '-v $HOME/.m2:/root/.m2'
                }
            }
            steps {
                sh "mvn test"
            }
            post {
                success {
                    updateGitlabCommitStatus name: 'format', state: 'success'
                }
                failure {
                    updateGitlabCommitStatus name: 'format', state: 'failed'
                }
            }
        }
        stage('build') {
            when {
                branch 'develop'
            }
            steps {
                sh "docker build -t ${env.DOCKER_REGISTRY}:${env.DOCKER_TAG} -f ${WORKSPACE}/docker/Dockerfile ${WORKSPACE}"
                sh "docker push ${env.DOCKER_REGISTRY}:${env.DOCKER_TAG}"
            }
        }
        stage('deploy') {
            when {
                branch 'develop'
            }
            steps {
                withCredentials([file(credentialsId: '1111222233334444', variable: 'env')]) {
                    dir('docker') {
                        sh "set +x"
                        sh "cp ${env} ${WORKSPACE}/docker/.env"
                        sh "docker stack deploy --compose-file ${WORKSPACE}/docker/docker-compose.yml"
                    }
                }
            }
        }
    }
    post {
        success {
            updateGitlabCommitStatus name: 'deploy', state: 'success'
            updateGitlabCommitStatus name: 'build', state: 'success'
        }
        failure {
            updateGitlabCommitStatus name: 'build', state: 'failed'
            updateGitlabCommitStatus name: 'deploy', state: 'failed'
        }
    }
}

```

# 参考

- DockerをPipelinesで動かす: https://www.jenkins.io/doc/book/pipeline/docker/
- https://medium.com/@teeks99/continuous-integration-with-jenkins-and-gitlab-fa770c62e88a
- http://knowledge.intellil.ink/open.knowledge/view/86
- https://stackoverflow.com/questions/54890974/why-isnt-jenkins-update-center-showing-latest-plugin-versions