1

I'm trying to make use of variables instead of secrets because some of the secrets are not really secrets so wanted to make it nicer, however, I'm having a bit of a fight here.

The first one runs ok because I'm able to use the environment: name: development. However on the build_deploy job, because I'm using a reusable workflow I'm not able to use the environment: name: development.

How can I fix this, or is this even the correct way to do it?

Thanks in advance! :)

name: Deploy 

on:
  workflow_dispatch:

jobs:

  check-env-vars:
    runs-on: ubuntu-latest
    environment:
      name: development

    steps:
      - name : Output vars
        run: |
          echo "cluster-name ${{vars.CLUSTER_NAME}}"
          echo "region ${{vars.REGION}}"
          echo "projectid ${{vars.PROJECT_ID}}"

  build_deploy:
    uses: owner/repo/.github/workflows/build_deploy.yaml@master
    with:
      project-id: ${{ vars.PROJECT_ID }}
      environment: development
      cluster-name: ${{ vars.CLUSTER_NAME }}
      region: ${{ vars.REGION }}
Azeem
  • 11,148
  • 4
  • 27
  • 40
Nino Matos
  • 91
  • 9

1 Answers1

1

because I'm using a reusable workflow I'm not able to use the environment: name: development.

In that case, following your main workflow, the reusable workflow should look like this:

on:
  workflow_call:
    # Map the workflow outputs to job outputs
    inputs:
      environment:
        required: true
        type: string

jobs:
  my-dynamic-env-job:
    runs-on: ubuntu-latest
    environment:
      name: ${{ inputs.environment }}
    steps:
      - run: |
          echo "${{ vars.PROJECT_ID }}"
          echo "${{ vars.CLUSTER_NAME }}"
          echo "${{ vars.REGION }}"

Where the environment input is used to fill the environment.name property of the job set in the reusable workflow.

In that case, you wouldn't need to send the PROJECT_ID, CLUSTER_NAME and REGION as inputs, because they would depend on the environment input sent.

Your Deploy workflow would then look like this:

name: Deploy 

on:
  workflow_dispatch:

jobs:
  check-env-vars:
    runs-on: ubuntu-latest
    environment:
      name: development
    steps:
      - name : Output vars
        run: |
          echo "cluster-name ${{vars.CLUSTER_NAME}}"
          echo "region ${{vars.REGION}}"
          echo "projectid ${{vars.PROJECT_ID}}"

  build_deploy:
    uses: owner/repo/.github/workflows/build_deploy.yaml@master
    with:
      environment: development

I made an example if you want to check (my environment name is other):

GuiFalourd
  • 15,523
  • 8
  • 44
  • 71
  • Hi @Gui, Thanks a lot for your answer! I've actually tried that solution before posting the question here and it didn't work. I believe that as to do with the fact that you have hardcoded the environment other in the file: workflow-tester78.yml . I was expecting that you would only add the environment on the main workflow and then it would look for the variables based on the input.environment passed. But that gave me another idea, which might be creating different jobs per environment as you have to handle the different environment and that would work. I will give it another try! :) – Nino Matos Mar 14 '23 at 11:45
  • The `vars` are hardcoded in the reusable workflow, but the related values will change according to the `environment` selected to run the job, so you could use them the same way in all environments. – GuiFalourd Mar 14 '23 at 13:25