Author – Shefali Kamble, Cloud Engineer
Overview
GitHub Actions is a CI/CD and general automation system introduced by GitHub. It is integrated right into GitHub and enabled by default in every GitHub repository. GitHub Actions help you automate tasks within your software development life cycle. It is event-driven, which means that we can run a series of commands after a specified event has occurred.
How GitHub Actions are automated?
Every time someone creates a pull request for a repository, you can automatically run a command that executes a software testing script. An event then automatically triggers the workflow, which contains a job. The job then uses steps to control the order in which actions are run. These actions are the commands that automate your software testing.
Getting Started with GitHub Actions
Prerequisites
- An Azure account with an active subscription. Create an account for free.
- A GitHub account. If you don’t have one, sign up for free.
- A working Azure App Service app.
- .NET: Create an ASP.NET Core web app in Azure
Workflow file overview
A workflow is defined by a YAML (.yml) file in the /.github/workflows/ path in your repository. This definition contains the various steps and parameters that make up the workflow.
Setting up the Workflow File
There are two ways to create a workflow file:
- Use the Deployment Center in Azure Portal
- Set up the workflow manually
Use the Deployment Center in Azure Portal
You can get started with GitHub Actions by using the App Service Deployment Center. This will automatically generate a workflow file based on your application stack and commit it to your GitHub repository in the correct directory.
- Go to Azure Portal and select your App service. In the left blade under Deployment select Deployment Center.
- In the source select GitHub and authorize your account.
- Fill in all the details under GitHub and Verify the configuration by clicking on Preview File and then click on Save.
- Go the GitHub repository and click on Actions tab, there you will find the running workflow. You can view all the tasks as it runs.
Once completed you can navigate to the Azure App Service URL and view the web page.
Set up a workflow manually
You can also deploy a workflow without using the Deployment Center. To do so, you will need to first generate deployment credentials.
Generate deployment credentials
A publish profile is an app-level credential. Set up your publish profile as a GitHub secret.
-
- Go to your app service in the Azure portal. On the Overview page, select Get Publish profile.
- Save the downloaded file. You’ll use the contents of the file to create a GitHub secret.Configure the GitHub secret
- In GitHub, browse your repository, select Settings > Secrets > Add a new secret.To use app-level credentials, paste the contents of the downloaded publish profile file into the secret’s value field. Name the secret AZURE_WEBAPP_PUBLISH_PROFILE.
- Create a new workflow file and paste in the code.
name: .NET Core CI on: [push] env: AZURE_WEBAPP_NAME: your-app-name # set this to your application’s name AZURE_WEBAPP_PACKAGE_PATH: ‘.’ # set this to the path to your web app project, defaults to the repository root DOTNET_VERSION: ‘3.1.x’ # set this to the dot net version to use jobs: build: runs-on: ubuntu-latest steps: # Checkout the repo – uses: actions/checkout@main # Setup .NET Core SDK – name: Setup .NET Core uses: actions/setup-dotnet@v1 with: dotnet-version: ${{ env.DOTNET_VERSION }} # Run dotnet build and publish – name: dotnet build and publish run: | dotnet restore dotnet build –configuration Release dotnet publish -c Release -o ‘${{ env.AZURE_WEBAPP_PACKAGE_PATH }}/myapp’ # Deploy to Azure Web apps – name: ‘Run Azure webapp deploy action using publish profile credentials’ uses: azure/webapps-deploy@v2 with: app-name: ${{ env.dotnetwebapp59 }} # Replace with your app name publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }} # Define secret variable in repository settings as per action documentation package: ‘${{ env.AZURE_WEBAPP_PACKAGE_PATH }}/myapp’ - Commit the new file and enter the name optionally.
- Go to the Actions tab and see the workflow runs.
- Browse the website URL through the Azure Portal or you can even find it at the end of job completion in the workflow run.
Hope this article was helpful and will guide you to automate your workflow using GitHub Actions. You can post your thoughts/queries in the comment section.