我如何在github操作中使用私有docker映像

时间:2020-09-23 17:53:33

标签: docker github-actions

我正在尝试在github-actions中设置一个运行私有docker映像的作业。我将使用container选项在该docker映像内进行构建。 link

我正在使用以下代码:

jobs:
  container1:
    runs-on: ubuntu-latest
    container: saeed/privateimage:1
    steps:
      - uses: actions/checkout@v2
      - run: |
          echo "Runs inside a container"

但是我无法提供我的docker hub凭据,所以失败了。

我如何进行身份验证以提取该私有图像?

谢谢。

3 个答案:

答案 0 :(得分:3)

好像已经在今天添加了对此功能的支持,请参见blog post

帖子使用以下示例:

jobs:
  build:
    container:
      image: octocat/ci-image:latest
      credentials:
        username: mona
        password: ${{ secrets.docker_hub_password}}
    services:
      db:
        image:  octocat/testdb:latest
        credentials:
          username: mona
          password: ${{ secrets.docker_hub_password }}

container的文档为here

答案 1 :(得分:2)

更新:检查@Benjamin W.answer。 GitHub操作added Private registry support for job and service containers


docs指示jobs.<job_id>.container.image应该是公开可用的图片:

“用作运行操作的容器的Docker映像。该值可以是Docker Hub映像名称或公共Docker注册表名称。”

您可以将访问docker专用注册表的凭据配置为secrets,然后使用密码登录并运行您的私有映像,例如:

  test:
    name: test
    runs-on: ubuntu-18.04
    steps:
      - uses: actions/checkout@v2

      - name: example.com docker registry login
        run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login example.com -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin

      - name: run backend tests using example.com/my-image
        run: |
          docker run --rm -i \
            -v ${PWD}:/workspace/source \
            -e PYTHONPATH=/workspace/source \
            -e DJANGO_SETTINGS_MODULE="www.settings" \
            -w /workspace/source \
            --entrypoint tox \
            example.com/my-image

答案 2 :(得分:1)

对于那些尝试使用 自定义 的Docker映像,在您的工作之一中发布到ghcr.io的新GitHub Docker容器注册表中或步骤,这就是我所做的。

步骤

  1. 创建一个个人访问令牌,如new Docker Container Registry的GitHub文档所示。为此,请转到您的GitHub Account > Settings > Developer Settings > Personal Access Tokens并为令牌选择以下选项:

    Creating a New Personal Access Token

  2. 转到项目的GitHub存储库,然后转到Settings > Secrets > New Secret并创建一个如下所示的秘密: Adding a secret containing your personal access token to your repository

  3. 获取该令牌并将其放入您的计算机环境中(或复制它,以可行的方式为准):

    export DOCKER_CONTAINER_REGISTRY_TOKEN=<the personal access token>
    
  4. 将您的Docker映像推送到ghcr.io/<YOUR_USERNAME>/<IMAGE_NAME>:<IMAGE_TAG>。为此,您可以在Pushing Docker Images to the GitHub Docker Container Registry的文档中找到。本质上,您可以按照以下步骤在计算机中执行某些操作:

    # Login to your ghcr.io
    echo $DOCKER_CONTAINER_REGISTRY_TOKEN | docker login -u <YOUR_USERNAME> --password-stdin
    # As an example, here I pull an image, tag it, and push it.
    docker pull ubuntu:18.04
    docker tag ubuntu:18.04 ghcr.io/<YOUR_USERNAME>/my_special_ubuntu:latest
    docker push ghcr.io/<YOUR_USERNAME>/my_special_ubuntu:latest
    
  5. 然后,在存储库中的.github/workflows/文件夹下创建一个操作。在此示例中,我们将其命名为super-action

    # You can just create the file in whichever editor you use.
    # This can do the work though...
    cd $YOUR_PROJECT_PATH/.github/workflows
    touch super-action.yml
    
  6. 打开super-action.yml操作,您可以执行以下操作:

    # Action name
    name: Super Action
    
    # Here, this action will be enabled on all pushes.
    # Modify this to fit your needs.
    on:
        push
    
    # Jobs section
    jobs:
        # The job that will use the container image you just pushed to ghcr.io
        super-job:
            runs-on: ubuntu-18.04
            container:
                image: ghcr.io/<YOUR_USERNAME>/<IMAGE_NAME>:<IMAGE_TAG>
                credentials:
                   username: <YOUR_USERNAME>
                   password: ${{  secrets.DOCKER_CONTAINER_REGISTRY_TOKEN }}
            steps:
                - name: super-step
                  shell: bash
                  run: |
                    # Whatever commands you want to run here using the container with your
                    # new Docker image at ghcr.io!>
                    echo "--This is running in my custom Docker image--"
    
    

结果

将某些内容推送到仓库后,您应该会在操作中看到类似的内容。在以下屏幕截图中,我使用了自己的docker映像found here。和我自己的super-action

Docker image is pulled in the GitHub action

然后,您会看到使用该Docker映像在容器内执行作业的run命令! Job executing commands inside the container that uses the Docker image at ghcr.io