|
1 |
| -# github-actions-compile-c |
| 1 | +# GitHub Actions to Compile C Programs |
| 2 | + |
| 3 | +This repository demonstrates how to use GitHub Actions to automatically compile and test a simple C program every time you push or create a pull request to the `main` branch. The action installs GCC, compiles the C program, and runs it, ensuring that the program is functional. |
| 4 | + |
| 5 | +## License |
| 6 | + |
| 7 | +MIT License |
| 8 | +Copyright (c) 2025 Max Base |
| 9 | + |
| 10 | +## Usage |
| 11 | + |
| 12 | +### 1. Clone the Repository |
| 13 | + |
| 14 | +To get started, clone this repository to your local machine: |
| 15 | + |
| 16 | +```bash |
| 17 | +git clone https://github.com/BaseMax/github-actions-compile-c.git |
| 18 | +``` |
| 19 | + |
| 20 | +### 2. GitHub Action Setup |
| 21 | + |
| 22 | +The GitHub Action is configured to automatically compile the C program every time a change is pushed to the main branch or a pull request is created. |
| 23 | + |
| 24 | +Create a new file in the `.github/workflows` directory: |
| 25 | + |
| 26 | +- File name: `c-compile.yml` |
| 27 | +- Action Setup: The action is defined in the `c-compile.yml` file, which runs the following steps: |
| 28 | + - Checkout code using the actions/checkout@v3 action. |
| 29 | + - Install GCC using the apt-get package manager on Ubuntu. |
| 30 | + - Compile the C program using gcc -o my_program main.c. |
| 31 | + - Run the compiled program and ensure that it works. |
| 32 | + |
| 33 | +### 3. Example C Program |
| 34 | + |
| 35 | +The program compiled in this repository is a simple C program that calculates the factorial of a number. |
| 36 | + |
| 37 | +**Example C Program (main.c):** |
| 38 | + |
| 39 | +```c |
| 40 | +#include <stdio.h> |
| 41 | + |
| 42 | +int factorial(int n) { |
| 43 | + if (n == 0) return 1; |
| 44 | + return n * factorial(n - 1); |
| 45 | +} |
| 46 | + |
| 47 | +int main() { |
| 48 | + printf("Hello, GitHub Actions!\n"); |
| 49 | + |
| 50 | + int num = 5; |
| 51 | + printf("Factorial of %d is %d\n", num, factorial(num)); |
| 52 | + |
| 53 | + return 0; |
| 54 | +} |
| 55 | +``` |
| 56 | +
|
| 57 | +### 4. Running the GitHub Action |
| 58 | +
|
| 59 | +Whenever you push to the main branch or create a pull request, the GitHub Action will automatically: |
| 60 | +
|
| 61 | +- Check out the code. |
| 62 | +- Set up the GCC compiler. |
| 63 | +- Compile the program. |
| 64 | +- Run the program to verify that the compilation was successful. |
| 65 | +- You can view the results of the action under the Actions tab of the GitHub repository. |
| 66 | +
|
| 67 | +### GitHub Action Workflow |
| 68 | +
|
| 69 | +The workflow for compiling the C program is defined in the .github/workflows/c-compile.yml file. |
| 70 | +
|
| 71 | +```yaml |
| 72 | +name: Compile and Test C Program |
| 73 | +
|
| 74 | +on: |
| 75 | + push: |
| 76 | + branches: |
| 77 | + - main |
| 78 | + pull_request: |
| 79 | + branches: |
| 80 | + - main |
| 81 | +
|
| 82 | +jobs: |
| 83 | + build: |
| 84 | + runs-on: ubuntu-latest |
| 85 | +
|
| 86 | + steps: |
| 87 | + - name: Checkout code |
| 88 | + uses: actions/checkout@v3 |
| 89 | +
|
| 90 | + - name: Set up GCC |
| 91 | + run: sudo apt-get install -y gcc |
| 92 | +
|
| 93 | + - name: Compile C program |
| 94 | + run: gcc -o my_program main.c |
| 95 | +
|
| 96 | + - name: Run tests |
| 97 | + run: | |
| 98 | + if ./my_program; then |
| 99 | + echo "Test Passed" |
| 100 | + else |
| 101 | + echo "Test Failed" |
| 102 | + exit 1 |
| 103 | + fi |
| 104 | +``` |
| 105 | + |
| 106 | +### Workflow Explanation: |
| 107 | + |
| 108 | +- **Checkout code:** The actions/checkout action checks out the code from the repository. |
| 109 | +- **Set up GCC:** Installs GCC (GNU Compiler Collection) to compile the C program. |
| 110 | +- **Compile C program:** The gcc command compiles the main.c file into an executable named my_program. |
| 111 | +- **Run tests:** The program is run, and if the execution is successful, it outputs "Test Passed"; otherwise, it outputs "Test Failed" and exits with a failure code. |
| 112 | + |
| 113 | +## Contributing |
| 114 | + |
| 115 | +If you'd like to contribute to this project, feel free to fork the repository, create a pull request with your changes, and submit it for review. |
| 116 | + |
| 117 | +## License |
| 118 | + |
| 119 | +This project is licensed under the MIT License - see the LICENSE file for details. |
| 120 | + |
| 121 | +Max Base, 2025 |
0 commit comments