Skip to content

Instantly share code, notes, and snippets.

@aliasadidev
Last active October 6, 2021 12:19
Show Gist options
  • Save aliasadidev/cfb283613456e4430a2d96a9caecf38e to your computer and use it in GitHub Desktop.
Save aliasadidev/cfb283613456e4430a2d96a9caecf38e to your computer and use it in GitHub Desktop.

Revisions

  1. aliasadidev revised this gist Oct 6, 2021. No changes.
  2. aliasadidev revised this gist Oct 6, 2021. 1 changed file with 14 additions and 14 deletions.
    28 changes: 14 additions & 14 deletions powershell-ci.ps1
    Original file line number Diff line number Diff line change
    @@ -56,16 +56,16 @@ if($isUpdate -eq $false) {
    }

    # go to the project directory
    cd $ProjectPath
    cd $ProjectPath

    # run dotnet restore
    dotnet restore --disable-parallel
    write-host "restore finished" -ForegroundColor green
    dotnet restore --disable-parallel
    write-host "restore finished" -ForegroundColor green


    # publish api project to publish directory
    dotnet publish -c Release -o ./publish
    write-host "publish finished" -ForegroundColor green
    write-host "publish finished" -ForegroundColor green


    # check app pool is running
    @@ -77,19 +77,19 @@ if($isUpdate -eq $false) {
    write-host "sevice is stopped" -ForegroundColor green

    # make a delay
    Start-Sleep -s 7
    Start-Sleep -s 7
    }


    #start syncing files
    write-host "start syncing files" -ForegroundColor green
    (robocopy $ProjectPublishPath $WORKER_SERIVCE_PATH /s /mir /purge);
    if ($lastexitcode -eq 0) {
    write-host 'Robocopy succeeded'
    }
    else {
    write-host $lastexitcode
    }
    write-host "start syncing files" -ForegroundColor green
    (robocopy $ProjectPublishPath $WORKER_SERIVCE_PATH /s /mir /purge);
    if ($lastexitcode -eq 0) {
    write-host 'Robocopy succeeded'
    }
    else {
    write-host $lastexitcode
    }

    # check app pool is running
    # if service is stoped, then run it
    @@ -104,7 +104,7 @@ if($isUpdate -eq $false) {
    # update last-commit.txt file
    Set-Content .\last-commit.txt $currentLastCommitHash

    write-host "publish finished" -ForegroundColor green
    write-host "publish finished" -ForegroundColor green

    }

  3. aliasadidev revised this gist Oct 6, 2021. No changes.
  4. aliasadidev revised this gist Oct 6, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion powershell-ci.ps1
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,7 @@ $ProjectPath = '.\ShopApi\'
    # project publish directory path
    $ProjectPublishPath = Join-Path -Path '.\' -ChildPath "publish"

    # go to project directory
    # go to the project directory
    cd $ProjectPath

    # checkout branch to dev #
  5. aliasadidev revised this gist Oct 6, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion powershell-ci.ps1
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    # simple ci with powershell
    # log everything in git-log-file-{datetime}
    # log everything in git-log-file-{datetime}.log
    $logFile = ".\git-log-file-$([System.Datetime]::Now.ToString("yyyy-MM-dd-HH-mm-ss")).log"

    Start-Transcript -Path $logFile
  6. aliasadidev revised this gist Oct 6, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion powershell-ci.ps1
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@

    # simple ci with powershell
    # log everything in git-log-file-{datetime}
    $logFile = ".\git-log-file-$([System.Datetime]::Now.ToString("yyyy-MM-dd-HH-mm-ss")).log"

  7. aliasadidev revised this gist Oct 6, 2021. 1 changed file with 0 additions and 3 deletions.
    3 changes: 0 additions & 3 deletions powershell-ci.ps1
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@
    ```powershell

    # log everything in git-log-file-{datetime}
    $logFile = ".\git-log-file-$([System.Datetime]::Now.ToString("yyyy-MM-dd-HH-mm-ss")).log"
    @@ -110,5 +109,3 @@ if($isUpdate -eq $false) {
    }

    Stop-Transcript

    ```
  8. aliasadidev created this gist Oct 6, 2021.
    114 changes: 114 additions & 0 deletions powershell-ci.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,114 @@
    ```powershell

    # log everything in git-log-file-{datetime}
    $logFile = ".\git-log-file-$([System.Datetime]::Now.ToString("yyyy-MM-dd-HH-mm-ss")).log"

    Start-Transcript -Path $logFile

    # app pool name
    $WORKER_SERVICE_NAME = '{App_Pool_Name}'

    # app directory path
    $WORKER_SERIVCE_PATH = 'D:\wwwroot\{App_Pool_Name}'

    # project root directory path
    $ProjectPath = '.\ShopApi\'

    # project publish directory path
    $ProjectPublishPath = Join-Path -Path '.\' -ChildPath "publish"

    # go to project directory
    cd $ProjectPath

    # checkout branch to dev #
    write-host "checkout branch to dev" -ForegroundColor Cyan
    (git checkout dev -f)

    # removes staged and working directory changes in the server
    (git reset --hard)

    # pull dev branch #
    write-host "pull dev branch" -ForegroundColor Cyan
    (git pull)

    # get latest commit hash code
    $currentLastCommitHash = (git log dev -1 --pretty=format:"%H")
    write-host "latest hash code:$currentLastCommitHash" -ForegroundColor Cyan

    # move to ci root directory
    cd ..

    # read latest commit hash code from last-commit.txt file
    $lastCommitHash = Get-Content .\last-commit.txt

    # compare the current commit hash with previous the commit hash
    $isUpdate = $currentLastCommitHash -eq $lastCommitHash

    # dev branch is updated?
    write-host "is updated: $isUpdate" -ForegroundColor Cyan;


    # publish new version
    if($isUpdate -eq $false) {

    # remove the publish directory in the project
    if(Test-Path $ProjectPublishPath){
    Remove-Item -path $ProjectPublishPath -r -ErrorAction SilentlyContinue
    }

    # go to the project directory
    cd $ProjectPath

    # run dotnet restore
    dotnet restore --disable-parallel
    write-host "restore finished" -ForegroundColor green


    # publish api project to publish directory
    dotnet publish -c Release -o ./publish
    write-host "publish finished" -ForegroundColor green


    # check app pool is running
    # if service is running, then stop it
    if((Get-WebAppPoolState -Name $WORKER_SERVICE_NAME).Value -ne 'Stopped'){
    write-host "sevice is running" -ForegroundColor green
    # stop the app pool
    Stop-WebAppPool -Name $WORKER_SERVICE_NAME
    write-host "sevice is stopped" -ForegroundColor green

    # make a delay
    Start-Sleep -s 7
    }


    #start syncing files
    write-host "start syncing files" -ForegroundColor green
    (robocopy $ProjectPublishPath $WORKER_SERIVCE_PATH /s /mir /purge);
    if ($lastexitcode -eq 0) {
    write-host 'Robocopy succeeded'
    }
    else {
    write-host $lastexitcode
    }

    # check app pool is running
    # if service is stoped, then run it
    if((Get-WebAppPoolState -Name $WORKER_SERVICE_NAME).Value -ne 'Started'){
    Start-WebAppPool -Name $WORKER_SERVICE_NAME
    write-host "service is started" -ForegroundColor green
    }

    # back to root directory
    cd ..

    # update last-commit.txt file
    Set-Content .\last-commit.txt $currentLastCommitHash

    write-host "publish finished" -ForegroundColor green

    }

    Stop-Transcript

    ```