Embarking on your GitHub Actions journey, you've likely come across the need to obtain the build number. This can be incredibly useful for versioning, artifact identification, or keeping a build history. In this guide, we'll explore how to achieve this using GitHub Actions features and best practices.

GitHub Actions offers a wealth of context and data for each running job, allowing you to access information like run numbers, event types, and more. Among these, retrieving the build number, or run number, can be easily accomplished using built-in environment variables.

Understanding GitHub Actions Environment Variables
GitHub Actions provides a plethora of environment variables that you can leverage to access run metadata. These variables are prefixed with `GITHUB_` and can be used directly in your workflow files without any additional setup.

To learn more about these environment variables and their possible values, you can refer to the official GitHub documentation: https://docs.github.com/en/actions/learn-github-actions/environment-variables
Finding the Run Number

The run number, also known as the build number, can be accessed using the `GITHUB_RUN_NUMBER` environment variable. This variable contains the number of times a workflow has been run in the repository. It increments by one each time the workflow is triggered.
For example, if you've run your workflow 5 times, the `GITHUB_RUN_NUMBER` will be `6` for the next run, as it starts counting from 1.
Accessing the Run Number in Your Workflow

To use the `GITHUB_RUN_NUMBER` in your workflow, you simply need to echo or print its value in your desired format. Let's see how you can achieve this using different programming languages.
For Bash scripts, you can use the `echo` command:
```bash echo "Build Number: ${{ env.GITHUB_RUN_NUMBER }}" ```
For PowerShell scripts, you can use the Write-Output cmdlet:

```powershell Write-Output "Build Number: $env:GITHUB_RUN_NUMBER" ```
For other programming languages or tools, you can use their respective execution methods to print or use the `GITHUB_RUN_NUMBER` in your operations.
Versioning Artifacts Using the Build Number




















Now that you've obtained the build number, you can put it to good use, such as versioning your artifacts. This helps in tracking and identifying specific builds and their outputs.
When publishing artifacts, you can append the build number to your artifact name to ensure uniqueness. Here's a simple example using an upload artifact action:
`actions/upload-artifact@v2`
```yaml - name: Upload Artifact uses: actions/upload-artifact@v2 with: name: MyArtifact-${{ env.GITHUB_RUN_NUMBER }} path: ./path/to/my/artifact ```
In this example, the uploaded artifact will be named `MyArtifact-{build_number}`.
Displaying the Build Number in Logs or Notifications
You can also display the build number in your workflow logs or send it as a notification to help keep track of your running jobs. To do this, you can use the `run` command in your workflow file or send a message to a notification channel like Slack or Microsoft Teams.
For example, to send a message to Slack, you can use the `slack pans_WITH_github_action` action:
`Firebase/slack-action`
```yaml - name: Send Slack Notification uses: Firebase/slack-action@v1 env: SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }} with: args: "The build number is ${{ env.GITHUB_RUN_NUMBER }}" ```
Remember to replace `SLACK_BOT_TOKEN` with the name of your Slack bot token secret.
By following these steps, you'll be able to effectively obtain and utilize the build number in your GitHub Actions workflows. This information can then be used to version artifacts, track builds, and enhance your overall CI/CD experience.
As you explore GitHub Actions further, you'll find that originality and adaptability are key to unlocking its full potential. So, go ahead, experiment, and build!