skip to Main Content

I have a github actions workflow that in there I need to do 4 deploys, but all of then are in different folders. So I need that when some changes are made in an unique repository, it just makes deploy on this depository also.

deploy:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout code
      uses: actions/checkout@v3

    - name: Authenticate to Google Cloud
      uses: google-github-actions/auth@v1
      with:
        credentials_json: ${{ secrets.GCP_SA_DEPLOY_KEY }}

    - name: Set up Google Cloud SDK
      uses: google-github-actions/setup-gcloud@v1
      with:
        project_id: ${{ secrets.GCP_PROJECT_ID }}
        install_components: 'beta'

    - name: Deploy Function 1
(deploy it just if any changes are made in ./Function1 repository)
      run: ...

    - name: Deploy Function 2
(deploy it just if any changes are made in ./Function2 repository)
      run: ...
          
    - name: Deploy Function 3
(deploy it just if any changes are made in ./Function3 repository)
      run: ...

    - name: Deploy Function 4
(deploy it just if any changes are made in ./Function4 repository)
      run: ...

I tried using:
if: contains(github.event.head_commit.message, './Function1')

but nothing happens.

Hope you could understand and help me! Thanks!

2

Answers


  1. some days ago, I used multiple deploy like this. In my case I also make dependent on each other..

    name: SSH Deploy
    
    on:
      push:
        branches:
          - main
    
    jobs:
      deploy1:
        runs-on: ubuntu-latest
        
        steps:
        - name: Checkout code
          uses: actions/checkout@v2
    
        - name: Setup SSH
          uses: webfactory/[email protected]
          with:
            ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
            
        - name: SSH into server 1
          run: |
            <commands>
    
      deploy2:
        needs: deploy1
        runs-on: ubuntu-latest
        
        steps:
        - name: Checkout code
          uses: actions/checkout@v2
    
        - name: Setup SSH
          uses: webfactory/[email protected]
          with:
            ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
    
        - name: SSH into server 2
          run: |
            <commands>
    
    Login or Signup to reply.
  2. You could use reusable workflow files. The following should give you an idea (of course you have to adapt it to your actual build steps).

    First create a workflow file, e.g. .github/workflows/wf-deploy-function.yml:

    name: My Deploy Workflow
    on:
      workflow_call:
        inputs:
          project-file:
            required: true
            type: string
          project-folder:
            required: true
            type: string
        secrets:      
          gcp_sa_deploy_key:
            required: true
          gcp_project_id:
            required: true
    jobs:
      build-and-deploy:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v4
            name: Checkout the code
          - name: Authenticate to Google Cloud
            uses: google-github-actions/auth@v1
            with:
              credentials_json: ${{ secrets.gcp_sa_deploy_key }}
          - name: Set up Google Cloud SDK
            uses: google-github-actions/setup-gcloud@v1
            with:
              project_id: ${{ secrets.gcp_project_id }}
              install_components: 'beta'
          - name: Deploy Function
            run: ... (use your inputs here, e.g. ${{ github.workspace }}${{ inputs.project-folder }} )
    

    Now create a yml file for each of your Function apps.

    E.g. .github/workflows/DeployFunction1.yml

    name: Deploy Function 1
    on:
      push:
        branches:
          - main
        paths:
          - 'Function1/**'
          - 'MyCommonCode/**'
    jobs:
      deploy-function:
        uses: ./.github/workflows/wf-deploy-function.yml
        with:
          project-file: 'Function1/myProjectFile'
          project-folder: 'Function1'
        secrets:
          gcp_sa_deploy_key: ${{ secrets.GCP_SA_DEPLOY_KEY }}
          gcp_project_id: ${{ secrets.GCP_PROJECT_ID }}
    

    E.g. .github/workflows/DeployFunction2.yml

    name: Deploy Function 2
    on:
      push:
        branches:
          - main
        paths:
          - 'Function2/**'
          - 'MyCommonCode/**'
    jobs:
      deploy-function:
        uses: ./.github/workflows/wf-deploy-function.yml
        with:
          project-file: 'Function2/myProjectFile'
          project-folder: 'Function2'
        secrets:
          gcp_sa_deploy_key: ${{ secrets.GCP_SA_DEPLOY_KEY }}
          gcp_project_id: ${{ secrets.GCP_PROJECT_ID }}
    

    For each of these files you can define the paths on which a change will trigger the workflow.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search