Skip to content

Commit

Permalink
Fix tests & modify GHA workflows
Browse files Browse the repository at this point in the history
Pipeline to test, build and deploy server to the ECS cluster
  • Loading branch information
Jyri Högman committed Apr 11, 2024
1 parent 07c4bf9 commit 7dce64c
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 123 deletions.
80 changes: 29 additions & 51 deletions .github/workflows/build_server.yaml
Original file line number Diff line number Diff line change
@@ -1,75 +1,53 @@
name: "Build server"

on:
push:
branches:
- main
pull_request:
branches:
- main
workflow_call:

jobs:
quality:
name: Quality Checks
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Check formatting
run: cargo fmt --all --check

- name: Lint
run: cargo clippy --all-targets --all-features -- -D warnings

- name: Audit
uses: rustsec/[email protected]
with:
token: ${{ secrets.GITHUB_TOKEN }}

build:
needs: quality
runs-on: ubuntu-latest
name: "Build and push to ECR"
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v1
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}
aws-region: ${{ vars.AWS_REGION }}

- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
id: login-ecr-public
uses: aws-actions/amazon-ecr-login@v2
with:
registry-type: public

- name: Build, tag, and push image to Amazon ECR
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: waterheater-calc
IMAGE_TAG: latest
REGISTRY: ${{ steps.login-ecr-public.outputs.registry }}
REGISTRY_ALIAS: ${{ vars.REGISTRY_ALIAS }}
REPOSITORY: waterheater-calc
run: |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG -f server/Dockerfile server
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
docker build -t $REGISTRY/$REGISTRY_ALIAS/$REPOSITORY:latest -f server/Dockerfile .
docker push $REGISTRY/$REGISTRY_ALIAS/$REPOSITORY:latest
# deploy:
# name: Update ECS instances with new image
# needs: build
# runs-on: ubuntu-latest
# steps:
# - name: Checkout Repository
# uses: actions/checkout@v2
deploy:
name: Update ECS instances with new image
needs: build
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v4

# - name: Configure AWS Credentials
# uses: aws-actions/configure-aws-credentials@v1
# with:
# aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
# aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
# aws-region: ${{ secrets.AWS_REGION }}
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ vars.AWS_REGION }}

# - name: Update ECS Service
# run: |
# aws ecs update-service --cluster ${{ secrets.ECS_CLUSTER }} --service ${{ secrets.ECS_SERVICE } --force-new-deployment
- name: Update ECS Service
run: |
aws ecs update-service --cluster ${{ secrets.ECS_CLUSTER }} --service ${{ secrets.ECS_SERVICE }} --force-new-deployment
61 changes: 61 additions & 0 deletions .github/workflows/workflow_server.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: Server workflow

on:
push:
branches:
- "**"
paths:
- ".github/workflows/workflow_server.yaml"
- ".github/workflows/build_server.yaml"
- "server/src/**"
- "server/Cargo.toml"
- "server/Cargo.lock"
- "server/Dockerfile"
- "Cargo.toml"
- "Cargo.lock"
workflow_dispatch:

jobs:
quality:
name: Quality Checks for Server
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Check formatting
run: cargo fmt --package server --all --check

- name: Lint
run: cargo clippy --package server --all-targets --all-features -- -D warnings

- name: Audit
uses: rustsec/[email protected]
with:
token: ${{ secrets.GITHUB_TOKEN }}

build:
name: "Build"
needs: quality
uses: ./.github/workflows/build_server.yaml
secrets: inherit

deploy:
if: ${{ github.ref_name == 'main' }}
name: Update ECS instances with new image
needs: build
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v2

- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ vars.AWS_REGION }}

- name: Update ECS Service
run: |
aws ecs update-service --cluster ${{ secrets.ECS_CLUSTER }} --service ${{ secrets.ECS_SERVICE }} --force-new-deployment
82 changes: 41 additions & 41 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 21 additions & 21 deletions server/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion server/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
[package]
name = "waterheater-calc"
name = "server"
version = "0.1.0"
edition = "2021"
target-dir = "./target"

[dependencies]
axum = "0.7.5"
Expand Down
14 changes: 8 additions & 6 deletions server/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ WORKDIR /app
RUN apt-get update
RUN apt-get install -y pkg-config openssl libssl-dev

COPY . .

RUN cargo build --release
COPY ./server/ ./server/
COPY ./worker/ ./worker/
COPY Cargo.toml .
COPY Cargo.lock .
RUN cd server && cargo build -p server --release

FROM debian:bullseye-slim AS final

RUN apt-get update && apt-get install -y ca-certificates pkg-config openssl libssl-dev curl && update-ca-certificates
WORKDIR /app
COPY --from=build /app/target/release/waterheater-calc .
WORKDIR /app/server
COPY --from=build /app/target/release/server .

ARG UID=10001
RUN adduser \
Expand All @@ -30,6 +32,6 @@ RUN chown -R appuser:appuser /app && chmod -R 755 /app

USER appuser

CMD ["/app/waterheater-calc"]
CMD ["/app/server/server"]

EXPOSE 8001
4 changes: 2 additions & 2 deletions server/src/tests/service_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ fn test_get_filtered_pricing_cross_midnight() {

#[test]
fn test_calculate_cheapest_start_time() {
let pricing_data = vec![
let pricing_data = [
create_pricing_with_hour(22, 0.1),
create_pricing_with_hour(23, 0.2),
create_pricing_with_hour(0, 0.3),
Expand Down Expand Up @@ -107,7 +107,7 @@ fn test_calculate_cheapest_start_time() {

#[test]
fn test_calculate_cheapest_start_time_before_midnight() {
let pricing_data = vec![
let pricing_data = [
create_pricing_with_hour_and_day(22, 9, 4.722),
create_pricing_with_hour_and_day(23, 9, 4.078),
create_pricing_with_hour_and_day(0, 10, 0.619),
Expand Down
3 changes: 2 additions & 1 deletion worker/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
[package]
name = "waterheater-calc-worker"
name = "worker"
version = "0.1.0"
edition = "2021"
target-dir = "./target"

[dependencies]
aws_lambda_events = { version = "0.15.0", default-features = false, features = [
Expand Down

0 comments on commit 7dce64c

Please sign in to comment.