【问题标题】:How to use a script of a Docker container from CI pipeline如何使用 CI 管道中的 Docker 容器脚本
【发布时间】:2019-08-18 00:16:20
【问题描述】:

这里是 Docker 和 Docker 容器的新手。

我正在尝试了解如何从我的 bitbucket-pipeline 进程运行映像中的脚本。

关于我在哪里的一些背景和一些知识

在 Bitbucket-Pipelines 步骤中,您可以添加任何图像以在该特定步骤中运行。例如,我已经尝试过并且没有问题的工作是获取像 alpine:node 这样的图像,这样我就可以在我的管道脚本中运行 npm 命令:

definitions:
    steps:
        - step: &runNodeCommands
              image: alpine/node
              name: "Node commands"
              script:
                  - npm --version

pipelines:
    branches:
        master:
            - step: *runNodeCommands

这意味着 master 分支上的每次推送都将运行一个构建,其中使用 alpine/node 映像我们可以运行像 npm --version 这样的 npm 命令并安装包。

我做了什么

现在我正在使用一个自定义容器,我在其中安装了一些节点包(如 eslint)来运行命令。 IE。 eslint file1.js file2.js

太棒了!

我正在尝试但不知道如何

我有一个本地 bash 脚本 awesomeScript.sh,在我的存储库中有一些输入参数。所以我的bitbucket-pipelines.yml 文件看起来像:

definitions:
    steps:
        - step: &runCommands
              image: my-user/my-container-with-eslint
              name: "Running awesome script"
              script:
                  - ./awesomeScript.sh -a $PARAM1 -e $PARAM2

pipelines:
    branches:
        master:
            - step: *runCommands

我在不同的存储库中使用相同的awesomeScript.sh,我想将该功能移到我的 Docker 容器中并在存储库中删除该脚本

如何构建我的 Dockerfile 以便能够在我使用 docker 映像的“任何地方”运行该脚本?

PS:

我一直在考虑构建一个 node_module,像 eslint 模块一样在 Docker Image 中安装该模块......但我想知道这是否可能

谢谢!

【问题讨论】:

    标签: docker continuous-integration dockerfile bitbucket-pipelines


    【解决方案1】:

    如果您将awesomeScript.sh 复制到my-container-with-eslint Docker 映像,那么您应该能够在不需要每个存储库中的脚本的情况下使用它。

    my-container-with-eslint 的 Dockerfile 中的某处,您可以将脚本文件复制到映像中:

    COPY awesomeScript.sh /usr/local/bin/
    

    然后在 Bitbucket-Pipelines 中:

    definitions:
        steps:
            - step: &runCommands
                  image: my-user/my-container-with-eslint
                  name: "Running awesome script"
                  script:
                      - awesomeScript -a $PARAM1 -e $PARAM2
    
    pipelines:
        branches:
            master:
                - step: *runCommands
    

    【讨论】:

      【解决方案2】:

      正如peterevans 所说,如果您将脚本复制到您的 docker 镜像中,那么您应该能够使用它,而无需每个存储库中的脚本。

      在您的Dockerfile 中添加以下行:

      COPY awesomeScript.sh /usr/local/bin/ # you may use ADD too

      在 Bitbucket-Pipelines 中:

      pipelines:
          branches:
              master:
                  - step: 
                        image: <your user name>/<image name>
                        name: "Run script from the image"
                        script:
                            - awesomeScript -a $PARAM1 -e $PARAM2
      

      【讨论】:

        猜你喜欢
        • 2020-07-19
        • 1970-01-01
        • 2021-01-13
        • 1970-01-01
        • 1970-01-01
        • 2017-07-06
        • 1970-01-01
        • 2021-02-16
        • 2022-01-19
        相关资源
        最近更新 更多