说明:在运行CI时,每次都会重新拉取yaml中指定的镜像,重新拉取仓库最新代码。当运行时,当前目录就是仓库下最新代码所在的目录。可以理解为就是在.gitlab-ci.yaml所在目录下
生成whl包:需要编写setup.py文件,具体可参考setup.py编写
提交代码到仓库(需要事先配置SSH免密提交),具体配置步骤如下:
$ ssh-keygen -t rsa -C "xxxx@xxx.com"
# 会产生两个文件:
# id_rsa : 这是私钥, 对应 SSH_PRIVATE_KEY
# id_rsa.pub : 这是公钥,对应 SSH_PUBLIC_KEY
编写.gitlab-ci.yml代码:
image: gitlab_ci_image:latest
variables:
USER_NAME: "SWHL"
USER_EMAIL: "liekkaskono@163.com"
REMOTE_URL: "git@${CI_SERVER_HOST}:${CI_PROJECT_PATH}.git"
CI_COMMIT_FOLDER: "tmp"
before_script:
## Install ssh-agent if not already installed, it is required by Docker.
- 'command -v ssh-agent >/dev/null || ( apt-get update -y && apt-get install openssh-client -y )'
## Run ssh-agent (inside the build environment)
- eval $(ssh-agent -s)
## Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
## Create the SSH directory and give it the right permissions
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
## Use ssh-keyscan to scan the keys of your private server. Replace gitlab.com
- echo "StrictHostKeyChecking no" >> ~/.ssh/config
- ssh-keyscan 10.252.19.101 >> ~/.ssh/known_hosts
- chmod 644 ~/.ssh/known_hosts
- git config --global user.name ${USER_NAME}
- git config --global user.email ${USER_EMAIL}
stage:
- gen_whl_and_push_repo
gen_whl_and_push_repo:
stage: gen_whl_and_push_repo
tags:
- python
script:
- mkdir ${CI_COMMIT_FOLDER}
- git clone ${REMOTE_URL} ${CI_COMMIT_FOLDER}
- cd ${CI_COMMIT_FOLDER}
- python setup.py bdist_wheel # 生成whl包,位于dist目录下
- git add dist/*.whl # 添加whl到git中
- git commit -m 'gitlab ci submit'
- git push ${REMOTE_URL} # 推到仓库下
stage: job_name
job_name:
stage: job_name
tags:
- python
script:
- export