Running Nuke inside a GitHub Action

Raul NaupariRaul Naupari
3 min read

This post is the official continuation of the post, Nuke: Deploy ASP. NET Web App to Azure (try to check it before and download the initial code from here). Here we'll see how easy it's to run Nuke inside a GitHub Action. We'll create two workflows, the first to compile the application on every push and the other to deploy the application on demand. We can generate the workflow files by adding the GitHubActions attribute at the top of the Build class:

[GitHubActions(
    "compile",
    GitHubActionsImage.UbuntuLatest,
    OnPushBranches = new[] { "main" },
    InvokedTargets = new[] { nameof(Compile) })]

Let's run the nuke --help command to generate the compile.yml workflow file(actually, every execution of the nuke command will generate the file):

name: compile

on:
  push:
    branches:
      - main

jobs:
  ubuntu-latest:
    name: ubuntu-latest
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - name: Cache .nuke/temp, ~/.nuget/packages
        uses: actions/cache@v2
        with:
          path: |
            .nuke/temp
            ~/.nuget/packages
          key: ${{ runner.os }}-${{ hashFiles('**/global.json', '**/*.csproj') }}
      - name: Run './build.cmd Compile'
        run: ./build.cmd Compile

In the second workflow, we want to generate an artifact as part of the execution. Change the Zip target as follows:


    Target Zip => _ => _
        .DependsOn(Publish)
        .Produces(ArtifactDirectory / "*.zip")
        .Executes(() =>
        {
            ZipFile.CreateFromDirectory(OutputDirectory, ArtifactDirectory / "deployment.zip");
        });

The attribute to add will be:

[GitHubActions(
    "deploy",
    GitHubActionsImage.UbuntuLatest,
    On = new[] { GitHubActionsTrigger.WorkflowDispatch },
    InvokedTargets = new[] { nameof(Deploy) },
    ImportSecrets = new[] { nameof(WebAppPassword)},
    AutoGenerate = false)]

In this case, the attribute has the AutoGenerate set to false because we want to control the generation explicitly by running the following command nuke --generate-configuration GitHubActions_deploy --host GitHubActions:

name: deploy

on: [workflow_dispatch]

jobs:
  ubuntu-latest:
    name: ubuntu-latest
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - name: Cache .nuke/temp, ~/.nuget/packages
        uses: actions/cache@v2
        with:
          path: |
            .nuke/temp
            ~/.nuget/packages
          key: ${{ runner.os }}-${{ hashFiles('**/global.json', '**/*.csproj') }}
      - name: Run './build.cmd Deploy'
        run: ./build.cmd Deploy
        env:
          WebAppPassword: ${{ secrets.WEB_APP_PASSWORD }}
      - uses: actions/upload-artifact@v1
        with:
          name: artifact
          path: artifact

Modify the deploy.yml file to complete the parameters needed by the Deploy target as follow:

name: deploy

on: [workflow_dispatch]

jobs:
  ubuntu-latest:
    name: ubuntu-latest
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - name: Cache .nuke/temp, ~/.nuget/packages
        uses: actions/cache@v2
        with:
          path: |
            .nuke/temp
            ~/.nuget/packages
          key: ${{ runner.os }}-${{ hashFiles('**/global.json', '**/*.csproj') }}
      - name: Run './build.cmd Deploy'
        run: ./build.cmd Deploy
        env:
          WebAppPassword: ${{ secrets.WEB_APP_PASSWORD }}
          WebAppUser: $nuke-sandbox-app
          WebAppName: nuke-sandbox-app
      - uses: actions/upload-artifact@v1
        with:
          name: artifact
          path: artifact

By default on Windows, the build.cmd and build.sh are not recognized as executable. We can set the executable flag on any file using the update-index git sub-command:

git update-index --chmod=+x .\build.sh
git update-index --chmod=+x .\build.cmd

Go to GitHub and add the WEB_APP_PASSWORD secret:

Push all the files to GitHub to start seeing the workflows in action:

Run the deploy workflow to see the following output:

You can find the final code here. Thanks, and happy coding.

1
Subscribe to my newsletter

Read articles from Raul Naupari directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Raul Naupari
Raul Naupari

Somebody who likes to code