首页

    gitlab CI中使用codecov

    标签:gitlab-CI-CD,docker

    若是使用shell executor,和travis ci中使用类似

    stages:
      - test
    
    test:
      stage: test
      script:
        - CGO_ENABLED=0 go test -v -coverprofile=cover.txt -covermode=atomic ./...
        - bash <(curl -s https://codecov.io/bash) -t $codecov_token -f cover.txt
      tags:
        - shell
    

    gitlab ci中能使用docker executor就尽量使用,codecov有相应文档

    Codecov Inside Docker

    .gitlab-ci.yml

    image: docker:stable
    
    stages:
      - build
      - test
    
    variables:
      CACHE_IMAGE_URL: theviper/ci-codecov:cache
      BUILD_IMAGE_URL: theviper/ci-codecov:$CI_COMMIT_SHA
    
    ....
    
    test:
      stage: test
      script:
        - docker pull $CACHE_IMAGE_URL || true
        - apk add --no-cache bash curl
        - ci_env=`(curl -s https://codecov.io/env) | bash`
        - docker run $ci_env --env token=$codecov_token --env GO111MODULE=on --network=host $CACHE_IMAGE_URL /bin/bash -c 'go test -v -coverprofile=coverage.txt -covermode=atomic ./... && bash <(curl -s https://codecov.io/bash) -t $token'
      only:
        - merge_requests
        - master
      tags:
        - docker
    

    docker socket bind

    • ci_env不能像文档所说,写成
    ci_env=`bash <(curl -s https://codecov.io/env)`
    

    会报错/bin/sh: eval: line 1: syntax error: unexpected "(",见job,因此需要换一种写法

    ci_env=`(curl -s https://codecov.io/env) | bash`
    
    • 设置ci_env时用到bash,curl,需要在设置前安装
    • docker run需要设置--network=host,否则main_test.go中localhost无效
    • docker run需要加上/bin/bash -c,否则报错,见job
    • gitlab ci设置的Variables,需要通过--env传给CACHE_IMAGE_URL,因为直接
    go test -v -coverprofile=coverage.txt -covermode=atomic ./... && bash <(curl -s https://codecov.io/bash) -t $codecov_token
    

    会报

    HTTP 400
    Please provide the repository token to upload reports via `-t :repository-token`
    

    说明设置的Variables没有传进去

    • -coverprofile输出的文件名必须是coverage.txt.如果是其他文件名,即使codecov加了-f cover.txt也没用,见job

    dind

    这种方式也可以与codecov集成,见job

    Codecov Outside Docker

    .gitlab-ci.yml

    ....
    test:
      stage: test
      script:
        - docker pull $CACHE_IMAGE_URL || true
        - apk add --no-cache bash curl
        - export SHARED_PATH="$(dirname ${CI_PROJECT_DIR})/shared"
        - echo ${SHARED_PATH}
        - mkdir -p ${SHARED_PATH}
        - docker run -v ${SHARED_PATH}:/shared --env GO111MODULE=on --network=host $CACHE_IMAGE_URL /bin/bash -c 'go test -v -coverprofile=coverage.txt -covermode=atomic ./... && mv /go/src/ci-codecov/coverage.txt /shared'
        - find / -name coverage.txt
        - bash -c "bash <(curl -s https://codecov.io/bash) -t $codecov_token -f ${SHARED_PATH}/coverage.txt"
    

    dind

    • docker run需要加上/bin/bash -c,否则报错,见job
    • 不能像上面一样
    bash <(curl -s https://codecov.io/bash) -t $codecov_token
    

    会报错/bin/sh: eval: line 102: syntax error: unexpected "(",见job,需要加上bash -c

    bash -c "bash <(curl -s https://codecov.io/bash) -t $codecov_token"
    
    • 最后一行执行codecov脚本,需要加上-f参数,见job

    docker socket bind

    这种方式也可以与codecov集成,见job