如何在 github actions 中使用私有 docker 镜像

2024-01-15

我正在尝试在 github-actions 中设置一个运行私有 docker 映像的作业。我将使用容器选项在该 docker 映像内进行构建。link https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idcontainer.

我正在使用以下代码:

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

但我无法提供我的 docker hub 凭据,因此失败。

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

Thanks.


对于那些尝试使用customDocker 镜像已发布到新的 GitHub Docker 容器注册表,网址为ghcr.io在你的其中之一工作或步骤,这就是我所做的。

Steps

  1. Create a Personal Access Token, as seen on GitHub documentation for the new Docker Container Registry https://docs.github.com/en/free-pro-team@latest/packages/getting-started-with-github-container-registry/migrating-to-github-container-registry-for-docker-images#authenticating-with-the-container-registry. To do this, go to your GitHub Account > Settings > Developer Settings > Personal Access Tokens and select the following options for your token:

    Creating a New Personal Access Token

  2. Go to your project's GitHub repository and go to Settings > Secrets > New Secret and create a secret like this: 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>。为此,您可以在文档中找到将 Docker 镜像推送到 GitHub Docker 容器注册表 https://docs.github.com/en/free-pro-team@latest/packages/getting-started-with-github-container-registry/migrating-to-github-container-registry-for-docker-images#migrating-a-docker-image-using-the-docker-cli。本质上,您可以在计算机中执行以下操作:

    # Login to your ghcr.io
    echo $DOCKER_CONTAINER_REGISTRY_TOKEN | docker login ghcr.io -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--"
    
    

Results

将某些内容推送到存储库后,您应该会看到类似的内容在您的操作中运行。在下面的截图中,我使用了我自己的docker镜像在这里找到 https://github.com/users/PabloAlexis611/packages/container/package/ubuntu-18.04-dotnet-sdk-3.1.402和我自己的super-action.

And then, you can see your job's run commands being executed inside a container using that Docker image! Job executing commands inside the container that uses the Docker image at ghcr.io

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 github actions 中使用私有 docker 镜像 的相关文章

随机推荐