Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variable as Namespace #45

Closed
hikarukujo opened this issue Jun 18, 2020 · 3 comments
Closed

Variable as Namespace #45

hikarukujo opened this issue Jun 18, 2020 · 3 comments

Comments

@hikarukujo
Copy link

Maybe I'm missing something simple, but I cannot figure this out for the life of me. I'm trying to set the namespace to dev if the branch is not "master" and I have the branch detection working right now. But it looks like k8s-deploy will not take a variable as the namespace name. Maybe it's a syntax error I'm missing? Here is my yaml:

on:
  push:
    branches:
    - dev
    - master

name: CI/CD Workflow

jobs:
    build-and-deploy:
        runs-on: ubuntu-latest
        steps:
        # checkout the repo
        - name: 'Checkout GitHub Action'
          uses: actions/checkout@master
          
        - name: 'Login via Azure CLI'
          uses: azure/login@v1
          with:
            creds: ${{ secrets.AZURE_CREDENTIALS }}
        
        - name: 'Build and push image to ACR'
          uses: azure/docker-login@v1
          with:
            login-server: ${{ secrets.REGISTRY_LOGIN_SERVER }}
            username: ${{ secrets.REGISTRY_USERNAME }}
            password: ${{ secrets.REGISTRY_PASSWORD }}
        - run: |
            docker build . -t ${{ secrets.REGISTRY_LOGIN_SERVER }}/revio-calendar-backend:${{ github.sha }}
            docker push ${{ secrets.REGISTRY_LOGIN_SERVER }}/revio-calendar-backend:${{ github.sha }}

        - name: 'Set context for AKS'
          uses: Azure/aks-set-context@v1
          with:
            creds: ${{ secrets.AZURE_CREDENTIALS }}
            cluster-name: ${{ secrets.CLUSTER_NAME }}
            resource-group: ${{ secrets.RESOURCE_GROUP }}

        - name: 'Create AKS Secret'
          uses: Azure/k8s-create-secret@v1
          with:
            container-registry-url: ${{ secrets.REGISTRY_LOGIN_SERVER }}
            container-registry-username: ${{ secrets.REGISTRY_USERNAME }}
            container-registry-password: ${{ secrets.REGISTRY_PASSWORD }}
            secret-name: c1-k8s-secret

        - name: 'Set Dev Branch'
          env:
            NAMESPACE: dev
          run: echo "$NAMESPACE"
            
        - name: 'Set Master Branch'
          if: endsWith(github.ref, '/master')
          env:
            NAMESPACE: prod
          run: echo "$NAMESPACE"
    
        - name: 'Deploy to Kubernetes'
          uses: Azure/k8s-deploy@v1
          with:
            namespace: $NAMESPACE
            manifests: |
              manifests/deployment.yml
              manifests/service.yml
            images: |
              ${{ secrets.REGISTRY_LOGIN_SERVER }}/revio-calendar-backend:${{ github.sha }}
            imagepullsecrets: |
              c1-k8s-secret

I succeed on all steps but the last, which says it cannot find a namespace named "$NAMEPSACE":

Error from server (NotFound): error when creating "/tmp/Service_revio-calendar-backend_1592439878791": namespaces "$NAMESPACE" not found

Is there a way to use a variable to determine the namespace so I can avoid one yaml for master and one for other branches?

@thesattiraju
Copy link
Contributor

You need to use namespace like ${{ env.NAMESPACE }} instead of $NAMESPACE. Given that the environment variable is not available in that context, it failed because of that.

When you use it like ${{ env.NAMESPACE }} this value gets replaced with the actual value of the variable and passed to the action.

@thesattiraju thesattiraju added this to To be Triaged in GitHub Actions for Azure via automation Jun 18, 2020
@hikarukujo
Copy link
Author

hikarukujo commented Jun 18, 2020

That didn't do the trick, it seems to ignore when I do that:

/usr/bin/kubectl apply -f /tmp/Deployment_revio-calendar-frontend_1592492995710,/tmp/Service_revio-calendar-frontend_1592492995710,/tmp/Ingress_revio-calendar-frontend-ingress_1592492995710 --namespace default
deployment.extensions/revio-calendar-frontend created
error: the namespace from the provided object "dev" does not match the namespace "default". You must pass '--namespace=dev' to perform this operation.
service/revio-calendar-frontend created
##[error]Error: error: the namespace from the provided object "dev" does not match the namespace "default". You must pass '--namespace=dev' to perform this operation.

This was with the following config:

    - name: 'Deploy to Kubernetes'
          uses: Azure/k8s-deploy@v1
          with:
            namespace: ${{ env.NAMESPACE }}
            manifests: |
              manifests/deployment.yml
              manifests/service.yml
              manifests/ingress-dev.yml
            images: |
              ${{ secrets.REGISTRY_LOGIN_SERVER }}/revio-calendar-frontend:${{ github.sha }}
            imagepullsecrets: |
              c1-k8s-secret
              - uses: actions/checkout@v2

@thesattiraju
Copy link
Contributor

You have to declare that environment globally. I suppose that value is not added to the env context because of where you're declaring it.

Consider adding an environment section at the workflow level. Something like this: https://github.com/Azure/actions-workflow-samples/blob/master/Kubernetes/build-and-deploy-docker-image-aks-using-manifests.yml#L15

Documentation: https://help.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
GitHub Actions for Azure
  
To be Triaged
Development

No branches or pull requests

2 participants