I am trying to setup ci with gitlab-ci. And I have a few questions about it.
It looks like there is no rollback mechanism on gitlab-ci. So should i care about rolling back if deploy stage fails?
I'am planning to use "dotnet publish Solution.sln -c release" script. But I have multiple projects in this solution. It has one classlib and 2 api. (like AdminApi and UserApi). And these 2 apis are hosted different sites in IIS. In this case, how can i configure dotnet publish script with params?
Should i use something like xcopy for moving publish output to iis folder?
I've put an app_offile.htm_for each web sites in iis with "We'll back soon message" in html.
And I've solved my problem with this gitlab-ci.yml
stages:
- build
- test
- deploy
build:
stage: build
script:
- echo "Building the app"
- "dotnet publish MySolution.sln -c release"
artifacts:
untracked: true
only:
- dev
test:
stage: test
script: echo "Running tests"
artifacts:
untracked: true
dependencies:
- build
only:
- dev
deploy_staging:
stage: deploy
script:
- echo "Deployintg to staging server Admin"
- ren c:\\inetpub\\vhosts\\xxx\\admin\\app_offline.htm_ app_offline.htm
- dotnet publish PathToAdmin.csproj -c release -o c:\\inetpub\\vhosts\\xxx\\admin
- ren c:\\inetpub\\vhosts\\xxx\\admin\\app_offline.htm app_offline.htm_
- echo "Deployintg to staging server User"
- ren c:\\inetpub\\vhosts\\xxx\\user\\app_offline.htm_ app_offline.htm
- dotnet publish PathToUser.csproj -c release -o c:\\inetpub\\vhosts\\xxx\\user
- ren c:\\inetpub\\vhosts\\xxx\\user\\app_offline.htm app_offline.htm_
dependencies:
- build
only:
- dev
Related
New to Gitlab CI/CD. My build job works wonderfully, but deploy job is never executed. I removed all rules and still it does not run.
Here is the contents of my .gitlab-ci.yml file:
build-job:
stage: build
script:
- echo "STAGE - BUILD"
- echo $CI_JOB_STAGE
- echo $CI_COMMIT_MESSAGE
- echo $CI_COMMIT_BRANCH
- echo $CI_ENVIRONMENT_NAME
- mkdir bin
- mkdir obj
- "dotnet build"
deploy-to-staging:
stage: deploy
script:
- echo "STAGE - DEPLOY (STAGING)"
Any idea why Gitlab would skip the deploy stage? Do I have to explicitly define my stages? I tried that, but it made no difference (These lines were at the bottom of the yml file for a while):
stages:
- build
- deploy
While it's not explicit in the stages documentation, you should generally set those at the top.
If you're getting a yaml invalid failure, then use the CI lint tool to double check your spacing and the like without having to run a pipeline.
Keep in mind that:
If a job fails, it doesn't start the next stage.
If you don't define stages, then it uses build, test, deploy.
Any job that doesn't have a stage defined is assumed to be test.
Any stage that isn't used should simply be hidden. However, the exact behaviour may depend on the version of GitLab you're on. (If memory serves correctly, this was changed, but I can't the merge request off hand.)
As you can see in the official documentation of Gitlab CI, by defining stages, the sequence and the order of execution of jobs will be specified.
So the following gitlab-ci.yml should works:
stages:
- build
- deploy
build-job:
stage: build
script:
- echo "STAGE - BUILD"
- echo $CI_JOB_STAGE
- echo $CI_COMMIT_MESSAGE
- echo $CI_COMMIT_BRANCH
- echo $CI_ENVIRONMENT_NAME
- mkdir bin
- mkdir obj
- "dotnet build"
deploy-to-staging:
stage: deploy
script:
- echo "STAGE - DEPLOY (STAGING)"
Screenshot:
I started using Gitlab CI/CD and it is my yml file:
variables:
EXE_LOCAL_TEMP_FOLDER: 'c:\TMP'
DEPLOY_FOLDER: 'c:\MyProject_Release'
stages:
- build
- deploy
build:
stage: build
script:
- nuget restore
- msbuild MySolution.sln /t:Build /p:Configuration=Release
- xcopy /y MyProject\bin\Release\*.* $EXE_LOCAL_TEMP_FOLDER\
deploy:
stage: deploy
when : manual
script:
- xcopy /y c:\TMP\*.* $DEPLOY_FOLDER\
Now I need to deploy a Windows Service(WinService.csproj in my solution) on a remote machine, How can I do it in my yml file?
Hi in my case I use Gitlab community and Sonar Qube. The Sonar is hosted on Azure Ubuntu VM with a Docker container. Before my Sonar was on a Windows machine and here is the part of gitlab.yml.
variables:
SONAR_USER_HOME: "${CI_PROJECT_DIR}/.sonar" # Defines the location of the analysis task cache
GIT_DEPTH: "0" # Tells git to fetch all the branches of the project, required by the analysis task
stages:
- build
- test
before_script:
- Set-Variable -Name "time" -Value (Get-Date -Format "%H:%m")
- echo ${time}
- echo "started by ${GITLAB_USER_NAME}"
build:
stage: build
only:
- branches
script:
- echo "running scripts in the build job"
- choco feature enable -n=allowGlobalConfirmation
- choco install netfx-4.6.2-devpack
- choco install dotnetcore-sdk
- nuget restore -ConfigFile .\nuget.config
- msbuild ".\MyProject\MyProject.csproj" /p:DeployOnBuild=true /p:PublishProfile="Local Publish" /p:WarningLevel=0
- dotnet build ".\MyProject.Tests\MyProject.Tests.csproj" --configuration Release
artifacts:
paths:
- Publish
- .\MyProject.Tests\bin\Release\netcoreapp3.1\**
- .\MyProject
expire_in: 1 day
after_script:
- Remove-Item -Recurse -Force .\packages\
sonarcloud-check:
image:
name: sonarsource/sonar-scanner-cli:latest
entrypoint: [""]
cache:
key: "${CI_JOB_NAME}"
paths:
- .sonar/cache
dependencies:
- build
script:
- choco install sonarqube-scanner.portable
- SonarScanner.MSBuild.exe begin /k:"myProject" /d:sonar.host.url="http://localhost:9000" /d:sonar.login="8f6658e7684de225a4f45c7cf3466d462a95c1c7"
- nuget restore -ConfigFile .\nuget.config
- MsBuild.exe ./MyProject /t:Rebuild
- SonarScanner.MSBuild.exe end /d:sonar.login="8f6658e7684de225a4f45c7cf3466d462a95c1c7"
only:
- merge_requests
- master
- develop
- GitLabQualityTool
I know that this yml is not the best but it's work. The main project is on .net 4.6.2.Sonar is running on Ubuntu on port 80:9000. The point is that I am started to use Azure, Docker, and Sonar before a week. Can somebody help me with yml configuration?
My CI has two main steps. Build and deploy. The result of build is that an artifact is uploaded to maven nexus. And currently manual deploy step just takes the latest artifact from nexus and deploys it.
stages:
- build
- deploy
full:
stage: build
image: ubuntu
script:
- // Build and upload to nexus here
deploy:
stage: deploy
script:
- // Take latest artifact from nexus and deploy
when: manual
But to me this doesn't seem to make that much sense to always deploy latest build from every pipeline. I think ideally deploy step of each pipeline should deploy the artifact that was build by the same pipelines build task. Otherwise deploy step of each pipeline will do exactly the same thing regardless when it is started.
So I have two questions.
1) How can I make my deploy step to deploy the version that was build by this run?
2) If I still want to keep the "deploy latest" functionality, then does gitlab support adding a task separate of each pipeline because as I explained this step doesn't make a lot of seance to be in pipeline? I imagine it being in a separate specific place.
Not too familiar with maven and nexus, but assuming you can name the artifact before you push it, you can add one of the built-in environment variables that dictates which pipeline it's from.
ie:
...
Build:
stage: build
script:
- ./buildAsNormal.sh > build$CI_PIPELINE_ID.extension
- ./pushAsNormal.sh
Deploy:
stage: deploy
script:
- ./deployAsNormal #(but specify the build$CI_PIPELINE_ID.extension file)
There are a lot of CI env variables you can use that are extremely useful. The full list of them is here. The difference with $CI_PIPELINE_ID and $CI_JOB_ID is that the pipeline id is constant for all jobs in the pipeline, no matter when they execute. That means the pipeline id will be the same even if you run a manual step a week after the automated steps. The job id is specific to each job.
Regarding your comment, the usage of artifacts: can solve your problem.
You can put the version number in a file and get the file in the next stage :
stages:
- build
- deploy
full:
stage: build
image: ubuntu
script:
- echo "1.0.0" > version
- // Build and upload to nexus here
artifacts:
paths:
- version
expire_in: 1 week
deploy:
stage: deploy
script:
- VERSION = $(cat version)
- // Take the artifact from nexus using VERSION variable and deploy
when: manual
An alternative is to build, push to nexus and use artifact: to pass the result of the build to the Deploy job :
stages:
- build
- deploy
full:
stage: build
image: ubuntu
script:
- // Build and put the result in out/ directory
- // Upload the result from out/ to nexus
artifacts:
paths:
- out/
expire_in: 1 week
deploy:
stage: deploy
script:
- // Take the artifact from in out/ directory and deploy it
when: manual
I'm trying to build android app and then upload the artifact to another server by:
cURL a php page in that server by passing the job ID and artifact name after job completes
Consuming gitlab api to fetch the artifact's single file using the passed job ID and artifact name
So far, I have 2 stages build and deploy. So, once the build succeeds and the artifacts is uploaded, the deploy job starts which should be able to pass in the build artifact's job ID (the artifact name is a global var) which will finally curl my php page to initiate the download. Here is my gitlab ci config:
stages:
- buildRelease
- deploy
variables:
fileName: artifact-Name.apk
buildReleaseApp:
variables:
GIT_SUBMODULE_STRATEGY: recursive
before_script:
- chmod +x ./gradlew
- echo $setUpBuildScript | base64 -d > buildRelease.sh
- chmod +x prepareConfig.sh
stage: buildRelease
script:
- sh buildRelease.sh
only:
- tags
artifacts:
paths:
- app/build/outputs/apk/playstore/release/*.apk
after_script:
- mv app/build/outputs/apk/playstore/release/*.apk app/build/outputs/apk/playstore/release/${fileName}
deployReleaseApp:
variables:
dependencies:
- buildReleaseApp
stage: deploy
only:
- tags
script:
- echo $pushScript | base64 -d > deploy.sh
- sh deploy.sh
Everything works for me except the php receives deploy job's ID and when the php tried to download the build artifact, it throws 404.
Is there anyway to pass the buildRelease stage's job ID to deploy job?
this can help...
after_script:
- echo "JOB_ID=$CI_JOB_ID" >> job.env
artifacts:
reports:
dotenv: job.env
You can access this job.env in other stages and call $JOB_ID to fetch the concerned job ID and also status