我想创建一个自定义 Docker 容器 Github 操作并从此 action.yml 文件开始
name: my-action
description: For testing purposes
branding:
icon: shield
color: green
inputs:
my-input:
description: demo
required: true
runs:
using: docker
image: Dockerfile
接下来我在根目录下的 app.js 文件中添加了一些代码
const core = require('@actions/core');
try {
const myInput = core.getInput('my-input', { required: true });
core.notice(`Input is: ${myInput}`);
} catch (error) {
core.setFailed(error.message);
}
Dockerfile
FROM node:16.13.0-alpine
USER node
WORKDIR /home/node
ADD --chown=node:node . /home/node
RUN npm install
CMD [ "node", "app.js" ]
为了测试我的操作,我发布了我的操作并创建了一个新的工作流程
name: Run the action
on: workflow_dispatch
jobs:
run-the-action:
runs-on: ubuntu-latest
steps:
- name: Run the action
uses: me/[email protected]
with:
my-input: hello
不幸的是,该操作崩溃并出现错误消息
错误:找不到模块'/github/workspace/app.js'
也许我缺少从
/home/node
到 /github/workspace
的映射?有人知道出了什么问题或如何解决吗?
如果您需要更多信息,请告诉我!
首先,您不能在自定义 GitHub Actions Dockerfile 上使用
WORKDIR
命令。这在 GitHub Actions docs 中进行了描述。
其次,使用
npm
作为包管理器会导致错误。我建议使用 yarn
代替。
最后,您必须授予
index.js
文件执行权限。
您的 Dockerfile 应该是这样的:
FROM node:20
COPY ./package.json ./
COPY ./yarn.lock ./
RUN yarn
COPY . .
RUN yarn build
RUN ["chmod", "+x", "/dist/index.js"]
ENTRYPOINT ["node", "/dist/index.js"]