Skip to content

Commit 53ab26f

Browse files
authored
Add devcontainer to the project (#157)
1 parent 276f50a commit 53ab26f

File tree

4 files changed

+184
-0
lines changed

4 files changed

+184
-0
lines changed

.devcontainer/Dockerfile

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Use the official PowerShell image as the base
2+
FROM mcr.microsoft.com/powershell:lts-ubuntu-22.04
3+
4+
# Add a label to name your image
5+
LABEL name="dscresource-docgenerator"
6+
7+
# Set environment variables for versions
8+
# Debian version should be the same as the base image.
9+
ENV UBUNTU_VERSION=22.04
10+
ENV DOTNET_SDK_VERSION=8.0
11+
ENV POWERSHELL_VERSION=7.4.5
12+
ENV POWERSHELL_PACKAGE_REVISION=1
13+
ENV GITVERSION_VERSION=5.*
14+
15+
# Install necessary packages
16+
RUN apt-get update && \
17+
apt-get install -y --no-install-recommends \
18+
git \
19+
wget \
20+
curl \
21+
unzip \
22+
sudo \
23+
nano \
24+
locales \
25+
apt-transport-https \
26+
ca-certificates \
27+
gpg \
28+
gnupg2 \
29+
software-properties-common && \
30+
apt-get clean -y && \
31+
rm -rf /var/lib/apt/lists/*
32+
33+
# Install .NET SDK
34+
# hadolint ignore=DL3008
35+
RUN wget https://packages.microsoft.com/config/ubuntu/$UBUNTU_VERSION/packages-microsoft-prod.deb -O packages-microsoft-prod.deb && \
36+
dpkg -i packages-microsoft-prod.deb && \
37+
rm packages-microsoft-prod.deb && \
38+
apt-get update && \
39+
apt-get install -y --no-install-recommends dotnet-sdk-$DOTNET_SDK_VERSION && \
40+
apt-get clean -y && \
41+
rm -rf /var/lib/apt/lists/*
42+
43+
# Remove the existing powershell-lts package to prevent conflicts instead of forcing
44+
# dpkg to overwrite the existing files. Forcing dpkg is generally not recommended
45+
# as it can lead to package management issues down the line.
46+
RUN apt-get update && \
47+
apt-get remove -y powershell-lts && \
48+
apt-get clean -y && \
49+
rm -rf /var/lib/apt/lists/*
50+
51+
# Update PowerShell to the specified version
52+
RUN wget https://github.com/PowerShell/PowerShell/releases/download/v$POWERSHELL_VERSION/powershell_${POWERSHELL_VERSION}-${POWERSHELL_PACKAGE_REVISION}.deb_amd64.deb -O powershell.deb \
53+
&& dpkg -i powershell.deb \
54+
&& apt-get update \
55+
&& apt-get install -f \
56+
&& rm powershell.deb \
57+
&& apt-get clean -y \
58+
&& rm -rf /var/lib/apt/lists/*
59+
60+
# Set the default shell to PowerShell
61+
ENV SHELL pwsh
62+
63+
# Set up a user (optional)
64+
ARG USERNAME=vscode
65+
ARG USER_UID=1000
66+
ARG USER_GID=$USER_UID
67+
68+
# Explanation:
69+
# 1. groupadd: Creates a new group with the specified GID ($USER_GID)
70+
# 2. useradd: Creates a new user with the specified UID ($USER_UID) and GID ($USER_GID)
71+
# The -m flag creates a home directory for the user
72+
# 3. echo: Adds a sudoers entry for the new user, allowing them to use sudo without a password
73+
# 4. chmod: Sets the permissions of the sudoers file to be read-only (0440)
74+
#
75+
# This setup creates a non-root user with sudo privileges, which is a common
76+
# practice in Docker containers for better security and to avoid running
77+
# processes as root.
78+
RUN groupadd --gid $USER_GID $USERNAME \
79+
&& useradd --uid $USER_UID --gid $USER_GID -m $USERNAME \
80+
&& echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \
81+
&& chmod 0440 /etc/sudoers.d/$USERNAME
82+
83+
# Switch to non-root user
84+
USER $USERNAME
85+
86+
# Set the working directory
87+
WORKDIR /workspace
88+
89+
# Install GitVersion as a dotnet tool for the vscode user
90+
RUN dotnet tool install --global GitVersion.Tool --version $GITVERSION_VERSION
91+
92+
# Add alias for gitversion, ensuring a newline before the alias
93+
RUN printf "\nalias gitversion=\"dotnet-gitversion\"\n" >> /home/$USERNAME/.bashrc && \
94+
mkdir -p /home/$USERNAME/.config/powershell && \
95+
echo 'New-Alias -Name gitversion -Value dotnet-gitversion' >> /home/$USERNAME/.config/powershell/Microsoft.PowerShell_profile.ps1

.devcontainer/devcontainer.json

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "DscResource.DocGenerator PowerShell Development Container",
3+
"build": {
4+
"dockerfile": "Dockerfile",
5+
"context": "..",
6+
"args": {
7+
// Add any build arguments here if needed
8+
}
9+
},
10+
"customizations": {
11+
"vscode": {
12+
"settings": {
13+
"terminal.integrated.shell.linux": "/usr/bin/pwsh",
14+
"security.workspace.trust.enabled": true,
15+
"security.workspace.trust.untrustedFiles": "open",
16+
"security.workspace.trust.trustedFolders": [
17+
"/workspaces"
18+
],
19+
"security.workspace.trust.startupPrompt": "never"
20+
},
21+
"extensions": [
22+
"streetsidesoftware.code-spell-checker",
23+
"usernamehw.errorlens",
24+
"davidanson.vscode-markdownlint",
25+
"pspester.pester-test",
26+
"ms-vscode.powershell",
27+
"redhat.vscode-yaml",
28+
"gruntfuggly.todo-tree",
29+
"codecov.codecov"
30+
]
31+
}
32+
},
33+
"postCreateCommand": "",
34+
"remoteUser": "vscode",
35+
"workspaceMount": "source=${localWorkspaceFolder},target=/workspaces/DscResource.DocGenerator,type=bind,consistency=cached",
36+
"workspaceFolder": "/workspaces/DscResource.DocGenerator",
37+
"shutdownAction": "stopContainer"
38+
}

.github/workflows/dockerfile-lint.yml

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: Lint Dockerfile
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- main
7+
8+
jobs:
9+
lint:
10+
runs-on: ubuntu-latest
11+
12+
# cSpell: ignore hadolint .devcontainer sarif codeql
13+
steps:
14+
- name: Checkout code
15+
uses: actions/checkout@v4
16+
17+
- name: Install Hadolint
18+
run: sudo wget -O /bin/hadolint https://github.com/hadolint/hadolint/releases/download/v2.8.0/hadolint-Linux-x86_64 && sudo chmod +x /bin/hadolint
19+
20+
- name: Create testResults directory
21+
run: mkdir -p testResults
22+
23+
- name: Lint Dockerfile
24+
run: hadolint .devcontainer/Dockerfile --failure-threshold warning --format sarif > testResults/hadolint-results.sarif
25+
continue-on-error: true
26+
27+
- name: Check if SARIF file exists
28+
id: check_sarif
29+
run: |
30+
if [ -f testResults/hadolint-results.sarif ]; then
31+
echo "file_exists=true" >> $GITHUB_OUTPUT
32+
else
33+
echo "file_exists=false" >> $GITHUB_OUTPUT
34+
fi
35+
36+
- name: Upload SARIF file
37+
uses: github/codeql-action/upload-sarif@v3
38+
if: always() && steps.check_sarif.outputs.file_exists == 'true'
39+
with:
40+
sarif_file: testResults/hadolint-results.sarif
41+
42+
- name: Upload testResults
43+
uses: actions/upload-artifact@v4
44+
if: always() && steps.check_sarif.outputs.file_exists == 'true'
45+
with:
46+
name: hadolint-results
47+
path: testResults/hadolint-results.sarif

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
55

66
## [Unreleased]
77

8+
### Added
9+
10+
- Added a devcontainer for development.
11+
812
## [0.12.5] - 2024-08-14
913

1014
- `Get-ClassResourceProperty`

0 commit comments

Comments
 (0)