Today, I added a step to the GitHub action that just writes secrets to an environmental file required for the next build steps. When I was writing it directly in the pipeline that runs on the creation or update of PR, I recognized that I should do the same thing for other pipelines. So, I started looking for a mechanism that would help me do that more efficiently.
GitHub Action has an approach to composing steps from different actions in one workflow.
name: Environment configuration
description: Setup keys, secrets, variables needed for run and build project
inputs:
USER_LOGIN_DEV:
required: true
description: 'User login for Development'
USER_PASSWORD_DEV:
required: true
description: 'User password for Development'
runs:
using: "composite"
steps:
- name: Set up Flutter
uses: subosito/flutter-action@v2
with:
channel: 'stable'
cache: true
- name: Setup secrets for prod and dev environment
shell: bash
run: |
mkdir -p assets/env
echo "USER_LOGIN=${{ inputs.USER_LOGIN_DEV }}" >> assets/env/dev.env
echo "USER_PASSWORD=${{ inputs.USER_PASSWORD_DEV }}" >> assets/env/dev.env
In difference to a regular workflow, we should add using: "composite"
, you should add the shell
property, which specifies the type of shell that will be used to execute the command.
This file is located by path .github/workflows/environment/action.yml
in my project. Now, If you want to use this step in your workflow, you should add, as in this example:
name: Code checks on Pull Request
on:
pull_request:
branches:
- 'develop'
jobs:
analyze:
name: Code checks on Pull Request
runs-on: ubuntu-latest
steps:
- name: checkout
uses: nschloe/action-cached-lfs-checkout@v1
with:
submodules: recursive
fetch-depth: 0
- name: Configure environment
uses: ./.github/workflows/environment
with:
USER_LOGIN_DEV: ${{ secrets.USER_LOGIN_DEV }}
USER_PASSWORD_DEV: ${{ secrets.USER_PASSWORD_DEV }}
Important moment, you should provide the correct path to the file with action, that you want to include, in our example, it should be ./.github/workflows/environment
. Also, in this folder should be a file with action that has the name action.yml
or action.yaml
.
Possibility of composition is a good way to be more flexible and avoid repeating in different workflows. I found out one limitation: the context of the workflow is unavailable during execution, and you can't get access to GitHub secrets, for instance.
You can find me in X, GitHub or LinkedIn. Thanks for your time and see you in next post.
Top comments (0)