首页 > 解决方案 > 是否可以使用 GitLab CI 将文件写入您的机器(本地驱动器)?

问题描述

我正在尝试使用 GitLab CI 运行 Python 脚本,它将创建一个 Pandas 数据框并将其作为 .csv 文件写入我的机器上。

作为测试脚本,我创建了以下do_stuff_2.py文件:

import datetime
import pandas as pd
import numpy as np

current_time = datetime.datetime.now()
print(f'Hello.\nCurrent date/time is:{current_time}')

df = pd.DataFrame(np.random.randint(0,100,size=(10, 4)), columns=list('ABCD'))
print(df)

df.to_csv('C:\\<USER_PATH>\\Desktop\\df_out.csv', index = False)

这应该:

执行 CI 管道时,我没有收到任何错误,并且前 3 个步骤成功运行: 流水线执行截图

我有一个.gitlab-ci.yml文件,其中包含以下内容:

stages:
    - build

build:
    stage: build
    image: python:3.6
    script: 
        - echo "Running python..."
        - pip install -r requirements.txt
        - python do_stuff_2.py

和一个requirements.txt文件:

numpy
pandas

看起来我已经正确设置了所有内容,因为时间正在显示并且print函数返回数据框。但是,没有文件写入指定位置。当我在本地运行脚本时,一切都按预期工作,并且数据框作为df_out.csv保存在我的桌面上。

我在 Windows 10 机器上使用 Python 3.6。

在 GitLab 的 CI 管道中是否有另一种方法可以做到这一点?

标签: pythonpython-3.xpandasgitlabgitlab-ci

解决方案


您需要在本地机器上安装gitlab-runner。

如果不能,您可以使用artifact:关键字将脚本结果上传到 gitlab 服务器,然后从 UI 下载。你的gitlab-ci.yml会看起来像:

stages:
    - build

build:
    stage: build
    image: python:3.6
    script: 
        - echo "Running python..."
        - pip install -r requirements.txt
        - python do_stuff_2.py
    artifacts: 
        paths:
        - df_out.csv

并且您的代码必须更改为:

df.to_csv('df_out.csv', index = False)

推荐阅读