diff --git a/.github/workflows/node-CD.yml b/.github/workflows/node-CD.yml index fd77989..e830945 100644 --- a/.github/workflows/node-CD.yml +++ b/.github/workflows/node-CD.yml @@ -9,6 +9,14 @@ on: defaults: run: working-directory: apis/userprofile + +env: + webappName: openhackkkg4t772userprofile + resourceGroup: openhackkkg4t772rg + subscriptionId: 03a6d591-1096-4413-9cf4-ae9bb9a8f348 + healthCheckUrl: https://openhackkkg4t772userprofile-staging.azurewebsites.net/api/healthcheck/userprofile + dockerImageName: api-userprofile + jobs: build-and-deploy: runs-on: ubuntu-latest @@ -26,10 +34,21 @@ jobs: - name: Output Run Number run: echo ${{ github.run_number }} + - name: create .env + run: | + touch .env + VERSION=${{ github.run_number }} > .env + - name: Docker build and push run: | docker build . -t openhackkkg4t772acr.azurecr.io/devopsoh/api-userprofile:${{ github.run_number }} docker push openhackkkg4t772acr.azurecr.io/devopsoh/api-userprofile:${{ github.run_number }} + + - name: Ensure slot is started + uses: azure/CLI@v1 + with: + inlineScript: | + az webapp start --slot staging --name ${{ env.webappName }} --resource-group ${{ env.resourceGroup }} --subscription ${{ env.subscriptionId }} - name: Deploy on staging uses: azure/webapps-deploy@v2 @@ -39,6 +58,58 @@ jobs: publish-profile: ${{ secrets.API_USERPROFILE_STAGING_DEPLOYMENT_PROFILE }} #TODO: add userprofile images: 'openhackkkg4t772acr.azurecr.io/devopsoh/api-userprofile:${{ github.run_number }}' + - name: Restart the slot + uses: azure/CLI@v1 + with: + inlineScript: | + az webapp restart --slot staging --name ${{ env.webappName }} --resource-group ${{ env.resourceGroup }} --subscription ${{ env.subscriptionId }} + + - name: Check the image tag + uses: azure/CLI@v1 + with: + inlineScript: | + for i in $(seq 1 10) + do + dockerVersion=$(az webapp show --slot staging --name ${{ env.webappName }} --resource-group ${{ env.resourceGroup }} --subscription ${{ env.subscriptionId }} --query "siteConfig.linuxFxVersion" --output tsv) + dockerVersion=$(echo $dockerVersion | cut -d ':' -f2) + echo "DockerVersion $dockerVersion" + + if [[ "$dockerVersion" = "${{ github.run_number }}" ]]; then + exit 0 + fi + + echo "Attempt #$i failed" + sleep 10 + done + + echo "Found a wrong version ($dockerVersion)." + + # It failed a lot of time + exit 1 + + - name: Check deployment + if: success() + id: 'checkDeployment' + continue-on-error: true + run: | + url=${{ env.healthCheckUrl }} + + for i in $(seq 1 10) + do + result=$(curl -i $url 2>/dev/null | grep HTTP/2) + status=${result:7:3} + + if [[ "$status" = "200" ]]; then + echo "200" + exit 0 + fi + + sleep 10 + done + + # It failed a lot of time + exit 1 + # Execute the create-an-issue task if the previous task fails - name: Create Issue if: failure() @@ -48,3 +119,16 @@ jobs: with: title: ${{ env.GITHUB_WORKFLOW }} filename: .github/ISSUE_TEMPLATE_HACK.md + + - name: Swap slot + uses: azure/CLI@v1 + if: success() + with: + inlineScript: | + az webapp deployment slot swap --slot staging --target-slot production --name ${{ env.webappName }} --resource-group ${{ env.resourceGroup }} --subscription ${{ env.subscriptionId }} + + - name: Stop slot + uses: azure/CLI@v1 + with: + inlineScript: | + az webapp stop --slot staging --name ${{ env.webappName }} --resource-group ${{ env.resourceGroup }} --subscription ${{ env.subscriptionId }} diff --git a/apis/userprofile/data/healthcheck/user.js b/apis/userprofile/data/healthcheck/user.js index fec07fc..f24c1fd 100644 --- a/apis/userprofile/data/healthcheck/user.js +++ b/apis/userprofile/data/healthcheck/user.js @@ -17,7 +17,7 @@ module.exports = { res.json({ message: 'healthcheck', status: 'healthy', - additional: true, + version: process.env.VERSION || 1, }); callback; },