diff --git a/jenkins/resources/org/ops/docker-compose-java.yaml b/jenkins/resources/org/ops/docker-compose-java.yaml new file mode 100644 index 00000000..a8ba1f35 --- /dev/null +++ b/jenkins/resources/org/ops/docker-compose-java.yaml @@ -0,0 +1,25 @@ +version: "3.9" + +x-traefik-label: &traefik-label + traefik.enable: true + traefik.http.routers.${param_release_name}.service: ${param_release_name} + traefik.http.services.${param_release_name}.loadbalancer.server.port: 8080 + +services: + app: + image: ${param_registry_repo}/library/${param_release_name}:${param_release_version} + pull_policy: always + container_name: ${param_release_name} + labels: + <<: *traefik-label + expose: + - "8080" + volumes: + - ${param_docker_data}/${param_release_name}/data/:/app/data/ + - ${param_docker_data}/${param_release_name}/log/:/app/log/ + restart: always + +networks: + default: + name: local + external: true \ No newline at end of file diff --git a/jenkins/resources/org/ops/docker-compose-web.yaml b/jenkins/resources/org/ops/docker-compose-web.yaml new file mode 100644 index 00000000..7c05dc11 --- /dev/null +++ b/jenkins/resources/org/ops/docker-compose-web.yaml @@ -0,0 +1,22 @@ +version: "3.9" + +x-traefik-label: &traefik-label + traefik.enable: true + traefik.http.routers.${param_release_name}.service: ${param_release_name} + traefik.http.services.${param_release_name}.loadbalancer.server.port: 80 + +services: + app: + image: ${param_registry_repo}/library/${param_release_name}:${param_release_version} + pull_policy: always + container_name: ${param_release_name} + labels: + <<: *traefik-label + expose: + - "80" + restart: always + +networks: + default: + name: local + external: true \ No newline at end of file diff --git a/jenkins/src/org/ops/Builder.groovy b/jenkins/src/org/ops/Builder.groovy index 3ff29cfc..2ccffac0 100644 --- a/jenkins/src/org/ops/Builder.groovy +++ b/jenkins/src/org/ops/Builder.groovy @@ -56,18 +56,18 @@ def build() { }, go : { pathMap.get("go").call() - cmd = StringUtils.format("cd {0};go build -o build main.go;", env.param_project_context) + def cmd = StringUtils.format("cd {0};go build -o build main.go;", env.param_project_context) sh "${cmd}" }, web : { pathMap.get("node").call() - cmd = StringUtils.format("npm_config_registry={1} npm_config_node_linker=hoisted pnpm --dir {0} install && pnpm --dir {0} run build", env.param_project_context, env.param_npm_repo) + def cmd = StringUtils.format("npm_config_registry={1} npm_config_node_linker=hoisted pnpm --dir {0} install && pnpm --dir {0} run build", env.param_project_context, env.param_npm_repo) sh "${cmd}" }, dotnet: { pathMap.get("dotnet").call() configFileProvider([configFile(fileId: "nuget.config", variable: 'CONFIG_FILE_NUGET')]) { - cmd = StringUtils.format( + def cmd = StringUtils.format( "rm -rf {0}/build && dotnet publish --configfile {1} -c Release {0} -o {0}/build", env.param_project_root, "${CONFIG_FILE_NUGET}" @@ -76,7 +76,7 @@ def build() { } }, shell : { - cmd = StringUtils.format("chmod +x {0};{0}", PathUtils.ofPath(env.param_project_root, env.param_project_shell_file)) + def cmd = StringUtils.format("chmod +x {0};{0}", PathUtils.ofPath(env.param_project_root, env.param_project_shell_file)) sh "${cmd}" } ] diff --git a/jenkins/src/org/ops/Docker.groovy b/jenkins/src/org/ops/Docker.groovy index 031ddfb8..8ee4a7a8 100644 --- a/jenkins/src/org/ops/Docker.groovy +++ b/jenkins/src/org/ops/Docker.groovy @@ -7,43 +7,38 @@ import org.ops.util.StringUtils def build() { configFileProvider([configFile(fileId: "docker.config", targetLocation: '.jenkins/docker/config.json')]) {} - def docker_file = "${env.param_docker_file}" + def docker_file = env.param_docker_file if (!fileExists(docker_file)) { docker_file = ".jenkins/Dockerfile-${env.param_code_type}" - def docker_file_content = libraryResource "org/ops/Dockerfile-${env.param_code_type}" - writeFile file: docker_file, text: docker_file_content, encoding: 'utf-8' + def content = libraryResource "org/ops/Dockerfile-${env.param_code_type}" + writeFile file: docker_file, text: content, encoding: 'utf-8' } - docker_config_dir = PathUtils.ofPath("${env.WORKSPACE}", ".jenkins/docker/") - docker_image_tag = "${env.param_release_image}:${env.param_release_version}" - docker_build_cmd = "docker build --tag ${docker_image_tag} --file ${docker_file} ${env.param_docker_context} --pull" + def docker_config_dir = PathUtils.ofPath("${env.WORKSPACE}", ".jenkins/docker/") + def docker_image_tag = "${env.param_release_image}:${env.param_release_version}" + def docker_build_cmd = "docker build --tag ${docker_image_tag} --file ${docker_file} ${env.param_docker_context} --pull" if (StringUtils.isNotEmpty(env.param_docker_build_args)) { env.param_docker_build_args.split(",").each { t -> docker_build_cmd += " --build-arg $t" } } - docker_push_cmd = "docker --config ${docker_config_dir} push ${docker_image_tag}" - docker_rmi_cmd = "docker rmi ${docker_image_tag}" + def docker_push_cmd = "docker --config ${docker_config_dir} push ${docker_image_tag}" + def docker_rmi_cmd = "docker rmi ${docker_image_tag}" sh "${docker_build_cmd} && ${docker_push_cmd} && ${docker_rmi_cmd}" } def deploy() { - deployMap = [ - java: { - configFileProvider([configFile(fileId: "docker-compose-java-v1.yaml", targetLocation: '.jenkins/docker-compose.yaml')]) {} - }, - web : { - configFileProvider([configFile(fileId: "docker-compose-web-v1.yaml", targetLocation: '.jenkins/docker-compose.yaml')]) {} - } - ] - if (fileExists("${env.param_docker_compose_file}")) { - sh "cp ${env.param_docker_compose_file} .jenkins/docker-compose.yaml" - } else { - deployMap.get(env.param_code_type).call() + + def compose_file = env.param_docker_compose_file + + if (!fileExists(compose_file)) { + compose_file = ".jenkins/docker-compose-${env.param_code_type}.yaml" + def content = libraryResource "org/ops/docker-compose-${env.param_code_type}.yaml" + writeFile file: compose_file, text: content, encoding: 'utf-8' } + withCredentials([dockerCert(credentialsId: 'docker-client', variable: 'DOCKER_CERT_PATH')]) { - param_file = PathUtils.ofPath("${env.WORKSPACE}", ".jenkins/param.yaml") - docker_compose_file = PathUtils.ofPath("${env.WORKSPACE}", ".jenkins/docker-compose.yaml") - cmd = "DOCKER_TLS_VERIFY=1 DOCKER_HOST=tcp://${env.param_docker_deploy_host}:2376 docker-compose --project-name ${env.param_release_name} --file ${docker_compose_file} --env-file ${param_file} up --detach --remove-orphans" + def param_file = PathUtils.ofPath("${env.WORKSPACE}", ".jenkins/param.yaml") + cmd = "DOCKER_TLS_VERIFY=1 DOCKER_HOST=tcp://${env.param_docker_deploy_host}:2376 docker-compose --project-name ${env.param_release_name} --file ${compose_file} --env-file ${param_file} up --detach --remove-orphans" sh "${cmd}" } }