如何将 Pulumi 添加到我的 GitHub Codespaces / VSCode .NET devcontainer?

How do I add Pulumi to my GitHub Codespaces / VSCode .NET devcontainer?

我想通过 VS Code .devcontainer 使用 Pulumi .NET / C# 开发和部署 IaC。如何将此功能添加到环境中?

我将 https://github.com/pulumi/pulumi-docker-containers/blob/main/docker/dotnet/Dockerfile 中的构建容器步骤包含在 .devcontainer/Dockerfile 中:

ARG DOTNET_VARIANT="3.1"
ARG PULUMI_VERSION=latest
ARG INSTALL_NODE="true"
ARG NODE_VERSION="lts/*"

# --------------------------------------------------------------------------------

FROM debian:11-slim AS builder
RUN apt-get update -y && \
    apt-get upgrade -y && \
    apt-get install -y \
    curl \
    build-essential \
    git

RUN if [ "$PULUMI_VERSION" = "latest" ]; then \
    curl -fsSL https://get.pulumi.com/ | bash; \
    else \
    curl -fsSL https://get.pulumi.com/ | bash -s -- --version $PULUMI_VERSION ; \
    fi

# --------------------------------------------------------------------------------

FROM mcr.microsoft.com/vscode/devcontainers/dotnetcore:0-${DOTNET_VARIANT}

RUN if [ "${INSTALL_NODE}" = "true" ]; then su vscode -c "umask 0002 && . /usr/local/share/nvm/nvm.sh && nvm install ${NODE_VERSION} 2>&1"; fi

COPY --from=builder /root/.pulumi/bin/pulumi /pulumi/bin/pulumi
COPY --from=builder /root/.pulumi/bin/*-dotnet* /pulumi/bin/

ENV PATH "/pulumi/bin:${PATH}"

我用这个 .devcontainer/devcontainer.json:

控制过程
// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.203.0/containers/alpine
{
    "name": "C# (.NET)",
    "build": {
        "dockerfile": "Dockerfile",
        "args": {
            "DOTNET_VARIANT": "3.1",
            "PULUMI_VERSION": "latest",
            "INSTALL_NODE": "true",
            "NODE_VERSION": "lts/*"
        }
    },
    "features": {
        "azure-cli": "latest"
    },
    // Set *default* container specific settings.json values on container create.
    "settings": {},
    // Add the IDs of extensions you want installed when the container is created.
    "extensions": [
        "ms-dotnettools.csharp"
    ],
    // Use 'forwardPorts' to make a list of ports inside the container available locally.
    // "forwardPorts": [],
    // Use 'postCreateCommand' to run commands after the container is created.
    // "postCreateCommand": "uname -a",
    // Replace when using a ptrace-based debugger like C++, Go, and Rust
    // "runArgs": [ "--init", "--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined" ],
    "runArgs": [
        "--init"
    ],
    // Comment out connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
    "remoteUser": "vscode"
}

be aware that the Rebuild will take a while and that you probably have to reload the devcontainer once it indicates success in the GitHub Codespaces: Details.

之后 pulumi login 例如pulumi new azure-csharp 应该在容器上工作。

您可以启动代码空间,并从代码空间内配置 devcontainer。

刚才做了,

Access the Command Palette (Shift + Command + P / Ctrl + Shift + P), then >start typing "dev container". Select Codespaces: Add Development Container >Configuration Files....

只需遵循本指南

遵循本指南 - https://docs.github.com/en/codespaces/setting-up-your-project-for-codespaces/setting-up-your-project-for-codespaces

我受到此线程中提到的 DOTNET 构建器容器方法的启发,以下是我将 pulumi 添加到我的 GO 代码空间 dockerfile 的方法。

Dockerfile

ARG GO_VARIANT="1"
ARG PULUMI_VERSION=latest
FROM debian:11-slim AS builder
RUN apt-get update -y && \
    apt-get upgrade -y && \
    apt-get install -y \
    curl \
    build-essential \
    git

RUN if [ "$PULUMI_VERSION" = "latest" ]; then \
    curl -fsSL https://get.pulumi.com/ | bash; \
    else \
    curl -fsSL https://get.pulumi.com/ | bash -s -- --version $PULUMI_VERSION ; \
    fi

FROM mcr.microsoft.com/vscode/devcontainers/go:0-${GO_VARIANT}

# [Choice] Node.js version: none, lts/*, 16, 14, 12, 10
ARG NODE_VERSION="none"
RUN if [ "${NODE_VERSION}" != "none" ]; then su vscode -c "umask 0002 && . /usr/local/share/nvm/nvm.sh && nvm install ${NODE_VERSION} 2>&1"; fi

COPY --from=builder /root/.pulumi/bin/pulumi /pulumi/bin/pulumi
COPY --from=builder /root/.pulumi/bin/*-go* /pulumi/bin/

ENV PATH "/pulumi/bin:${PATH}"
ENV GOOS "linux"
ENV GOARCH "amd64"

来自 devcontainer.json

的代码片段
        "args": {
           ... //redacted for clarity
            "GO_VARIANT": "1.18",
        }

)

我可以用 azure 登录,pulumi 和 pulumi up 也可以。

这里有完整的例子 https://github.com/DevOpsJava/solution-using-secret

我解决了 pulumi up 的一个问题,后来发现这是一个关于内存的资源问题。将代码空间从 2 核切换到 4 核,这也将内存增加了一倍,达到 8GB。