python - 是否可以使用 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)
这应该:
- 打印当前时间
- 生成一个 10 行 4 列的数据框,随机填充 0 到 100 之间的值
- 打印所述数据框
- 将数据框保存到本地驱动器
执行 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 管道中是否有另一种方法可以做到这一点?
解决方案
您需要在本地机器上安装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)
推荐阅读
- r - 显示错误标签的传单
- python - 有什么方法可以过早停止泡菜过程?
- google-apps-script - 将重定向 URI 添加到自动生成的 Google OAuth 2.0 客户端 ID
- javascript - 我怎样才能得到第一百个货币符号?
- javascript - 具有多个条目的 Webpack 5 ChunkGroup API?
- javascript - 通过自动发送数据获得高 reCaptcha 分数
- function - 在实现结构的默认特征并使用函数初始化其成员时,这些函数是评估一次还是多次?
- ubuntu - 在 Ubuntu 20 中安装 unixodbc-dev
- android - API 调用之间的 Firebase 令牌不一致
- javascript - 页面后退按钮不重置css