Using GitHub Secrets and Environment Variables

Using GitHub Secrets and Environment Variables


Today, software development processes are largely carried out within the framework of DevOps practices. Version control systems play an important role in ensuring these processes run according to a specific standard. GitHub, with its Secrets and Environment Variables features, helps to increase the security of projects in this field. In this article, we will detail how to use these two features on GitHub.

What are GitHub Secrets?

GitHub Secrets is a mechanism that ensures the secure storage of sensitive information (e.g., API keys, passwords) in your repository. Secrets can be used in CI/CD processes and for data sharing between integrated applications with features. In this way, as sensitive information is not directly present in the code, a high level of security is ensured.

How to Create GitHub Secrets?

Creating a secret in a GitHub repository is quite simple. Here is an example showing how to do it step by step:


# First go to your repository on GitHub
# Click on the Settings tab
# Under Secrets and variables, click on 'Actions'
# Click on the New repository secret button to create a new secret
# After filling in the Name and Value fields, click the Add secret button

What are Environment Variables?

Environment variables are used to define custom information for your applications during runtime. This allows developers to manage certain conditions in their applications and provides flexibility to switch between configurations. Combined with GitHub Actions, it allows different arrangements for different specified environments.

How to Use Environment Variables?

To use environment variables, you should define them in your GitHub Actions workflow as follows:


name: CI/CD Workflow
on: [push]
jobs:
  build:
    runs-on: ubuntu-latest
    env:
      MY_ENV_VAR: ${{ secrets.MY_SECRET }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - name: Run my script
        run: echo "The value is $MY_ENV_VAR"

Conclusion

GitHub Secrets and Environment Variables are of critical importance for secure and effective software development processes. While these two features help protect sensitive information, they also increase the flexibility of your application. By using this practical information on GitHub, you can make your projects more secure. We recommend reviewing the GitHub documentation to learn more about Secrets and Environment Variables.