无法设置JAVA_HOME GitLab CI

时间:2019-04-04 15:11:07

标签: java docker gitlab gitlab-ci gitlab-ci-runner

我正在2个docker上运行gitLab和gitLab Runner。

当我推送代码时,我的管道启动了,但是出现了这个错误:

Running with gitlab-runner 11.9.1 (de08a4bb)
  on bf23c668df26 78rP5HeK
Using Docker executor with image alpine:latest ...
Pulling docker image alpine:latest ...
Using docker image sha256:5cb3aa00f89934411ffba5c063a9bc98ace875d8f92e77d0029543d9f2ef4ad0 for alpine:latest ...
Running on runner-78rP5HeK-project-2-concurrent-0 via bf23c668df26...
Reinitialized existing Git repository in /builds/project/app/.git/
HEAD is now at f5a8fe6 gitlab conf
Fetching changes...
fatal: remote origin already exists.
From http://10.34.18.1/project/app
   f5a8fe6..756284c  feature-develop -> origin/feature-develop
Checking out 756284cc as feature-develop...

Skipping Git submodules setup
Checking cache for feature-develop-1...
No URL provided, cache will not be downloaded from shared cache server. Instead a local version of cache will be extracted. 
Successfully extracted cache
$ export JAVA_HOME="/usr/lib/jvm/jre-1.8.0-openjdk"
$ export MAVEN_USER_HOME=`pwd`/.maven
$ chmod +x mvnw
$ ./mvnw com.github.eirslett:frontend-maven-plugin:install-node-and-npm -DnodeVersion=v10.13.0 -DnpmVersion=6.4.1 -Dmaven.repo.local=$MAVEN_USER_HOME
Error: JAVA_HOME is not defined correctly.
  We cannot execute /usr/lib/jvm/jre-1.8.0-openjdk/bin/java
ERROR: Job failed: exit code 1

我的.gitlab-ci.yml:

cache:
    key: "$CI_COMMIT_REF_NAME"
    paths:
        - .maven/
stages:
    - build
    - test
    - analyze
    - package
    - release
    - deploy


before_script:
    - export JAVA_HOME="/usr/lib/jvm/jre-1.8.0-openjdk"
    - export MAVEN_USER_HOME=`pwd`/.maven
    - chmod +x mvnw
    - ./mvnw com.github.eirslett:frontend-maven-plugin:install-node-and-npm -DnodeVersion=v10.13.0 -DnpmVersion=6.4.1 -Dmaven.repo.local=$MAVEN_USER_HOME
    - ./mvnw com.github.eirslett:frontend-maven-plugin:npm -Dmaven.repo.local=$MAVEN_USER_HOME

maven-compile:
    stage: build
    script:
        - ./mvnw compile -Dmaven.repo.local=$MAVEN_USER_HOME
    artifacts:
        paths:
            - target/classes/
            - target/generated-sources/
        expire_in: 1 day

maven-test:
    stage: test
    script:
        - ./mvnw test -Dmaven.repo.local=$MAVEN_USER_HOME
    artifacts:
        reports:
            junit: target/surefire-reports/TEST-*.xml
        paths:
            - target/test-results
        expire_in: 1 day

frontend-test:
    stage: test
    script:
        - ./mvnw com.github.eirslett:frontend-maven-plugin:npm -Dfrontend.npm.arguments='test -- -u' -Dmaven.repo.local=$MAVEN_USER_HOME
    artifacts:
        reports:
            junit: target/test-results/jest/TESTS-*.xml
        paths:
            - target/test-results
        expire_in: 1 day

maven-package:
    stage: package
    script:
        - ./mvnw verify -Pprod -DskipTests -Dmaven.repo.local=$MAVEN_USER_HOME
    artifacts:
        paths:
            - target/*.war
        expire_in: 1 day

# Uncomment the following line to use gitlabs container registry. You need to adapt the REGISTRY_URL in case you are not using gitlab.com
#docker-push:
#    image: docker:latest
#    services:
#        - docker:dind
#    variables:
#        REGISTRY_URL: registry.gitlab.com
#        DOCKER_HOST: tcp://docker:2375
#        DOCKER_DRIVER: overlay2
#        IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG
#    stage: release
#    dependencies:
#        - maven-package
#    before_script:
#        - docker info
#        - cp target/*.war src/main/docker
#    script:
#        - docker login -u "gitlab-ci-token" -p "$CI_BUILD_TOKEN" $REGISTRY_URL
#        - docker build -f src/main/docker/Dockerfile -t $IMAGE_TAG src/main/docker
#        - docker push $IMAGE_TAG

/usr/lib/jvm/jre-1.8.0-openjdk是服务器上的JAVA_HOME路径,而不是Runner Docker上的路径

1 个答案:

答案 0 :(得分:0)

我已经在我的conf文件中添加了一个码头映像image: maven:3-jdk-8

cache:
    key: "$CI_COMMIT_REF_NAME"
    paths:
        - .maven/

image: maven:3-jdk-8

stages:
    - build
    - test
    - analyze
    - package
    - release
    - deploy


before_script:
    - chmod +x mvnw
    - export MAVEN_USER_HOME=`pwd`/.maven
    - chmod +x mvnw
    - ./mvnw com.github.eirslett:frontend-maven-plugin:install-node-and-npm -DnodeVersion=v10.13.0 -DnpmVersion=6.4.1 -Dmaven.repo.local=$MAVEN_USER_HOME
    - ./mvnw com.github.eirslett:frontend-maven-plugin:npm -Dmaven.repo.local=$MAVEN_USER_HOME
[...]