diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 8c3f8c2..c0cd1c0 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -1,6 +1,5 @@ { - "name": "Codespace to bootstrap valet in a Codespace", - //Use base codespace image then pull Valet on postCreateCommand, + "name": "Codespace to perform Valet Labs", "image": "mcr.microsoft.com/vscode/devcontainers/universal:linux", "remoteUser": "codespace", "overrideCommand": false, @@ -15,8 +14,7 @@ // Add the IDs of extensions you want installed when the container is created. "extensions": [ - "ms-azuretools.vscode-docker", + "ms-azuretools.vscode-docker" ], - "onCreateCommand": 'echo "export GITHUB_ACCESS_TOKEN=$VALET_GHCR_PASSWORD" >> ~/.bashrc', - "postCreateCommand": "sudo bash .devcontainer/setupcodespace.sh ${VALET_GHCR_PASSWORD} ${AZURE_DEVOPS_PROJECT} ${AZURE_DEVOPS_ORGANIZATION} ${AZURE_DEVOPS_ACCESS_TOKEN} ${GITHUB_USER} 'https://github.com/' && gh extension install github/gh-valet || echo 'Could not auto-build. Skipping.' " + "postCreateCommand": "gh extension install github/gh-valet || echo 'Could not auto-build. Skipping.' " } diff --git a/.devcontainer/setupcodespace.sh b/.devcontainer/setupcodespace.sh deleted file mode 100644 index c98469a..0000000 --- a/.devcontainer/setupcodespace.sh +++ /dev/null @@ -1,28 +0,0 @@ -#!/bin/bash -echo "Starting setupcodespace.sh" - -azdoProject="AZURE_DEVOPS_PROJECT=" -azdoOrg="AZURE_DEVOPS_ORGANIZATION=" -azdoInstance="AZURE_DEVOPS_INSTANCE_URL=" -ghAccess="GITHUB_ACCESS_TOKEN=" -azdoAccess="AZURE_DEVOPS_ACCESS_TOKEN=" -ghInstanceUrl="GITHUB_INSTANCE_URL=" - -if [ -z "$1" -o -z "$5" ] -then - echo "Error: Docker Pull Valet not executing because GITHUB_USER and/or VALET_PASSWORD not set" -else - docker login ghcr.io/valet-customers --username $5 --password $1 - docker pull ghcr.io/valet-customers/valet-cli:latest - echo "Success: Docker Pull Valet completed" -fi - -value=`cat valet/.env.local.template` -echo "$value" > valet/.env.local - -if [ -z "$1" -o -z "$2" -o -z "$3" -o -z "$4" -o -z "$6" ] -then - echo "Error: Set envars not set, valid values not passed in. You will have to manually use the valet/.env.local folder" -fi - -echo "Finished setupcodespace.sh" diff --git a/.github/workflows/valetbsazdo.yml b/.github/workflows/valetbsazdo.yml deleted file mode 100644 index bdcda6f..0000000 --- a/.github/workflows/valetbsazdo.yml +++ /dev/null @@ -1,35 +0,0 @@ -# This is a workflow to set up an Azure DevOps instance seeded with pipelines -# You need to Add a secret for an Azure DevOps PAT called AZDOPAT - -name: Valet Bootstrap for Azure DevOps - -on: - # Allows you to run this workflow manually from the Actions tab - workflow_dispatch: - inputs: - AzDoOrgName: - description: 'Azure DevOps organization name' - required: true - type: string - AzDoUserName: - description: 'Azure DevOps user name' - required: true - type: string - AzDoProjectName: - description: 'Azure DevOps project name' - default: 'ValetBootstrap' - type: string -jobs: - bootstrap: - name: Bootstrap - runs-on: windows-latest - steps: - - name: Check out repository code - uses: actions/checkout@v2 - - name: Run powershell file to bootstrap Azure DevOps - env: - AZDOPATTOUSE: ${{ secrets.AZURE_DEVOPS_ACCESS_TOKEN }} - ROOTFOLDER: ${{ github.workspace }} - shell: pwsh - run: | - ./azure_devops/bootstrap/azDevOps.ps1 -project ${{ github.event.inputs.AzDoProjectName }} -userName ${{ github.event.inputs.AzDoUserName }} -repoName ${{ github.event.inputs.AzDoProjectName }} -orgToUse ${{ github.event.inputs.AzDoOrgName }} -rootFolder $env:ROOTFOLDER -daPassword $env:AZDOPATTOUSE diff --git a/azure_devops/bootstrap/azDevOps.ps1 b/azure_devops/bootstrap/azDevOps.ps1 deleted file mode 100644 index 8e36704..0000000 --- a/azure_devops/bootstrap/azDevOps.ps1 +++ /dev/null @@ -1,235 +0,0 @@ -param ( - $project = "ValetBootstrap", - $repoName = "ValetBootstrap", - $codeFolder= "code,azure_devops/pipelines", - $rootFolder= ".", - $pipelinesRoot="azure_devops/pipelines", - [String]$orgToUse = "..", - [String]$userName=$(Throw "Azure DevOps Username required."), - [String]$daPassword=$(Throw "Password required.") - ) - -Write-Host "Start Valet Bootstrap" -Write-Host "Azure DevOps Organization to use $orgToUse" -Write-Host "Azure DevOps Project to use $project" -Write-Host "Repo Name $repoName" -Write-Host "Code Folders: $codeFolder" -Write-Host "Root Folders: $rootFolder" -Write-Host "Pipelines Folders: $pipelinesRoot" -Write-Host "Azure DevOps username to authenticate with $userName" -Write-Host "Azure DevOps PAT to authenticate with $daPassword" - -function Get-BasicAuthCreds { - param([string]$Username,[String]$Password) - $AuthString = "{0}:{1}" -f $Username,$Password - $AuthBytes = [System.Text.Encoding]::Ascii.GetBytes($AuthString) - return [Convert]::ToBase64String($AuthBytes) -} - -#Get the creds to use in all GitHub API calls. -$BasicCreds = Get-BasicAuthCreds -Username "$userName" -Password $daPassword - -########################################################################## -#CREATE THE AZURE DEVOPS PROJECT -$createtUrlToUse = [string]::Format("https://dev.azure.com/{0}/_apis/projects?api-version=6.0", $orgToUse) - -$projectToCreate = @{ - "name"= "$project" - "description"= "Project to be used for Valet Demos" - "capabilities"= @{ - "versioncontrol"= @{ - "sourceControlType"= "Git" - } - "processTemplate"= @{ - "templateTypeId"= "6b724908-ef14-45cf-84f8-768b5384da45" - } - } - } - - try{ - $projectResponse = Invoke-RestMethod -Method 'Post' -Uri $createtUrlToUse -Headers @{"Authorization"="Basic $BasicCreds"} -Body ($projectToCreate|ConvertTo-Json) -ContentType "application/json" - $projectResponse | ConvertTo-Json - - Start-Sleep -s 5 - Write-Host "Yes! Project $project was created!" - - } Catch { - Write-Host " " - Write-Host "##########################################################################" - Write-Host "ERROR during project creation: $project" - Write-Host "The API call was: $createtUrlToUse" - Write-Host " " - - if($_.ErrorDetails.Message) { - Write-Host "See error message below:" - Write-Host $_.ErrorDetails.Message - } else { - Write-Host $_ - Write-Host "It is likely that the AZURE_DEVOPS_ACCESS_TOKEN is not set correctly in the worflow OR does not have sufficent permissions" - } - Write-Host " " - Write-Host "##########################################################################" - throw "Exiting. The Project $project was NOT created in Azure DevOps" - } - -########################################################################## -# IMPORT THE REPO -Set-Location $rootFolder - -[array]$folderArray = $codeFolder.Split(",") - -$addTextFile = @{ - "refUpdates"= @( - @{ - "name"= "refs/heads/main" - "oldObjectId"= "0000000000000000000000000000000000000000" - } - ) - "commits"= @( - @{ - "comment"= "Initial commit." - "changes"= @( - foreach ($folder in $folderArray) - { - $codeRootFolder = Join-Path -Path $rootFolder -ChildPath $folder - Write-Host "codeRootFolder: $codeRootFolder" - - $itemPath = $folder.Substring($folder.lastIndexOf('/') + 1) - Write-Host "itemPath: $itemPath" - - $codeFiles = (Get-ChildItem "$codeRootFolder" -Recurse -Attributes !Directory) - foreach ($codeFile in $codeFiles ) { - $daFullName = $codeFile.FullName - $codeBody = (Get-Content "$daFullName" -Raw) - #Write-Host "codeBody: $codeBody" - #Write-Host "folder: $folder" - $relath = $daFullName | Resolve-Path -Relative - #Write-Host "relath: $relath" - $replaceSlash = $folder.Replace("/", "\") - $repoPath = $relath.Substring($relath.lastIndexOf($replaceSlash) + $replaceSlash.Length) - $wholeRepoPath = Join-Path -Path $itemPath -ChildPath $repoPath - $wholeRepoPath = $wholeRepoPath.Replace("\", "/") - #Write-Host "wholeRepoPath: $wholeRepoPath" - #Write-Host "repoPath: $repoPath" - @{ - "changeType"= "add" - "item"= @{ - "path"= "/$wholeRepoPath" - } - "newContent"= @{ - "content"= "$codeBody" - "contentType"= "rawtext" - } - } - } - } - ) - } - ) - } - -try{ - $repoUrlToUse = [string]::Format("https://dev.azure.com/{0}/{2}/_apis/git/repositories/{1}/pushes?api-version=7.1-preview.2", $orgToUse, $repoName, $project) - $pushResponse = Invoke-RestMethod -Method 'Post' -Uri $repoUrlToUse -Headers @{"Authorization"="Basic $BasicCreds"} -Body ($addTextFile|ConvertTo-Json -Depth 5) -ContentType "application/json" - $repoIds = $pushResponse.repository.id - Write-Host "Yes! Repo Was was created! The repoId is $repoIds" -} Catch { - Write-Host " " - Write-Host "##########################################################################" - Write-Host "ERROR during Repo creation:" - Write-Host "The API call was: $repoUrlToUse" - Write-Host " " - - if($_.ErrorDetails.Message) { - Write-Host "See error message below:" - Write-Host $_.ErrorDetails.Message - } else { - Write-Host $_ - } - Write-Host " " - Write-Host "##########################################################################" - throw "Exiting. The Repo was NOT created. It is likely the Project was created in Azure DevOps." -} - -########################################################################## -########################################################################## -# Create YML Pipelines -$plUrlToUse = [string]::Format("https://dev.azure.com/{0}/{1}/_apis/pipelines?api-version=6.0-preview.1", $orgToUse, "$project") - -$files = Get-ChildItem ".\$pipelinesRoot\yml\" -foreach ($f in $files) -{ - $nameOfPl = $f.BaseName - $fileName = $f.Name - Write-Host "basename: $nameOfPl filename: $fileName" - - $pipelineCreateBody = @{ - "folder"= "pipelines" - "name"= "$nameOfPl" - "configuration"= @{ - "type"= "yaml" - "path"= "/pipelines/yml/$fileName" - "repository"= @{ - "id"= "$repoIds" - "name"= "$repoName" - "type"= "azureReposGit" - } - } - } - - try{ - $repoResponse = Invoke-RestMethod -Method 'Post' -Uri $plUrlToUse -Headers @{"Authorization"="Basic $BasicCreds"} -Body ($pipelineCreateBody|ConvertTo-Json) -ContentType "application/json" - } Catch { - Write-Host " " - Write-Host "##########################################################################" - Write-Host "ERROR during Pipeline creation:" - Write-Host "The API call was: $plUrlToUse" - Write-Host " " - - if($_.ErrorDetails.Message) { - Write-Host "See error message below:" - Write-Host $_.ErrorDetails.Message - } else { - Write-Host $_ - } - Write-Host " " - Write-Host "The Pipeline $nameOfPl was NOT created." - Write-Host "##########################################################################" - } -} - -########################################################################## -#CREATE CLASSIC PIPELINEs - -$classicUrlToUse = [string]::Format("https://dev.azure.com/{0}/{1}/_apis/build/definitions?api-version=7.1-preview.7", $orgToUse, "$project") - -$files = Get-ChildItem ".\$pipelinesRoot\classic\" -foreach ($f in $files) -{ - $nameOfPl = $f.BaseName - $fileName = $f.Name - Write-Host "basename: $nameOfPl filename: $fileName" - - $pclassicreateBody = (Get-Content ./$pipelinesRoot/classic/$fileName).Replace("REPOTOREPLACE", "$repoName") - - try{ - Invoke-RestMethod -Method 'Post' -Uri $classicUrlToUse -Headers @{"Authorization"="Basic $BasicCreds"} -Body ($pclassicreateBody) -ContentType "application/json" - } Catch { - Write-Host " " - Write-Host "##########################################################################" - Write-Host "ERROR during Pipeline creation:" - Write-Host "The API call was: $classicUrlToUse" - Write-Host " " - - if($_.ErrorDetails.Message) { - Write-Host "See error message below:" - Write-Host $_.ErrorDetails.Message - } else { - Write-Host $_ - } - Write-Host " " - Write-Host "The Pipeline $nameOfPl was NOT created." - Write-Host "##########################################################################" - } -} -Write-Host "End Valet Bootstrap" diff --git a/azure_devops/readme.md b/azure_devops/readme.md index ba0c2e5..3c75df0 100644 --- a/azure_devops/readme.md +++ b/azure_devops/readme.md @@ -14,107 +14,55 @@ This lab bootstraps a Valet environment using GitHub Codespaces and enables you 1. Verify you are in your own Repository created from the landing page [Valet Labs](https://github.com/valet-customers/labs). -## Prerequisites -1. Azure DevOps organization. Please identify or create an Azure DevOps organization to use: [Click to create an Azure DevOps Org](https://docs.microsoft.com/en-us/azure/devops/organizations/accounts/create-organization?toc=%2Fazure%2Fdevops%2Fget-started%2Ftoc.json&bc=%2Fazure%2Fdevops%2Fget-started%2Fbreadcrumb%2Ftoc.json&view=azure-devops) - - Note and store the Azure DevOp sorganization name for later. - - Note and store the user name you use for your Azure DevOps Organization. It will be an email address. -2. Azure DevOps Project. The default project name for this lab is `ValetBootstrap` There is an Action workflow that will create and populate the Azure DevOps project. No need to create it yourself. Note: The project has to be unique in the Azure DevOps organization. If you already have a project name `ValetBootstrap` please pick a different unique project name. - - Note and store the Azure DevOps project name for later. -3. Create an Azure DevOps personal access token with the following scopes: - - To do so, navigate to Sign in to your organization `https://dev.azure.com/{yourorganization}`. - - Click your `Account management` icon - - Click `user settings` - - Click `Personal access tokens`. - - Select `+ New Token` - - Name your token, select the organization where you want to use the token, and then set your token to automatically expire after a set number of days. - - Select the following scopes (Click `Show more scopes` if you don't see all of the below): - - Agents Pool: `Read` - - Build: `Read & Execute` - - Code: `Read & Write` - - Project and Team: `Read, Write, & Manage` - - Release: `Read` - - Service Connections: `Read` - - Task Groups: `Read` - - Variable Groups: `Read` - - Click `Create` - - Copy the PAT somewhere safe and temporary. -4. Create a GitHub personal access token. - - To do so, navigate to your GitHub `Settings` - click your profile photo and then click `Settings`. - - Go to `Developer Settings` - - Go to `Personal Access Tokens` -> `Legacy tokens (if present)` - - Click `Generate new token` -> `Legacy tokens (if present)`. If required, provide your password. - - Select at least these scopes: `read packages` and `workflow`. Optionally, provide a text in the **Note** field and change the expiration. - - Click `Generate token` - - Copy the PAT somewhere safe and temporary. - -## Codespace secrets -Please add the following Codespace secrets. - -- `VALET_GHCR_PASSWORD`: Add `VALET_GHCR_PASSWORD` as the `Name` and the GitHub personal access token created above as the value. -- `AZURE_DEVOPS_ACCESS_TOKEN`: Add `AZURE_DEVOPS_ACCESS_TOKEN` as the `Name` and the Azure DevOps personal access token created above as the value. -- `AZURE_DEVOPS_ORGANIZATION`: Add `AZURE_DEVOPS_ORGANIZATION` as the `Name` and the Azure DevOps organization noted above as the value. -- `AZURE_DEVOPS_PROJECT`: Add `AZURE_DEVOPS_PROJECT` as the `Name` and the Azure DevOps project noted above as the value. - -Steps to create the Codespace secrets. Complete for secret noted above: - -- Navigate to the `Settings` tab in this repo -- Find `Secrets` and click the down arrow -- Click `Codespaces` -- Click `New Repository Secret` to create a new secret -- Name the secret as noted above -- Paste in the value noted above -- Click `Add Secret` - -## Action secrets -Please add the following Action secrets. - -- `AZURE_DEVOPS_ACCESS_TOKEN`: Add `AZURE_DEVOPS_ACCESS_TOKEN` as the `Name` and the Azure DevOps personal access token created above as the value. - -Steps to create the Actions secret. Complete for secret noted above: - -- Navigate to the `Settings` tab in this repo -- Find `Secrets` and click the down arrow -- Click `Actions` -- Click `New Repository Secret` to create a new secret -- Name the secret as noted above -- Paste in the value noted above -- Click `Add Secret` - -## Azure DevOps project creation - -Run the Actions workflow: -- CLick the `Actions` tab -- Select the `Valet Bootstrap for Azure DevOps` action -- Click `Run Workflow` -- Input the Azure DevOps Organization name identified above -- Input the Azure DevOps user name of the user that created the Azure DevOps PAT above. This will be an email address -- Accept the default Azure DevOps project name or change it to one of your preference - - NOTE: The default project name is `ValetBootstrap`. This must be unique in your Azure DevOps Organization. If not, please choose a unique name. -- Click `Run Workflow` -- Verify the workflow completed successfully. If the workflow did not run successfully you will see a detailed error message. - -### Example ### -![runaction](https://user-images.githubusercontent.com/26442605/167679930-9bdf6f4f-2e94-4145-aed3-8ee3e8e91d90.png) - - ## Use Valet with a codespace 1. Start the codespace - - Click the `Code` with button down arrow above repository on the repository's landing page. - - Click the `Codespaces` tab - - Click `Create codespaces on main` to create the codespace. If you are in another branch then the `main` branch, the codespace will button will have the current branch specified. - - Wait a couple minutes, then verify that the codespace starts up. Once it is fully booted up, the termininal should be present. -2. Verify Valet CLI is installed and working. More information on the [GitHub Valet CLI extension](https://github.com/github/gh-valet) - - Verify Valet CLI is installed and working - - Run `gh valet version` in the Visual Studio Code terminal and verify the output looks like the below image. Note the valet version will be different than below as the latest version gets pulled down. - - If `gh valet version` did not produce a similar image with a version please follow these instructions [Troubleshoot GH Valet extension](#troubleshoot-gh-valet-extension) - - Start using Valet by following along with the [Labs for Azure DevOps](#labs-for-azure-devops) - -### Example ### -![gh-valet-version](https://user-images.githubusercontent.com/26442605/170106559-e69e669f-a1f6-4c2c-8998-3f089b899704.png) + +- Click the `Code` with button down arrow above repository on the repository's landing page. +- Click the `Codespaces` tab +- Click `Create codespaces on main` to create the codespace. If you are in another branch then the `main` branch, the codespace will button will have the current branch specified. +- Wait a couple minutes, then verify that the codespace starts up. Once it is fully booted up, the termininal should be present. + +2. Verify the [extension](https://github.com/github/gh-valet) to the official GitHub CLI for Valet is installed and working. + +- Run `gh valet version` in the Visual Studio Code terminal and verify the output looks like the below image. Note the valet version will be different than below as the latest version gets pulled down. +- If `gh valet version` did not produce a similar image with a version please follow these instructions [Troubleshoot GH Valet extension](#troubleshoot-gh-valet-extension) + +----- + +## Bootstrap your Azure DevOps environment + +1. Create an Azure DevOps personal access token with the following scopes: + +- To do so, navigate to your organization `https://dev.azure.com/{organization}`. +- Click `Personal access tokens`. +- Select `+ New Token` +- Name your token, select the organization where you want to use the token, and then set your token to automatically expire after a set number of days. +- Select the following scopes (Click `Show more scopes` if you don't see all of the below): + - Agents Pool: `Read` + - Build: `Read & Execute` + - Code: `Read & Write` + - Project and Team: `Read, Write, & Manage` + - Release: `Read` + - Service Connections: `Read` + - Task Groups: `Read` + - Variable Groups: `Read` +- Click `Create` +- Copy the PAT somewhere safe and temporary. + +2. Run the Azure DevOps setup script. This script will create an Azure DevOps project and ensure it is ready to be used in the following labs. This script should only need to be run once. + +- Navigate to the terminal within your Codespace. +- Run `./azure_devops/bootstrap/setup --organization :organization --project :project --access-token :access-token` while replacing these values: + - `:organization`: the name of your existing Azure DevOps organization + - `:project`: the name of the project to be created in your Azure DevOps organization + - `:access_token`: the PAT created in step 1 above +- Once this script completes, you will see a new project in your Azure DevOps organization that is populated with some pipelines. ## Labs for Azure DevOps -Perform the following labs to test-drive Valet + +Perform the following labs to learn how to migrate Azure DevOps pipelines to GitHub Actions using Valet: + - [Audit Azure DevOps pipelines using the Valet audit command](valet-audit-lab.md) - [Dry run the migration of an Azure DevOps pipeline to GitHub Actions](valet-dry-run-lab.md) - [Migrate an Azure DevOps pipeline to GitHub Actions](valet-migrate-lab.md) @@ -122,12 +70,14 @@ Perform the following labs to test-drive Valet - [Forecast: Valet forecast lab](valet-forecast-lab.md) ## Troubleshoot GH Valet extension -Manually Install the GitHub CLI Valet extension. More information on the [GitHub Valet CLI extension](https://github.com/github/gh-valet) -- Verify you are in the Visual Studio Code terminal -- Run this command to install the GitHub Valet extension -- `gh extension install github/gh-valet` -- Verify the result of the install is: `✓ Installed extension github/gh-valet` -- If you get a similiar error to the following, click the link to authorize the token + +Manually installing the [extension](https://github.com/github/gh-valet) to the official GitHub CLI for Valet: + +- Verify you are in the Visual Studio Code terminal +- Run this command to install the GitHub Valet extension +- `gh extension install github/gh-valet` +- Verify the result of the install is: `✓ Installed extension github/gh-valet` +- If you get a similiar error to the following, click the link to authorize the token ![linktolcickauth](https://user-images.githubusercontent.com/26442605/169588015-9414404f-82b6-4d0f-89d4-5f0e6941b029.png) - - Restart Codespace after clicking the link -- Verify Valet CLI is installed and working + - Restart Codespace after clicking the link +- Verify the Valet CLI is installed and working by running the `gh valet version` command diff --git a/azure_devops/valet-configure-lab.md b/azure_devops/valet-configure-lab.md new file mode 100644 index 0000000..e69de29