Anatolii Vorobiov
Anatolii Vorobiov

Reputation: 179

use selected branch on workflow_dispatch in github actions

enter image description here

Hi everyone, I am building Github actions workflow to use master and develop branches. I know that i can check out branch by reusing actions like below, but how do i actually pass variable form the manual workflow_dispatch dropdown?

   uses: actions/checkout@v2
   with:
     ref: develop

Since workflows can be created only in default branch, the only workaround is to create trigger workflow that is reusing core workflow and passing branch as parameter.

UPDATE Here is the code for master (production) branch

name: Trigger ECR deploy


on:
  release:
      types: [published]
  workflow_dispatch:
  
jobs:
  deploy-terraform:
    uses: <reusable-workflow-path>
    with:
      AWS_REGION: "ap-south-1"                  
      ECR_REPOSITORY: "repo-name-here"
      BRANCH: "master"
    secrets:
      AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
      AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}

This is another file for staging env

name: Trigger ECR deploy


on:
  release:
      types: [published]
  workflow_dispatch:
  
jobs:
  deploy-terraform:
    uses: <reusable-workflow-url>
    with:
      AWS_REGION: "ap-south-1"                  
      ECR_REPOSITORY: "repo-name-here"
      BRANCH: "develop"
      ENVIRONMENT: "staging"
    secrets:
      AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
      AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}

Both these files are in master branch at the moment. I recreated staging file in develop branch (i kept the name of the file the same). However, when i go to trigger workflow i still get this error as seen on screenshot. The parameters that I pass to reusable workflow is like a workaround.

Upvotes: 7

Views: 25692

Answers (3)

Anatolii Vorobiov
Anatolii Vorobiov

Reputation: 179

This issue has been resolved a while ago. As Sir GuiFalourd mentioned in the comments, files indeed should be in both branches to be accessible. I cannot say exactly why that didn't work, but I realized that to allow workflows in staging and prod branches to function properly and don't cause overwrite on merge (unless we ignore it), I needed to create a universal workflow that allows conditional run.

Upvotes: 2

codeaprendiz
codeaprendiz

Reputation: 3195

You can use the following to check.

official docs - workflow_dispatch

on:
  workflow_dispatch:
    inputs:
      logLevel:
        description: 'Log level'
        required: true
        default: 'warning'
        type: choice
        options:
        - info
        - warning
        - debug
      tags:
        description: 'Test scenario tags'
        required: false
        type: boolean
      environment:
        description: 'Environment to run tests against'
        type: environment
        required: true

jobs:
  log-the-inputs:
    runs-on: ubuntu-latest
    steps:
      - run: |
          echo "Log level: $LEVEL"
          echo "Tags: $TAGS"
          echo "Environment: $ENVIRONMENT"
        env:
          LEVEL: ${{ inputs.logLevel }}
          TAGS: ${{ inputs.tags }}
          ENVIRONMENT: ${{ inputs.environment }}

Upvotes: -1

ojef1
ojef1

Reputation: 51

Can you confirm that your workflow files are in .github/workflows/<workflow.yml>?

Also, to answer your original question about being presented with variable inputs in the GUI, you will need to define the variables within the on.workflow_dispactch section (https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#workflow_dispatch):

name: Trigger ECR deploy


on:
  release:
      types: [published]
  workflow_dispatch:
    inputs:
      AWS_REGION:
        description: 'AWS Region to deploy in'
        required: true
        default: 'ap-south-1'
        type: choice
        options:
        - ap-south-1
        - ap-south-2
        - ....
      ECR_REPO:
        description: 'ECR repository'
        required: true
        type: string
      BRANCH:
        description: 'Branch to use'
        required: true
        default: 'master'
        type: choice
        options:
        - master
        - develop
  
jobs:
  deploy-terraform:
    uses: <reusable-workflow-path>
    with:
      AWS_REGION: ${{ inputs.AWS_REGION }}                  
      ECR_REPOSITORY: ${{ inputs.ECR_REPO }}
      BRANCH: ${{ inputs.BRANCH }}
    secrets:
      AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
      AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}

Upvotes: 4

Related Questions