Skip to content

self-hosted runner: always use latest Git for Windows and GitHub Actions version (plus some PowerShell cleanups) #19

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

Merged
merged 13 commits into from
Jan 15, 2023

Conversation

rwidmark
Copy link

@rwidmark rwidmark commented Jan 13, 2023

GitHub git-windows and Action runner

  • Added check as was asked for both ActionRunner and GitHub git-windows
  • Now all data for GitHub git-windows and ActionRunner are stored in the objects $GitHubAction and $GitHubGit
  • Used invoke-restmethod to eliminate overhead and it makes it easier to go over to PSCore later on. Also I added the accept and API version in the header. With this you don't need to convert it from json etc. it's easier.

Ensure that Action runner service was created
Added so it will look if it has been created 3 times 3 seconds apart before it write error as sometime it can take time for the service to be created.

Stop Service, actions.runner.*

  • As you was using * I'm guessing that it can be many services so I did add an foreach loop on them.
  • Inside the foreach it will make sure that each service has been stopped before continuing, it will look 5 times with 5 seconds apart.

Param

  • Moved text to helpmessage
  • Added validation
  • Changed StopService to bool so now you need to write -StopService $false if you don't want to stop the service. Default it's $true as it was from the start.

Error handling

  • Replaced throw with write-error and exit 1 as I'm guessing that you want to exit the script on this errors.

Other

  • Removed $ProgressPreferences where it was not needed.

Got some more things that I can do later on when I have the time, I have tested it locally at the code should work but as I can't test with ActionRunner I'm advising you to test it also.

@rwidmark
Copy link
Author

@dscho forget to req review on this one but when you have the time, take a look at it :)

Copy link
Member

@dscho dscho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wonderful!

I would like to wait for @dennisameling's review before merging this because they are much more familiar with this stuff than I am (I basically only cargo-culted my work on this so far).

Apart from the one change to a [bool] (which I am not so sure about), I only have two minor suggestions (but I won't insist on them): in the Git project, we usually craft commit messages with a lot more information. Oh, and every time I read "GitHub" with a lower-case "h" my brain mis-pronounces that word; PowerShell might be case-insensitive, but I am not 😁

Write-Output "Installing GitHub Actions runner ${GitHubActionsRunnerVersion} as a Windows service with labels ${GithubActionsRunnerLabels}..."
if ((Get-FileHash -Path $GitHubAction.OutFile -Algorithm SHA256).Hash.ToUpper() -ne $GitHubAction.hash) { throw 'Computed checksum did not match' }

Write-Output "Installing GitHub Actions runner $($GitHubAction.Tag) as a Windows service with labels $($GitHubAction.RunnerLabels)..."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really interesting information. Since the post-deployment script is run on Azure, and the GitHub workflow that kicks off that deployment has no connection to the actual deployment, I wonder whether there is any way we could manually mirror it into the GitHub workflow run's logs... @dennisameling any ideas?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what you mean? Do you want to log information from the script? If so, that's possible. I can even make somethign that can send you a email if you want.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oooh, ooh, I think I found something: https://learn.microsoft.com/en-us/powershell/module/az.resources/get-azdeploymentscriptlog?view=azps-9.3.0

Nice try, but Deployment Scripts are something else than the Custom Script Extension 😉

Let's follow up in this PR for getting the post-deployment script output 👍🏼

Robin Stolpe added 4 commits January 14, 2023 13:15
Reverted Parameter StopService to string from boolen.

Changed -notlike to -like in the parameter validation of GitHubActionRunnerRegistrationUrl
Reverted StopService parameter back to string from bool and adjusted the changes in the rest of the code.

Fixed parameter GitHubActionsRunnerRegistrationUrl so it's -like and not -notlike in the validation.
Missed to add validation on StartService parameter when I did revert it back from bool, It's fixed now.
@rwidmark
Copy link
Author

@dscho i know I need to train on my comments on the commits. Same at work. I added some better ones for mistakes I was correcting

I wanted to use switch instead of bool but as I don't know how you execute the script I don't know if switch was good to use or not.

String for true or false is a big no ;) but I did revert it back

I'll adjust the script a little more during the end of next week I have some more adjustments that I want to add to make it even better.

Sorry that my last commits was unsigned I did not have any pgp cert for GitKraken but now I have fixed that too, thought it would use my ssl cert that vs and vscode are using.

@dscho
Copy link
Member

dscho commented Jan 14, 2023

Oh, you don't need to bother with GPG-signing... I was thinking more about giving context in the commit messages, intentions and additional information that's not obvious from reading the diff.

About using [bool]: if this works by providing the command-line parameter as a string, then by all means, let's use the correct data type.

@dennisameling
Copy link
Contributor

dennisameling commented Jan 14, 2023

I don't know how you execute the script I don't know if switch was good to use or not.

We use Microsoft's Azure Custom Script Extension for it. Here's how it's invoked. So I guess that something like this happens behind the scenes:

powershell -ExecutionPolicy Unrestricted -File C:\PATH_TO_SCRIPT\post-deployment-script.ps1 -GitHubActionsRunnerToken ABC123 -GithubActionsRunnerRegistrationUrl https://example.com -GithubActionsRunnerName ABC123 -StopService false -GitHubActionsRunnerPath D:\a

I'll test your changes on a fresh VM later today :) thanks for your work on this!

@dscho dscho changed the title Made some changes self-hosted runner: always use latest Git for Windows and GitHub Actions version (plus some PowerShell cleanups) Jan 14, 2023
@rwidmark
Copy link
Author

I don't know how you execute the script I don't know if switch was good to use or not.

We use Microsoft's Azure Custom Script Extension for it. Here's how it's invoked. So I guess that something like this happens behind the scenes:


powershell -ExecutionPolicy Unrestricted -File C:\PATH_TO_SCRIPT\post-deployment-script.ps1 -GitHubActionsRunnerToken ABC123 -GithubActionsRunnerRegistrationUrl https://example.com -GithubActionsRunnerName ABC123 -StopService false -GitHubActionsRunnerPath D:\a

I'll test your changes on a fresh VM later today :) thanks for your work on this!

I did return it to string but as it look of it I can improve it more now when I know how you execute it.

But test this things out first and after that I during next week improve it more :)

Copy link
Contributor

@dennisameling dennisameling left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just ran the script on a fresh VM. Everything looks great. Also really like how you approached this in PowerShell. Thanks @rstolpe! 🚀

Script log
Starting post-deployment script.
Enabling Windows Developer Mode...
Enabled Windows developer mode.
Adding Microsoft Defender Exclusions...
Finished adding Microsoft Defender Exclusions.
Downloading Git for Windows...
Installing Git for Windows...
Finished installing Git for Windows.
Downloading GitHub Actions runner...
Installing GitHub Actions runner 2.300.2 as a Windows service with labels self-hosted,Windows,ARM64...
Configuring the runner to shut down automatically after running
Configuring the runner

--------------------------------------------------------------------------------
|        ____ _ _   _   _       _          _        _   _                      |
|       / ___(_) |_| | | |_   _| |__      / \   ___| |_(_) ___  _ __  ___      |
|      | |  _| | __| |_| | | | | '_ \    / _ \ / __| __| |/ _ \| '_ \/ __|     |
|      | |_| | | |_|  _  | |_| | |_) |  / ___ \ (__| |_| | (_) | | | \__ \     |
|       \____|_|\__|_| |_|\__,_|_.__/  /_/   \_\___|\__|_|\___/|_| |_|___/     |
|                                                                              |
|                       Self-hosted runner registration                        |
|                                                                              |
--------------------------------------------------------------------------------

# Authentication


√ Connected to GitHub

# Runner Registration




√ Runner successfully added
√ Runner connection is good

# Runner settings


√ Settings Saved.

Granting file permissions to 'NT AUTHORITY\NETWORK SERVICE'.
Service actions.runner.dennisameling-git-for-windows-automation.arm64-test-vm-dennis successfully installed
Service actions.runner.dennisameling-git-for-windows-automation.arm64-test-vm-dennis successfully set recovery option
Service actions.runner.dennisameling-git-for-windows-automation.arm64-test-vm-dennis successfully set to delayed auto start
Service actions.runner.dennisameling-git-for-windows-automation.arm64-test-vm-dennis successfully configured
Waiting for service to start...
Service actions.runner.dennisameling-git-for-windows-automation.arm64-test-vm-dennis started successfully
Finished installing GitHub Actions runner.

@rwidmark
Copy link
Author

@dennisameling ill optimize it some more at the end of next week but until that I do think this will work for you.

It's nice to contribute to other projects.

Just DM me if you have some other things you want automated or similar :)

@dennisameling
Copy link
Contributor

@dscho could you do a final review? I guess you'll want some more descriptive commit messages, right?

@dscho
Copy link
Member

dscho commented Jan 15, 2023

@dscho could you do a final review? I guess you'll want some more descriptive commit messages, right?

Maybe next time the commit messages could follow the Intent, Context, Implementation & Justification format.

But I don't want to hold up this contribution any further 😄

@dscho dscho merged commit e9b20e2 into git-for-windows:main Jan 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

create-self-hosted-runner: automatically use the latest Git for Windows and GitHub Actions runner version
3 participants