简体   繁体   English

Github actions 在容器中运行步骤

[英]Github actions run steps in container

Currently I am trying to implement a contianer into my Github Actions workflow.目前,我正在尝试在我的 Github Actions 工作流程中实施一个容器。 However I am having difficulties figuring out how to run steps in the container itself.但是,我很难弄清楚如何在容器本身中运行步骤。

The following workflow is used:使用以下工作流程:

name: Laravel

on: pull_request

jobs:
  laravel-checks:
    runs-on: ubuntu-latest
    container: thomasowow/laravel-php:7.4

    steps:
    - uses: actions/checkout@v2

    - name: Yarn
      run: |
        yarn

This workflow results in the following error:此工作流导致以下错误:

/__w/_temp/c399fe7d-6cd2-4cdc-bb06-acc829cddbb8.sh: 1: /__w/_temp/c399fe7d-6cd2-4cdc-bb06-acc829cddbb8.sh: yarn: not found
##[error]Process completed with exit code 127

It is unable to find yarn .它无法找到yarn The thomasowow/laravel-php:7.4 runs locally with yarn available. thomasowow/laravel-php:7.4在本地运行,可用yarn I have tested this with other things that should be avilable in the docker image and they were not found either.我已经用 docker 图像中应该可用的其他东西测试了这个,但也没有找到。 It looks like the steps are not being executed in the container.看起来这些步骤没有在容器中执行。

The documentation states the following for the jobs.<job_id>.container syntax:文档说明jobs.<job_id>.container语法的以下内容:

A container to run any steps in a job that don't already specify a container一个容器,用于运行尚未指定容器的作业中的任何步骤

I know there are solutions that work without using a container, I would prefer to use it.我知道有些解决方案无需使用容器即可工作,我更愿意使用它。

Anybody had the same issue or knows what I am doing wrong?任何人有同样的问题或知道我做错了什么?


Solution解决方案

@DannyB pointed out that my image contains the following entrypoint: @DannyB 指出我的图像包含以下入口点:

["/bin/bash", "--login", "-c", "tail -f /dev/null"]

This might have been the cause of Github not being able to run things in the container properly.这可能是 Github 无法在容器中正常运行的原因。

They were required in the image to install nvm, node and yarn图像中需要它们来安装 nvm、node 和 yarn

SHELL ["/bin/bash", "--login", "-c"]

RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
RUN nvm install 12.18.3
RUN npm install -g yarn

CMD ["/bin/bash"]

Removing SHELL to RUN npm... solved the issue and Github was able again to run things in the container properly.删除SHELLRUN npm...解决了问题,Github 能够再次在容器中正常运行。

Currently I am still unable to install yarn without my old solution, but I think there are other ways to do this.目前,如果没有我的旧解决方案,我仍然无法安装 yarn,但我认为还有其他方法可以做到这一点。 Anybody suggestions how to do this in a clean way?有人建议如何以干净的方式做到这一点吗?


Installing node and yarn安装节点和纱线

I was able to get node and yarn installed using this answer .我能够使用此答案安装nodeyarn

ENV NODE_VERSION=12.18.3
RUN curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash
ENV NVM_DIR=/root/.nvm
RUN . "$NVM_DIR/nvm.sh" && nvm install ${NODE_VERSION}
RUN . "$NVM_DIR/nvm.sh" && nvm use v${NODE_VERSION}
RUN . "$NVM_DIR/nvm.sh" && nvm alias default v${NODE_VERSION}
ENV PATH="/root/.nvm/versions/node/v${NODE_VERSION}/bin/:${PATH}"
RUN npm install -g yarn

Some attempts were made to COPY from the office Node docker images.尝试从办公室节点 docker 图像进行COPY With this solution I was able to get node working.通过这个解决方案,我能够让node工作。 npm and yarn were also running but with errors. npmyarn也在运行但有错误。

COPY --from=node:12.18.3 /usr/local/bin/node /usr/bin/node
...

The problem seems to be in your image, and not in your syntax of GitHub Actions.问题似乎出在您的图像中,而不是在您的 GitHub Actions 语法中。

It seems like your entrypoint is:看起来你的入口点是:

["/bin/bash", "--login", "-c", "tail -f /dev/null"]

This is not compatible with what GitHub Actions needs - since it cannot enter into your container at all with that entrypoint.这与 GitHub Actions 需要的不兼容 - 因为它根本无法通过该入口点进入您的容器。

You will probably need to change your entrypoint to its standard /bin/sh or /bin/bash entrypoint.您可能需要将入口点更改为其标准的/bin/sh/bin/bash入口点。 Make sure that you can bash into it locally before you expect GitHub Actions to be able to work on it.在您期望 GitHub 操作能够对其进行操作之前,请确保您可以 bash 在本地进入它。

$ docker run --rm -it thomasowow/laravel-php:7.4 bash

As a "container sandbox" proof of concept, you can try the below action, and see that it works as advertised.作为“容器沙箱”概念验证,您可以尝试以下操作,并查看它是否像宣传的那样工作。

name: Experiment
on: [push]

jobs:
  debug:
    runs-on: ubuntu-latest
    container: { image: alpine }
    steps:  
    - run: uname -a
    - run: cat /etc/alpine-release
    - run: touch /hello
    - run: ls /

In addition, you might be able to instruct GitHub Actions to use a different entrypoint with something like this:此外,您可以指示 GitHub Actions 使用不同的入口点,如下所示:

container: 
  image: thomasowow/laravel-php:7.4
  options: "--entrypoint /bin/bash"

But, first, you need to make sure it is working locally, with something like this:但是,首先,你需要确保它在本地工作,像这样:

$ docker run --rm -it --entrypoint='' thomasowow/laravel-php:7.4 yarn

Update regarding installing yarn关于安装纱线的更新

Adding to the answer, since you added to your question.添加到答案,因为你添加到你的问题。 Installing things inside a docker image should be straight forward for the most part, including things like node and yarn.在 docker 图像中安装东西在大多数情况下应该是直接的,包括节点和纱线之类的东西。

Although I am not a node user, I suspect that the problem lies in the fact that you are using nvm .虽然我不是 node 用户,但我怀疑问题出在你使用的是nvm In other languages (python, ruby) these "version managers" are designed to run in an interactive shell, knowing the user's environment and boot scripts.在其他语言(python、ruby)中,这些“版本管理器”被设计为在交互式 shell 中运行,了解用户的环境和启动脚本。 Inside docker, you should not need any version manager - since you do not need more than one version.在 docker 内部,您不需要任何版本管理器——因为您不需要多个版本。

Look for ways to install your dependencies "plain vanilla", and avoid version managers in Dockerfiles, and I am sure that your problems will either go away, or be reduced to simpler ones.寻找“普通香草”安装依赖项的方法,并避免在 Dockerfile 中使用版本管理器,我相信您的问题要么 go 消失,要么减少为更简单的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM