CI/CD Automation Scripts

The project at screenly/balena-prometheus-exporter does not currently have a built CI/CD pipeline established. This limits automated deployment and integration processes for code changes.

Next Steps:

To implement CI/CD automation, consider the following steps and recommendations:

  1. Set Up a CI/CD Tool: Choose a CI/CD platform such as GitHub Actions, GitLab CI, Travis CI, or CircleCI.

  2. Create Configuration Files: Depending on the selected CI/CD tool, create a configuration file in the root directory of the repository.

    For example, using GitHub Actions, create a file named .github/workflows/ci.yml:

    name: CI Pipeline
    
    on:
      push:
        branches:
          - main
    
    jobs:
      build:
        runs-on: ubuntu-latest
    
        steps:
          - name: Checkout code
            uses: actions/checkout@v2
    
          - name: Set up Python
            uses: actions/setup-python@v2
            with:
              python-version: '3.10'
    
          - name: Install dependencies
            run: |
              python -m pip install --upgrade pip
              pip install -r requirements.txt
    
          - name: Run tests
            run: |
              python -m unittest discover -s tests
    
  3. Add Docker Support: Given that the project relies on Docker, include steps in the CI/CD process to build and push Docker images to a container registry.

    Extend the example above to include Docker build and push commands:

          - name: Build Docker image
            run: |
              docker build -t yourusername/balena-exporter .
    
          - name: Log in to Docker Hub
            run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login --username yourusername --password-stdin
    
          - name: Push Docker image
            run: |
              docker push yourusername/balena-exporter
    
  4. Environment Handling: Handle environment variables securely. In GitHub Actions, you can add them as secrets under repository settings. This can include BALENA_TOKEN and CRAWL_INTERVAL.

  5. Testing: Utilize the existing unit tests defined in tests/test_exporter.py to validate the functionality. Ensure the tests are invoked in the CI process:

    import unittest
    from main import BalenaCollector
    
    class TestBalenaCollector(unittest.TestCase):
        def setUp(self):
            self.collector = BalenaCollector()
    
        def test_collect(self, mock_metrics):
            with mock.patch.object(self.collector, "get_balena_fleets") as mock_fleets:
                mock_fleets.return_value = ["fleet1", "fleet2"]
                mock_metrics.side_effect = [("test_fleet", 3), ("test_fleet", 3)]
    
                result = list(self.collector.collect()[0].samples)
                expected = [('balena_devices_online', {'fleet_name': 'test_fleet'}, 3),
                            ('balena_devices_online', {'fleet_name': 'test_fleet'}, 3)]
    
                result = [(s.name, s.labels, s.value) for s in result]
    
                self.assertEqual(result, expected)
    
    if __name__ == "__main__":
        unittest.main()
    

By following these steps, a fully automated CI/CD pipeline can be established for the screenly/balena-prometheus-exporter project, ensuring efficient integration and deployment processes.

Source:

  • Dockerfile
  • README.md
  • main.py
  • requirements.txt
  • tests/test_exporter.py