Skip to content

Commit

Permalink
Добавлены CI кеши
Browse files Browse the repository at this point in the history
  • Loading branch information
vanya-beat committed Oct 7, 2024
1 parent 8201ba0 commit 54ef99f
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 36 deletions.
76 changes: 69 additions & 7 deletions .github/workflows/ci-cpp-build.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: C++ Build (Сборка проекта)
name: C++ Build and Run (Сборка и запуск проекта)

on:
push:
Expand All @@ -10,16 +10,78 @@ on:

jobs:
build:
name: Build Docker Image
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
# 1. Checkout the repository
- name: Checkout repository
uses: actions/checkout@v4

- name: Run docker-compose
uses: hoverkraft-tech/[email protected]
# 2. Set up Docker Buildx
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

# 3. Cache Docker layers
- name: Cache Docker layers
id: cache-docker-layers
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-docker-${{ github.sha }}
restore-keys: |
${{ runner.os }}-docker-
# 4. Build Docker image using Docker Compose
- name: Build Docker image with Docker Compose
run: |
docker compose build bmstu
docker compose up bmstu
# Save the image as a tarball
docker save bmstu_cpp_course-bmstu:latest -o cpp_course_image.tar
# 5. Upload Docker image tarball as artifact
- name: Upload Docker image tarball
uses: actions/upload-artifact@v3
with:
name: cpp_course-image
path: cpp_course_image.tar

# 6. Save Docker layer cache
- name: Save Docker layer cache
if: always()
uses: actions/upload-artifact@v3
with:
name: docker-layer-cache
path: /tmp/.buildx-cache

run-script:
name: Run run.sh Script
runs-on: ubuntu-latest
needs: build

steps:
# 1. Download the Docker image tarball artifact
- name: Download Docker image tarball
uses: actions/download-artifact@v3
with:
compose-file: "docker-compose.yml"
name: cpp_course-image
path: ./artifacts

- name:
# 2. Load the Docker image into Docker daemon
- name: Load Docker image
run: |
docker compose up --build --abort-on-container-exit --exit-code-from bmstu
docker load -i ./artifacts/cpp_course_image.tar
# 3. Create a Docker network (optional, if needed)
#- name: Create Docker network
# run: docker network create my-network || echo "Network already exists"

# 4. Run the Docker container and execute the script
- name: Run run.sh Script inside Docker Container
run: |
docker run --rm -v ${{ github.workspace }}:/cppcourse bmstu_cpp_course-bmstu:latest bash -c "/cppcourse/docker/runtasks/run.sh"
# 5. (Optional) Bring down any Docker Compose services if previously started
#- name: Tear Down Docker Compose (if applicable)
# run: docker compose down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ services:
volumes:
- .:/cpp_bmstu
working_dir: /cpp_bmstu
command: ["/bin/bash", "-c", "cmake -B build -S . && cmake --build build && /cpp_bmstu/docker/run_tasks/run.sh"]
command: ["/bin/bash", "-c", "cmake -B build -S . && cmake --build build"]
container_name: cpp_course
4 changes: 1 addition & 3 deletions docker/image/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM ubuntu:22.04

COPY docker/image/install_deps.sh /
RUN sh install_deps.sh

#COPY . /cpp_course
ENV TZ=Europe/Moscow

ENV LANG C.UTF-8
Expand All @@ -16,5 +16,3 @@ EXPOSE 22
RUN echo "StrictHostKeyChecking=no" >> /etc/ssh/ssh_config
RUN mkdir /var/run/sshd
CMD ["/usr/sbin/sshd", "-D"]


15 changes: 10 additions & 5 deletions tasks/task_basic_c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ message(STATUS "Running tasks/basic_c/CMakeLists.txt")
set(CMAKE_CXX_STANDARD 26)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
get_filename_component(NAME_EXECUTABLE ${CMAKE_CURRENT_SOURCE_DIR} NAME)
#glob all path to folder in task_* an

#save all folders in tasks with prefix task_ to array
file(GLOB TASKS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/task_*)
#find all files in folder with name *.c and *.h and *.cpp *.hpp and add them to the project

foreach(TASK ${TASKS})
file(GLOB_RECURSE SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/${TASK} ${CMAKE_CURRENT_SOURCE_DIR}/${TASK}/*.c ${CMAKE_CURRENT_SOURCE_DIR}/${TASK}/*.h ${CMAKE_CURRENT_SOURCE_DIR}/${TASK}/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/${TASK}/*.hpp)
endforeach ()
message(STATUS "TASKS: ${TASKS}")
message(STATUS "FIND IN: " ${TASK})
file(GLOB FILES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/${TASK}/*.[ch]pp
${CMAKE_CURRENT_SOURCE_DIR}/${TASK}/*.h
${CMAKE_CURRENT_SOURCE_DIR}/${TASK}/*.c)
list(APPEND SOURCES ${FILES})
endforeach()
message(STATUS "SOURCES: ${SOURCES}")
add_executable(${NAME_EXECUTABLE} ${SOURCES})
target_link_libraries(
Expand Down
8 changes: 8 additions & 0 deletions tasks/task_basic_c/task_int2str/int2str.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <assert.h>
#include "int2str.h"
#include "stdio.h"

char* int2str(int number) {
char* str = "0";
return str;
}
15 changes: 15 additions & 0 deletions tasks/task_basic_c/task_int2str/int2str.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#ifdef __cplusplus
extern "C" {
#endif

char* int2str(int number);

#ifdef __cplusplus
}
#endif




22 changes: 22 additions & 0 deletions tasks/task_basic_c/task_int2str/int2str_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <gtest/gtest.h>
#include "int2str.h"

TEST(int2str, BasicTest) {
EXPECT_STREQ(int2str(0), "0");
}

TEST(int2str, BasicTestZero) {
EXPECT_STREQ(int2str(0), "0");
}

TEST(int2str, BasicTestZeroNext) {
EXPECT_STREQ(int2str(-0), "0");
}

TEST(int2str, BasicTestNumbers) {
EXPECT_STREQ(int2str(-5), "-5");
EXPECT_STREQ(int2str(-66), "-66");
EXPECT_STREQ(int2str(-123), "-123");
EXPECT_STREQ(int2str(2147483647), "2147483647");
EXPECT_STREQ(int2str(-2147483648), "-2147483648");
}
20 changes: 1 addition & 19 deletions tasks/task_basic_c/task_str2int/str2int.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,5 @@
#include "stdio.h"

int str2int(const char *str) {
int znak = 1;
int result = 0;

if (*str == '-'){
znak = -1;
str++;
} else if (*str == '+') {
str++;
}

int count = 0;
while (*str != '\0') {
result = result * 10 + (*str - '0');
count += 1;
str++;
assert(znak == -1 && result == -2147483648 || znak == -1 && result >= 0 && count <= 9 || znak == 1 && result >= 0);
}
assert(count > 0);
return result * znak;
return 0;
}
1 change: 0 additions & 1 deletion tasks/task_basic_c/task_str2int/str2int_tests.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include <gtest/gtest.h>
#include "str2int.h"


TEST(str2int, BasicTest) {
EXPECT_EQ(str2int("0"), 0);
}
Expand Down

0 comments on commit 54ef99f

Please sign in to comment.