Azure / k8s-deploy

GitHub Action for deploying to Kubernetes clusters
MIT License
252 stars 103 forks source link

Bug: cannot use set-context as reusable workflow #310

Open Krikooo opened 6 months ago

Krikooo commented 6 months ago

What happened?

Hello folks!

I tried to the set-context as an reusable job (https://docs.github.com/en/actions/using-workflows/reusing-workflows) like this:

name: k8s_connection_to_cluster
on:
  workflow_call:
jobs:
  connection:
    name: Setup k8s client
    runs-on: self-hosted
    steps:
        - name: Setup k8s client
          uses: azure/setup-kubectl@v3
          with:
            version: v1.28.6 # default is latest stable
          id: install
        - name: Connect to k8s cluster
          uses: azure/k8s-set-context@v3
          with:
            method: kubeconfig
            kubeconfig: ${{ secrets.K8S_PROD_CONFIG }}
            context: kubernetes-admin@kubernetes # current-context from kubeconfig is used as default

And then call it in my workflow like this:


name: Deploy cronjobs on k8s
on:
  push:
    branches:
      - master
    paths:
      - 'infra/kubernetes/**'
  workflow_dispatch:

jobs:
    setup_kubectl:
      name: Setup client & connect to cluster
      uses: foobar/infrastructure/.github/workflows/deploy_kubernetes_connection.yaml@main
      secrets: inherit

    kubernetes:
      needs: setup_kubectl
      runs-on: self-hosted

      steps:
        - uses: actions/checkout@v4
        - name: Create old envfile as secret
          uses: azure/k8s-create-secret@v2
          with:
            namespace: foobar
            secret-type: 'generic'
            secret-name: envfile-old
            string-data: ${{ secrets.K8S_ENV_OLD_CRON }}
        - name: Deploy configmap yml files on k8s
          uses: Azure/k8s-deploy@v4
          with:
            namespace: 'foobar'
            manifests: |
                infra/kubernetes/configmap.*.yml
        - name: Deploy cronjob yml files on k8s
          uses: Azure/k8s-deploy@v4
          with:
            namespace: 'foobar'
            manifests: |
                infra/kubernetes/cronjob.*.yml
    clean:
      if: always()
      needs: kubernetes
      uses: foobar/infrastructure/.github/workflows/deploy_docker_clean.yaml@main```

But when the workflow had run and I get this:

Error: Error: Cluster context not set. Use k8s-set-context/aks-set-context action to set cluster context



### Version

- [X] I am using the latest version

### Runner

self-hosted

### Relevant log output

##[debug]Evaluating condition for step: 'Create old envfile as secret'
##[debug]Evaluating: success()
##[debug]Evaluating success:
##[debug]=> true
##[debug]Result: true
##[debug]Starting: Create old envfile as secret
##[debug]Loading inputs
##[debug]Evaluating: secrets.K8S_ENV_OLD_CRON
##[debug]Evaluating Index:
##[debug]..Evaluating secrets:
##[debug]..=> Object
##[debug]..Evaluating String:
##[debug]..=> 'K8S_ENV_OLD_CRON'
##[debug]=> '***
##[debug]'
##[debug]Result: '***
##[debug]'
##[debug]Loading env
Run azure/k8s-create-secret@v[2](https://github.com/foobar/foobar/actions/runs/8068795893/job/22068903639#step:4:2)
  with:
    namespace: foobar
    secret-type: generic
    secret-name: envfile-old
    string-data: ***

Error: Error: Cluster context not set. Use k[8](https://github.com/foobar/foobar/actions/runs/8068795893/job/22068903639#step:4:8)s-set-context/aks-set-context action to set cluster context
##[debug]Node Action run completed with exit code 1
##[debug]Finishing: Create old envfile as secret
OliverMKing commented 6 months ago

The set-context action downloads the Kubectl context to the Job. Its context only applies to the GitHub Job it runs in. You are running k8s-deploy in a separate Job.