diff --git a/.github/workflows/ci-cpp-build.yml b/.github/workflows/ci-cpp-build.yml index e67cfaf..b4042a8 100644 --- a/.github/workflows/ci-cpp-build.yml +++ b/.github/workflows/ci-cpp-build.yml @@ -1,4 +1,4 @@ -name: C++ Build (Сборка проекта) +name: C++ Build and Run (Сборка и запуск проекта) on: push: @@ -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/compose-action@v2.0.1 + # 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 diff --git a/docker-compose.yml b/docker-compose.yml index 8b60ba8..4cad880 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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 \ No newline at end of file diff --git a/docker/image/Dockerfile b/docker/image/Dockerfile index b307941..8647ad0 100644 --- a/docker/image/Dockerfile +++ b/docker/image/Dockerfile @@ -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 @@ -16,5 +16,3 @@ EXPOSE 22 RUN echo "StrictHostKeyChecking=no" >> /etc/ssh/ssh_config RUN mkdir /var/run/sshd CMD ["/usr/sbin/sshd", "-D"] - - diff --git a/tasks/task_basic_c/CMakeLists.txt b/tasks/task_basic_c/CMakeLists.txt index cadb18d..9937087 100644 --- a/tasks/task_basic_c/CMakeLists.txt +++ b/tasks/task_basic_c/CMakeLists.txt @@ -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( diff --git a/tasks/task_basic_c/task_int2str/int2str.c b/tasks/task_basic_c/task_int2str/int2str.c new file mode 100644 index 0000000..3536270 --- /dev/null +++ b/tasks/task_basic_c/task_int2str/int2str.c @@ -0,0 +1,8 @@ +#include +#include "int2str.h" +#include "stdio.h" + +char* int2str(int number) { + char* str = "0"; + return str; +} diff --git a/tasks/task_basic_c/task_int2str/int2str.h b/tasks/task_basic_c/task_int2str/int2str.h new file mode 100644 index 0000000..cd00dea --- /dev/null +++ b/tasks/task_basic_c/task_int2str/int2str.h @@ -0,0 +1,15 @@ +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +char* int2str(int number); + +#ifdef __cplusplus +} +#endif + + + + diff --git a/tasks/task_basic_c/task_int2str/int2str_tests.cpp b/tasks/task_basic_c/task_int2str/int2str_tests.cpp new file mode 100644 index 0000000..e47cdc0 --- /dev/null +++ b/tasks/task_basic_c/task_int2str/int2str_tests.cpp @@ -0,0 +1,22 @@ +#include +#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"); +} diff --git a/tasks/task_basic_c/task_str2int/str2int.c b/tasks/task_basic_c/task_str2int/str2int.c index 83e564e..b03c18d 100644 --- a/tasks/task_basic_c/task_str2int/str2int.c +++ b/tasks/task_basic_c/task_str2int/str2int.c @@ -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; } diff --git a/tasks/task_basic_c/task_str2int/str2int_tests.cpp b/tasks/task_basic_c/task_str2int/str2int_tests.cpp index 5ec93d1..d73ce0c 100644 --- a/tasks/task_basic_c/task_str2int/str2int_tests.cpp +++ b/tasks/task_basic_c/task_str2int/str2int_tests.cpp @@ -1,7 +1,6 @@ #include #include "str2int.h" - TEST(str2int, BasicTest) { EXPECT_EQ(str2int("0"), 0); }