Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Helpers for GitHub Actions #2609

Open
xt0rted opened this issue Aug 21, 2019 · 29 comments
Open

Helpers for GitHub Actions #2609

xt0rted opened this issue Aug 21, 2019 · 29 comments
Assignees

Comments

@xt0rted
Copy link

xt0rted commented Aug 21, 2019

Now that GitHub Actions supports CI/CD it'd be nice to have some helpers to detect if you're running in an actions workflow like we have for other providers.

There's a list of environment variables that are set so it should be pretty straight forward to do the check and surface the basic info. https://help.github.com/en/articles/virtual-environments-for-github-actions#default-environment-variables

I'm not sure if we'll be able to setup helpers to upload artifacts (for now). There's an action for this but it seems like it references something installed on the VM instead of calling an api endpoint or writing to the log. https://github.com/actions/upload-artifact

Log messages can also be adjusted by prefixing them with ##[debug], ##[warning] , and##[error]. This will highlight them in the build output. There's no docs on this yet, but you can see how it's done in these two files https://github.com/actions/toolkit/tree/master/packages/core/src.

Note: the format for these just changed to :: instead of ## according to the comments in actions/toolkit#57

Similar to VS Code there's also support for problem matchers, so if we wanted to highlight specific lines from cake's output we could do that by bundling those files alongside cake and registering them if we're in an actions workflow. This could be useful for highlighting the version of cake being used, or any warnings it may give about mismatched versions.

I'm slowly migrating all of my builds over to actions so I'm happy to help add this in and test it.

@devlead
Copy link
Member

devlead commented Aug 21, 2019

Agreed, this would be something we would want to support.

@gep13
Copy link
Member

gep13 commented Aug 21, 2019

@xt0rted do you have any examples of projects which you are porting to GitHub Actions CI/CD?

@xt0rted
Copy link
Author

xt0rted commented Aug 21, 2019

@gep13 I don't have any public .net projects right now, but I could setup a test one if you'd like.

Here's the workflows for a node action I'm migrating along with the build output.

This is one of my master branch workflows that's building, packaging, and publishing to azure app services. It's being published as s self-contained app so it's using the Windows VM but it could also use Ubuntu or OSX.

name: master

on:
  push:
    branches: master

jobs:
  build:
    runs-on: windows-latest

    steps:
      - name: Checkout repo
        uses: actions/checkout@master

      - name: Setup .NET Core
        uses: actions/setup-dotnet@v1
        with:
          version: 3.0.100-preview8-013656

      - name: Build Site
        run: dotnet build --configuration Release

      - name: Publish Site To Artifacts Folder
        env:
          DOTNET_CONFIGURE_AZURE: 1
        run: dotnet publish --configuration Release --output artifacts

      - name: Package Artifacts
        run: Compress-Archive -Path artifacts\* -DestinationPath artifacts
        shell: powershell

      - name: Publish Artifacts
        uses: actions/upload-artifact@v1
        with:
          name: artifacts.zip
          path: artifacts.zip

      - name: Publish To Azure
        uses: azure/appservice-actions/webapp@master
        with:
          app-name: app-name
          package: artifacts.zip
          publish-profile: ${{ secrets.azureWebAppPublishProfile }}

@gep13
Copy link
Member

gep13 commented Aug 21, 2019

@xt0rted thanks for that! An example would be good. I am just curious about "how" Cake is being used in the wild with regard to GitHub Actions CI/CD, to better understand how we can support it, i.e. where it makes sense to get involved, what we leave up to the consumer, etc.

@devlead
Copy link
Member

devlead commented Aug 21, 2019

Guess anything after Setup .NET Core could be replaced with

dotnet tool install -g Cake.Tool
dotnet cake

which we probably could provide a GitHub action for that would work xplat.

@xt0rted
Copy link
Author

xt0rted commented Aug 21, 2019

@gep13 I'll be moving projects from both AppVeyor and Azure DevOps. In those builds I'm using the following:

var buildNumber = EnvironmentVariable<int>("BUILD_BUILDID", 0);
var prNumber = EnvironmentVariable<int>("SYSTEM_PULLREQUEST_PULLREQUESTNUMBER", 0);

Task("PatchAssemblyInfo")
    .WithCriteria(TFBuild.IsRunningOnAzurePipelinesHosted)
    .Does(() => { ... });
var buildNumber = EnvironmentVariable<int>("APPVEYOR_BUILD_NUMBER", 0);

var prNumber = EnvironmentVariable("APPVEYOR_PULL_REQUEST_NUMBER");

if (AppVeyor.IsRunningOnAppVeyor)
{
    AppVeyor.UpdateBuildVersion(version);
}

Information($"Build version: {version}");
Information($"Version suffix: {versionSuffix}");

The main thing I was thinking about carrying over was a check to see if you're running in an action, along with the other environment variables where appropriate. You can also access the full json payload that triggered the workflow by reading the file from the GITHUB_EVENT_PATH env var. That can have more info like PR number, etc.

I don't think actions themselves are really relevant to Cake, they're just another build platform to surface information from.

You can run scripts as job steps now so to call your build script it's as simple as:

steps:
  - name: Build Project
    run: ./build.ps1
    shell: powershell

  - name: Build Project
    run: ./build.sh
    shell: bash

If an action uses a Docker container then it always runs on Linux regardless of the host OS, so I don't think there's need for a Cake action like the DevOps task. Maybe a Javascript based action could work but when you can call the script so easily it seems like a lot of work for little gain.

@gep13
Copy link
Member

gep13 commented Aug 21, 2019

@devlead said...
which we probably could provide a GitHub action for that would work xplat.

This is sort of what I was getting at. i.e. does it make sense to have a setup-cake action, similar to setup-dotnet

@gep13
Copy link
Member

gep13 commented Aug 21, 2019

@xt0rted said...
If an action uses a Docker container then it always runs on Linux regardless of the host OS, so I don't think there's need for a Cake action like the DevOps task. Maybe a Javascript based action could work but when you can call the script so easily it seems like a lot of work for little gain.

I toyed with a Cake Action in the first release of GitHub Actions, that used the Docker Image that we have for Cake, so you could run that directly, rather than having to do any other bootstrapping, as it was already there. If we continued with that, we would need to update it to the new format, but again, trying to understand "how" we should be involved, and what we should leave up to the creator of the workflow.

@xt0rted
Copy link
Author

xt0rted commented Aug 21, 2019

A setup-cake action that was js based could work since that would be cross platform. What would that give you over just calling the build script directly though?

Later this week I'll be migrating my first cake based project over to actions. Once that's done I'll have a better idea of how the out of the box experience is.

@devlead
Copy link
Member

devlead commented Aug 21, 2019

V2 Has changed actions quite a bit, need to play with it more, but from perf perspective using the global tool could make more sense for some scenarios. And container in others. So we should probably do both.

actions/setup-dotnet@v1 essentially is https://github.com/actions/setup-dotnet/tree/v1.0.2

@xt0rted
Copy link
Author

xt0rted commented Aug 29, 2019

Here's a quick demo using actions to build a .net core site using cake and pushing it to azure app services.

https://github.com/xt0rted/actions-cake-demo

There's two workflows:

  1. Build and package on pushes
  2. Build, package, and deploy on pushes to master

This is the run that did the deploy https://github.com/xt0rted/actions-cake-demo/runs/206324261

The site is using .net core 3.0 preview 8 and is being built on a windows vm targeting win10-x86 and being deployed as a self-contained application using zip deploy and run from package.

@gep13
Copy link
Member

gep13 commented Aug 29, 2019

@xt0rted this is very cool, thanks for sharing!

Would be interesting to see if you could do a "staged" deployment. i.e. run the build and generate the artifact, then at some point in the future, use the same artifact to deploy the site, rather than having to run all the build steps again.

@gitfool
Copy link
Contributor

gitfool commented Sep 16, 2019

FWIW, I've ported all my Cake related projects to build with both Azure Pipelines and GitHub Actions.

Cake.Dungeon builds on Linux, Mac, Windows, and Docker, and dumps all environment variables, which helps verify expected values when putting together a build provider:

I also refactored the Azure Pipelines config to match with the GitHub Actions config so you can clearly see the heritage:

I hit one issue though; actions/setup-dotnet@v1 seems to be broken on Linux and Mac as it probably misconfigures DOTNET_ROOT, so I'm using a temporary work around.

@ecampidoglio
Copy link
Member

If anyone is looking for a GitHub Action for Cake, there's one right over here. 🙂

@gitfool
Copy link
Contributor

gitfool commented Nov 30, 2019

We also need a build provider. See #2678. 😉

@soroshsabz
Copy link
Contributor

soroshsabz commented Mar 23, 2020

I think we need some addins for supporting GitHub Actions API in cake, like AppVeyor, and add native support for all GitHub Actions built-in uses in Cake DSL

@gitfool

@devlead
Copy link
Member

devlead commented Mar 23, 2020

@soroshsabz there's a build provider available now, so commands could be added to it.

@soroshsabz
Copy link
Contributor

@devlead thanks for response, did you can add some example or documents in https://cakebuild.net/dsl/
for working with GitHub Actions ?

@devlead
Copy link
Member

devlead commented Mar 23, 2020

Sure documented under
https://cakebuild.net/dsl/build-system/
You can access it using the global GitHubActions property i.e.

GitHubActions.IsRunningOnGitHubActions

many of the properties on i.e. GitHubActionsEnvironmentInfo have code samples in the documentation.

@soroshsabz
Copy link
Contributor

soroshsabz commented Mar 23, 2020

@devlead Ok, thanks a lot for awesome helping, but I couldn't find something like AppVeyor.UploadArtifact(); or AppVeyor.UploadTestResults equivalent for GitHub Actions

I think IGitHubActionsProvider must have more functions

@devlead
Copy link
Member

devlead commented Mar 23, 2020

Correct that's why I wrote commands that could be added to it. So currently it's up for grabs.

@soroshsabz
Copy link
Contributor

soroshsabz commented Mar 23, 2020

thanks a lot, another point is I think it is good to provide official GitHub Action instead of https://github.com/marketplace/actions/cake-action

@ecampidoglio
Copy link
Member

@soroshsabz The cake-action is currently being used to build Cake itself. What would you like to see in an "official" GitHub Action that's not already there?

@ecampidoglio
Copy link
Member

📢 The cake-action is now officially part of the Cake organization.

https://github.com/cake-build/cake-action

@ecampidoglio
Copy link
Member

If no one else is working on this, I'll gladly take it on.

@devlead
Copy link
Member

devlead commented Jul 3, 2020

@ecampidoglio I've assigned issue to you.

@jasells
Copy link

jasells commented Mar 14, 2024

I am trying to use context.GitHubActions().Commands.UploadArtifact , but get

Cake.Core.CakeException: GitHub Actions Runtime Token missing.
     at Cake.Common.Build.GitHubActions.Commands.GitHubActionsCommands.ValidateArtifactParameters[T](T path, String artifactName)
     at Cake.Common.Build.GitHubActions.Commands.GitHubActionsCommands.UploadArtifact(DirectoryPath path, String artifactName)

Is there any documentation/example on how to set up the token so i can use this from cake frosting?

@devlead
Copy link
Member

devlead commented Mar 19, 2024

@jasells You'll need to use Cake action to access GitHub APIs.

Until cake-build/cake-action#43 is resolved you'll need to create a minimal Cake script bootstrapper.

I've created an example of that here: https://github.com/devlead/BootstrapFrostingAction

@jasells
Copy link

jasells commented Mar 19, 2024

@devlead

You'll need to use Cake action to access GitHub APIs.

Ahhh, I see. TY for the example, I had also been wondering how to specify the target task.

Just for clarity of anyone else reading this, make sure to check the build.yml file here for the key portion of the bootstrapper in the example. Note the explicit task target arg, and that it runs after the "Default" task.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants