Skip to content

Commit

Permalink
hw25-ci-cd: updates
Browse files Browse the repository at this point in the history
  • Loading branch information
vyefremov committed Jun 23, 2024
1 parent 21e8db1 commit 82fd251
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/hw25-ci-cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ jobs:
env:
Version: ${{ needs.version.outputs.fullSemVer }}
working-directory: './hw25-ci-cd/src/webapi'
run: docker build -t hw25-webapi:${{ env.Version }} .
run: docker build --build-arg APP_VERSION=${{ env.Version }} -t hw25-webapi:${{ env.Version }} .

- name: Tag Docker Image
env:
Expand Down
57 changes: 57 additions & 0 deletions hw25-ci-cd/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Homework: CI/CD

## Task

## Solution

Setup AWS EC2 instance with Nginx and Docker installed.

```bash
sudo -i
apt update
apt install nginx
systemctl start nginx
systemctl status nginx
systemctl enable nginx

cd /etc/nginx


sudo apt install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt update
sudo apt install docker-ce
systemctl status docker
systemctl enable docker
```

```bash
sudo nano /etc/nginx/sites-available/webapi.conf
sudo ln -s /etc/nginx/sites-available/webapi.conf /etc/nginx/sites-enabled/

# Test Nginx configuration
sudo nginx -t
```

```
server {
listen 80;
location /api {
proxy_pass http://localhost:8080;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
}
```

```bash
docker pull mongo; docker run -d -p 27017:27017 --name mongodb mongo

docker pull vladyslavyefremov/hw25-webapi:0.1.0-hw25-ci-cd-run6.1
docker run -d -p 8080:8080 vladyslavyefremov/hw25-webapi:0.1.0-hw25-ci-cd-run6.1 --name hw25-webapi
```
5 changes: 3 additions & 2 deletions hw25-ci-cd/src/integration-tests/AnalyticsEndpointsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)

builder.UseContentRoot(".");
builder.UseEnvironment("Testing");
builder.UseSetting("MongoConnection", "mongodb://localhost:27017");

builder.ConfigureServices(services =>
{
Expand All @@ -57,9 +58,9 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)

public interface IEndpoints
{
[Post("/analytics/events")]
[Post("/api/v1/analytics/events")]
Task<IApiResponse> CreateEvent([Body] AnalyticsEvent request);

[Get("/analytics/events/summary")]
[Get("/api/v1/analytics/events/summary")]
Task<IApiResponse<Dictionary<string, AnalyticsEventSummary>>> GetEventsSummary([Query] DateTime from, [Query] DateTime to);
}
2 changes: 2 additions & 0 deletions hw25-ci-cd/src/webapi/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ EXPOSE 8080

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
ARG APP_VERSION=1.0.0
WORKDIR /webapi
COPY ["webapi.csproj", "."]
RUN dotnet restore "webapi.csproj"
COPY . .
RUN sed -i "s/VERSION_TO_REPLACE/$APP_VERSION/g" appsettings.json
WORKDIR "/webapi"
RUN dotnet build "webapi.csproj" -c $BUILD_CONFIGURATION -o /app/build

Expand Down
9 changes: 4 additions & 5 deletions hw25-ci-cd/src/webapi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,20 @@
builder.Services
.AddEndpointsApiExplorer()
.AddSingleton<IAnalyticsEventSummaryBuilder, AnalyticsEventSummaryBuilder>()
.AddMongoStore(builder.Configuration, builder.Environment)
.AddElasticStore(builder.Configuration, builder.Environment);
.AddMongoStore(builder.Configuration, builder.Environment);

var app = builder.Build();

app.MapGet("/version", (IConfiguration configuration) => configuration.GetValue("Version", "1.0.0"));
app.MapGet("api/v1/version", (IConfiguration c) => Results.Ok(c.GetValue("ImageVersion", "Unknown")));

app.MapPost("/analytics/events", async (AnalyticsEvent request, IEnumerable<IAnalyticsEventStore> stores) =>
app.MapPost("api/v1/analytics/events", async (AnalyticsEvent request, IEnumerable<IAnalyticsEventStore> stores) =>
{
await Task.WhenAll(stores.Select(store => store.InsertAsync(request)));

return Results.Accepted();
});

app.MapGet("/analytics/events/summary",
app.MapGet("api/v1/analytics/events/summary",
async (DateTime? from,
DateTime? to,
IEnumerable<IAnalyticsEventStore> stores,
Expand Down
5 changes: 3 additions & 2 deletions hw25-ci-cd/src/webapi/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
}
},
"AllowedHosts": "*",
"MongoConnection": "mongodb://localhost:27017",
"MongoConnection": "mongodb://mongodb:27017",
"MongoDatabase": "Analytics",
"ElasticsearchConnection": "http://elasticsearch:9200"
"ElasticsearchConnection": "http://elasticsearch:9200",
"ImageVersion": "VERSION_TO_REPLACE"
}
5 changes: 5 additions & 0 deletions hw25-ci-cd/src/webapi/endpoints.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
### Get version
GET http://localhost:5000/api/v1/version

### Get version Docker
GET http://localhost:8080/api/v1/version
1 change: 0 additions & 1 deletion hw25-ci-cd/src/webapi/webapi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,4 @@
<PackageReference Include="MongoDB.Driver" Version="2.24.0" />
<PackageReference Include="prometheus-net.AspNetCore" Version="8.2.1" />
</ItemGroup>

</Project>

0 comments on commit 82fd251

Please sign in to comment.