gitlab-ci.yml

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

前幾天做以docker方式來運行runner,現在想好像有點出入。真正應該是通過打包一個docker鏡像,在鏡像裡進行測試,測試通過後提交到鏡像倉庫,接著發布到測試叢集裡進行整合測試,最終發布到生產環境裡。
現在完成了通過打包測試鏡像進行測試,然後再產生生產鏡像。為了縮減生產鏡像的大小(golang鏡像將近700M,golang:alpine鏡像250M,而通過mutil+stage方式打包到Alpine基礎包,只有10M左右。下面有詳細的檔案例子。
在一個項目裡一般只有一個Dockerfile,滿足不了需求,發現可以通過-f Dockefile指定不同的檔案進行打包,但是Dockerfile.test這種帶尾碼方式有問題,應該使用DockerfileTest這種方式。參見Docker build

一、打包Docker鏡像

首先要安裝docker ce,參照Docker安裝
接著需要給gitlab-runner使用者賦予docker執行許可權,否則在打包是會提示無許可權。

sudo usermod -aG docker gitlab-runnersudo -u gitlab-runner -H docker info

很麻煩,怎麼先進行測試,測試通過後再打包?
測試是不是使用一個docker runner來做,測試通過後再使用shell runner來打包上傳發布?

項目根目錄下的DockerfileTest,DockerfileProd,gitlab-ci.yml代碼如下:

# DockerfileTestFROM golang:alpineMAINTAINER panzulong "panzulong@gmail.com"WORKDIR /go/srcCOPY src /go/srccmd ["go","test"]
# DockerfileProd# build product binFROM golang:alpineMAINTAINER panzulong "panzulong@gmail.com"WORKDIR /go/srcCOPY src /go/srcRUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .# build product imageFROM alpine:latestWORKDIR /root/COPY --from=0 /go/src/app .EXPOSE 9090ENTRYPOINT ["./app"]

gitlab-ci.yml

# This file is a template, and might need editing before it works on your project.stages:    - go-test    - build-image go-test:    stage: go-test    script:        - docker build -f DockerfileTest -t test-student:v0.1 . #把測試專用的dockerfile放到src目錄中        - docker run --rm test-student:v0.1 #進行測試,如果不通過後面就不會執行,如果通過後面繼續執行    after_script:        - docker rmi test-student:v0.1    tags:        - shellbuild-image:    stage: build-image    before_script:        - docker login -u panzl -p **** registry.cacec.com.cn    script:        - docker build -f DockerfileProd -t registry.cacec.com.cn/ms/student:v0.1 .        - docker push registry.cacec.com.cn/ms/student:v0.1        - docker images    after_script:            - docker images|grep none|awk '{print $3}'|xargs docker rmi #為了刪除中間產生的那個鏡像檔案    tags:        - shell

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.