Skip to content

Commit e959d37

Browse files
committed
Add support for devcontainers
For new Rails apps, a suitable devcontainer is enabled by default. The setup provided has been tested and confirmed to work with a singe click in Visual Studio Code.
1 parent 951ac44 commit e959d37

10 files changed

Lines changed: 187 additions & 9 deletions

File tree

.devcontainer/Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Make sure RUBY_VERSION matches the Ruby version in .ruby-version
2+
ARG RUBY_VERSION=3.3.5
3+
FROM ghcr.io/rails/devcontainer/images/ruby:$RUBY_VERSION

.devcontainer/compose.yaml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: "codeharbor"
2+
3+
services:
4+
rails-app:
5+
build:
6+
context: ..
7+
dockerfile: .devcontainer/Dockerfile
8+
9+
volumes:
10+
- ../..:/workspaces:cached
11+
12+
# Overrides default command so things don't shut down after the process ends.
13+
command: sleep infinity
14+
15+
# Uncomment the next line to use a non-root user for all processes.
16+
# user: vscode
17+
18+
# Use "forwardPorts" in **devcontainer.json** to forward an app port locally.
19+
# (Adding the "ports" property to this file will not forward from a Codespace.)
20+
depends_on:
21+
- selenium
22+
- postgres
23+
24+
selenium:
25+
image: selenium/standalone-chromium
26+
restart: unless-stopped
27+
28+
postgres:
29+
image: postgres:17
30+
restart: unless-stopped
31+
networks:
32+
- default
33+
volumes:
34+
- postgres-data:/var/lib/postgresql/data
35+
environment:
36+
POSTGRES_USER: postgres
37+
POSTGRES_PASSWORD: postgres
38+
39+
volumes:
40+
postgres-data:

.devcontainer/devcontainer.json

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
2+
// README at: https://github.com/devcontainers/templates/tree/main/src/ruby
3+
{
4+
"name": "CodeHarbor",
5+
"dockerComposeFile": "compose.yaml",
6+
"service": "rails-app",
7+
"workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}",
8+
9+
// Features to add to the dev container. More info: https://containers.dev/features.
10+
"features": {
11+
"ghcr.io/devcontainers/features/git-lfs:1": {},
12+
"ghcr.io/devcontainers/features/github-cli:1": {},
13+
"ghcr.io/rails/devcontainer/features/activestorage": {},
14+
"ghcr.io/devcontainers/features/node:1": {
15+
"installYarnUsingApt": false
16+
},
17+
"ghcr.io/rails/devcontainer/features/postgres-client": {}
18+
},
19+
20+
"containerEnv": {
21+
"COREPACK_ENABLE_DOWNLOAD_PROMPT": "0",
22+
"CAPYBARA_SERVER_PORT": "45678",
23+
"SELENIUM_HOST": "selenium",
24+
"DB_HOST": "postgres"
25+
},
26+
27+
// Use 'forwardPorts' to make a list of ports inside the container available locally.
28+
"forwardPorts": [3045, 7500],
29+
30+
"portsAttributes": {
31+
"3045": {
32+
"label": "webpack-dev-server"
33+
},
34+
"7500": {
35+
"label": "rails-server"
36+
}
37+
},
38+
39+
// Configure tool-specific properties.
40+
// "customizations": {},
41+
42+
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
43+
// "remoteUser": "root",
44+
45+
46+
// Use 'postCreateCommand' to run commands after the container is created.
47+
"postCreateCommand": "bin/setup"
48+
}

.github/dependabot.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,13 @@ updates:
4040
labels:
4141
- dependencies
4242
- docker
43+
44+
- package-ecosystem: devcontainers
45+
directory: "/"
46+
schedule:
47+
interval: daily
48+
time: "03:00"
49+
timezone: UTC
50+
labels:
51+
- dependencies
52+
- devcontainers

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
CodeHarbor is a repository system for automatically gradeable programming exercises and enables instructors to exchange of such exercises via the [ProFormA XML](https://github.com/ProFormA/proformaxml) format across diverse code assessment systems.
99

1010
## Server Setup and Deployment
11-
Use [Capistrano](https://capistranorb.com/). or the provided Dockerfile (only for production). Vagrant is for local development only.
12-
11+
Use [Capistrano](https://capistranorb.com/). or the provided Dockerfile (only for production). The devcontainer and Vagrant setup are for local development only.
1312

1413
## Development Setup
1514

bin/setup

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ FileUtils.chdir APP_ROOT do
2323
# Install JavaScript dependencies
2424
system! "yarn install"
2525

26-
# puts "\n== Copying sample files =="
27-
# unless File.exist?("config/database.yml")
28-
# FileUtils.cp "config/database.yml.sample", "config/database.yml"
29-
# end
26+
puts "\n== Copying sample files =="
27+
%w[action_mailer.yml content_security_policy.yml database.yml mnemosyne.yml].each do |file|
28+
unless File.exist?("config/#{file}")
29+
FileUtils.cp "config/#{file}.example", "config/#{file}"
30+
end
31+
end
3032

3133
puts "\n== Preparing database =="
3234
system! 'bin/rails db:prepare'

docs/LOCAL_SETUP.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
CodeHarbor consists of a web application that is connected to a PostgreSQL database. The following document will guide you through the setup of CodeHarbor with all aforementioned components.
44

5-
We recommend using the **native setup** as described below. We also prepared a setup with Vagrant using a virtual machine as [described in this guide](./LOCAL_SETUP_VAGRANT.md). However, the Vagrant setup might be outdated and is not actively maintained (PRs are welcome though!)
5+
We recommend using the **native setup** as described below or the **devcontainer setup** as [described in this guide](./LOCAL_SETUP_DEVCONTAINER.md). We also prepared a setup with Vagrant using a virtual machine as [described in this guide](./LOCAL_SETUP_VAGRANT.md). However, the Vagrant setup might be outdated and is not actively maintained (PRs are welcome though!)
66

77
## Native setup for CodeHarbor
88

docs/LOCAL_SETUP_DEVCONTAINER.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Devcontainer setup
2+
3+
With the devcontainer-based setup, you won't need to (manually) install CodeHarbor and all dependencies on your local instance. Instead, a Docker setup containing all requirements will be configured. The development environment is defined in the `.devcontainer` repository folder and will be applied when you open the project in a supported editor or IDE.
4+
5+
## Local setup
6+
7+
In order to run the devcontainer locally, you need to have Docker installed on your machine. You can find the installation instructions for your operating system on the [official Docker website](https://docs.docker.com/get-docker/). Then, you'll need an editor or IDE that supports devcontainers. We recommend [Visual Studio Code](https://code.visualstudio.com/),[RubyMine](https://www.jetbrains.com/ruby/), [IntelliJ IDEA](https://www.jetbrains.com/idea/).
8+
9+
### Clone the repository:
10+
11+
You may either clone the repository via SSH (recommended) or HTTPS (hassle-free for read operations). If you haven't set up GitHub with your SSH key, you might follow [their official guide](https://docs.github.com/en/authentication/connecting-to-github-with-ssh).
12+
13+
**SSH (recommended, requires initial setup):**
14+
```shell
15+
git clone git@github.com:openHPI/codeharbor.git
16+
```
17+
18+
**HTTPS (easier for read operations):**
19+
```shell
20+
git clone https://github.com/openHPI/codeharbor.git
21+
```
22+
23+
### Open the project in your editor or IDE:
24+
25+
Open the project in your editor or IDE to get started:
26+
27+
**Visual Studio Code:**
28+
```shell
29+
code codeharbor
30+
```
31+
32+
**RubyMine:**
33+
```shell
34+
rubymine codeharbor
35+
```
36+
37+
**IntelliJ IDEA:**
38+
```shell
39+
idea codeharbor
40+
```
41+
42+
### Install the recommended extensions:
43+
44+
When you open the project in an supported editor or IDE, you'll be prompted to install the recommended extension(s) for support with devcontainers. Click on "Install" to install the recommended extensions.
45+
46+
### Start the devcontainer:
47+
48+
After you've installed the recommended extension(s), you can start the devcontainer by a simple click.
49+
50+
**Visual Studio Code:**
51+
Click on the blue "Reopen in Container" button in the bottom right corner of your editor. [More information](https://code.visualstudio.com/docs/devcontainers/tutorial).
52+
53+
**RubyMine:** / **IntelliJ IDEA:**
54+
Open the file `.devcontainer/devcontainer.json` and click on the blue Docker icon in the top left corner of your editor. More information for [RubyMine](https://www.jetbrains.com/help/ruby/connect-to-devcontainer.html#create_dev_container_inside_ide) or [IntelliJ IDEA](https://www.jetbrains.com/help/idea/connect-to-devcontainer.html#create_dev_container_inside_ide).
55+
56+
# Start CodeHarbor
57+
58+
When developing with the devcontainer, you can run CodeHarbor in the same way as you would on your local machine. The only difference is that you're running it inside the devcontainer. You can find more information on how to run CodeHarbor in the [LOCAL_SETUP.md](LOCAL_SETUP.md#start-codeharbor). All ports are forwarded to your local machine, so you can access CodeHarbor in your browser as usual.

spec/support/capybara.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,16 @@
1616

1717
config.before(:each, type: :system, js: true) do
1818
# Selenium when we need JavaScript
19-
driven_by :selenium, using: :"#{display_mode}#{browser}", &send(:"#{browser}_options")
19+
if ENV['CAPYBARA_SERVER_PORT']
20+
ActionDispatch::SystemTestCase.served_by host: 'rails-app', port: ENV['CAPYBARA_SERVER_PORT']
21+
22+
driven_by :selenium, using: :headless_chrome, options: {
23+
browser: :remote,
24+
url: URI::HTTP.build(host: ENV.fetch('SELENIUM_HOST', nil), port: 4444).to_s,
25+
}, &chrome_options
26+
else
27+
driven_by :selenium, using: :"#{display_mode}#{browser}", &send(:"#{browser}_options")
28+
end
2029
end
2130

2231
private

spec/support/webmock.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
# frozen_string_literal: true
22

3-
WebMock.disable_net_connect!(allow_localhost: true)
3+
allowed_hosts = []
4+
5+
# Allow connections to the Selenium server and the Rails app
6+
if ENV['SELENIUM_HOST']
7+
allowed_hosts << ENV.fetch('SELENIUM_HOST')
8+
# This hostname is defined for devcontainers
9+
allowed_hosts << 'rails-app'
10+
end
11+
12+
WebMock.disable_net_connect!(allow_localhost: true, allow: allowed_hosts)

0 commit comments

Comments
 (0)