Set Github Action deployment job target environment conditionally - continuous-integration

I'm creating a deploy.yml for my Github Action job. One very first section about the job definition is to define some information on the environment:
jobs:
deploy:
name: Deploy
runs-on: ubuntu-latest
environment: production
While this works perfect for production environment, it can be very scary for dev & testing deployment. My question is really how to set the value for environment dynamically based on the target branch? For example, pushing into dev branch will set environment: dev and test branch will set environment: test.
I have tried environment: $GITHUB_REF_NAME but it just simply print it as a plain string when sending alerts to slack.
Appreciate if anyone could advise.

Related

Deploying ASP.NET Core MVC app to Azure Web App Service gives "Your web app is running and waiting for your content"

I am new to deploying web apps.
I recently finished working on a project I wanted to deploy to Azure. I used VS to publish it an Azure App Service with a GitHub CI/CD Workflow but when I try to access it I am getting the following page:
I have absolutely no clue why. Everything in the process went smoothly but the page is not showing. I could really use some help!
I searched around for this error but I couldn't find anything. I also tried different deployment strategies to no avail.
EDIT: turns out that my project files are not getting copied to the app service.
EDIT 2: I got the app running with a zip deploy but Id still like to use GitHub Actions to automate it
I don't know how you create the .yml file. I generate it by default, you can check below. It it will deploy failed and don't worry.
Then you will find the .yml file generated under your github repo. Please find it and copy the below script and paste there. Don't forget replace the app-name, publish-profile and so on.
Then it will auto deploy again, now it should be deployed successfully. If not, try to modify anything, and deploy again for testing. You can see I add Azure Web App-01 in the first line.
The script I have tested and it works well like below.
Then we will find the publish file under the wwwroot. The scm site is :
https://app_name.scm.azurewebsites.net/newui
Then we should add startup command in azure portal.
Then the application should works well in your side. 👍
It not works in my side, after investigating the reason,I found it missing the connectionstring and other configurations, so it not works in my side.
.yml script
name: Build and deploy ASP.Net Core app to Azure Web App-01 - WebApplication920221114163713
on:
push:
branches:
- main
env:
AZURE_WEBAPP_NAME: WebApplication920221114163713
AZURE_WEBAPP_PACKAGE_PATH: .
CONFIGURATION: Release
DOTNET_CORE_VERSION: 6.0.x
WORKING_DIRECTORY: EasyRank
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: Set up .NET Core
uses: actions/setup-dotnet#v1
with:
dotnet-version: '6.0.x'
include-prerelease: true
- name: Build with dotnet
run: dotnet build "${{ env.WORKING_DIRECTORY }}" --configuration Release
- name: dotnet publish
run: dotnet publish "${{ env.WORKING_DIRECTORY }}" --configuration ${{ env.CONFIGURATION }} --no-build -o ${{env.DOTNET_ROOT}}/myapp
- name: Upload artifact for deployment job
uses: actions/upload-artifact#v2
with:
name: .net-app
path: ${{env.DOTNET_ROOT}}/myapp
deploy:
runs-on: ubuntu-latest
needs: build
environment:
name: 'Production'
url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}
steps:
- name: Download artifact from build job
uses: actions/download-artifact#v2
with:
name: .net-app
- name: Deploy to Azure Web App
id: deploy-to-webapp
uses: azure/webapps-deploy#v2
with:
app-name: 'WebApplication920221114163713'
slot-name: 'Production'
publish-profile: ${{ secrets.AZUREAP***0A789F0B31 }}
package: .

Why am I not able to trigger my pipeline upon PR merge into master?

I have a pipeline which I want to trigger when PR is merged into master. I have tried different things, but this did not work. Furthermore, I am neither getting any error nor pipeline is triggering.
I do not want this to be triggered on PR creation. I want this to be triggered when PR is merged into master. That is the reason I have not added pr in my yml.
What am I missing here?
Approaches:
Enabled "Continuous Integration" option
Following trigger syntax per Microsoft recommendation
trigger:
batch: True
branches:
include:
- master
paths:
include:
- cosmos
Setting up valid YML file
Pipeline:
# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml
trigger:
batch: True
branches:
include:
- master
paths:
include:
- cosmos
stages:
- stage: SOME_PATH_dev
displayName: SOME_PATH_dev
jobs:
- deployment: 'DeployToDev'
environment: Dev
cancelTimeoutInMinutes: 1
pool:
vmImage: ubuntu-latest
strategy:
runOnce:
deploy:
steps:
- checkout: self
- task: AzureResourceGroupDeployment#2
displayName: Azure Deployment:Create Or Update Resource Group action on SOME_PATH_dev
inputs:
ConnectedServiceName: SOME_KEY
resourceGroupName: SOME_PATH_dev
location: West US
csmFile: cosmos/deploy.json
csmParametersFile: cosmos/parameters-dev.json
deploymentName: SOME_PATH.Cosmos.DEV
Repo Structure:
References:
"Configuring the trigger failed, edit and save the pipeline again" with no noticeable error and no further details
Azure Devops build pipeline: CI triggers not working on PR merge to master when PR set to none
https://medium.com/#aksharsri/add-approval-gates-in-azure-devops-yaml-based-pipelines-a06d5b16b7f4
https://erwinstaal.nl/posts/manual-approval-in-an-azure-devops-yaml-pipeline/
I was changing YML (pipeline) files instead of files inside cosmos folder and expecting trigger.

How to stop GitHub Action build when SonarQube scan fails

I have a scan step built into my GitHub Action build and that is working fine. I reach out to my company's SonarQub instance and the scan is initiated. The problem I am having is trying to stop a build if there is a failure. For the life of me I can't seem to find a way to do that. Also, when I watch the scan it appears as though the next steps might be happening before it finishes (not positive on that but thought I would mention it). Any ideas??
name: Build, test, & deploy
on: [push]
jobs:
sonarqube:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
with:
# Disabling shallow clone is recommended for improving relevancy of reporting
fetch-depth: 0
# Triggering SonarQube analysis as results of it are required by Quality Gate check
- name: SonarQube Scan
uses: sonarsource/sonarqube-scan-action#master
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
- name: SonarQube Quality Gate check
uses: sonarsource/sonarqube-quality-gate-action#master
# Force to fail step after specific time
timeout-minutes: 5
env:
SONAR_TOKEN: ${{ secrets.ADAM_SONAR_TOKEN }}
build:
name: Project build & package
if: "!contains(github.even.head_commit.message, '[skip-ci]')"
runs-on: ubuntu-latest
env:
#environment var for this job
#### the rest of the build is below this area - I didn't think it was necessary to include
You should use needs in your build job:
build:
needs: sonarqube
name: Project build & package
You can find information here: https://docs.github.com/en/actions/using-jobs/using-jobs-in-a-workflow
The answer is using "needs": https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idneeds

Refactor .gitlab-ci.yml

I have a .gitlab-ci.yml file which has a couple of sections for deploying to staging servers - in our small dev team of two, we each have a staging server to test on. The portion of the file looks like this:
...
.deploy: &deploy
image: docker:stable
stage: deploy
script:
- ./deploy.sh
deploy_to_staging_sf:
<<: *deploy
only:
- staging_sf
tags:
- staging_sf
deploy_to_staging_ay:
<<: *deploy
only:
- staging_ay
tags:
- staging_ay
...
The different sections are there to match the different tags for gitlab's CI runners.
If we were to add another developer (or another platform; I was testing deployment to a Raspberry Pi once), I would need to replicate another deploy_to_... section.
I'm just wondering if there's a Gitlab or YAML way to refactor this and make it generic enough so I can add another deployment platform without modifying the file.
Thanks

Only run integration test when deploying from staging to production

I have 3 main branches in my gitlab project: dev , staging, production. I'm using postman newman for integration testing like this in .gitlab-ci.yml:
postman_tests:
stage: postman_tests
image:
name: postman/newman_alpine33
entrypoint: [""]
only:
- merge_requests
script:
- newman --version
- newman run https://api.getpostman.com/collections/zzz?apikey=zzz --environment https://api.getpostman.com/environments/xxx?apikey=xxxx
this script only run in merge request approval process from dev to staging , or staging to production. The problem is i need to only run this postman newman test when the process of merge request approval from staging to production, how can i achieve this?
This can be achieved using the 'advanced' only/except settings in combination with supplied environment variables:
postman_tests:
stage: postman_tests
image:
name: postman/newman_alpine33
entrypoint: [""]
only:
refs:
- merge_requests
variables:
- $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME == "staging"
- $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "production"
script:
- newman --version
- newman run https://api.getpostman.com/collections/zzz?apikey=zzz --environment https://api.getpostman.com/environments/xxx?apikey=xxxx
For a full list of predefined environment variables you can go here

Resources