Let's say I want two workflows build.yml
and release.yml
in my repo. The first one should build the project (let's say using CMake) and the second one should build the project and create a GitHub Release with the built binary.
The project building code is duplicated between the two files. How could it be shared between them, instead of being copy-pasted and having to be manually updated in two places at once?
I have not been able to find a way to achieve any kind of code sharing behavior yet between separate workflows or even jobs within the same workflow.
We don't support to share variables between jobs. As you metioned artifacts, we would suggest you use it . You can write the version into a file , upload it as artifact in one job and then download the artifacts in the other job , read the file content.
Share code with GitHub gists Gists let you share code snippets, entire files, or even applications. You can also use gists to save and share console output when running, debugging, or testing your code. Each gist is a repository that can be cloned or forked by other people.
You call a reusable workflow by using the uses keyword. Unlike when you are using actions within a workflow, you call reusable workflows directly within a job, and not from within job steps.
You can configure a GitHub Actions workflow to be triggered when an event occurs in your repository, such as a pull request being opened or an issue being created. Your workflow contains one or more jobs which can run in sequential order or in parallel.
On GitHub.com, navigate to the main page of the organization. Under your organization name, click Settings. In the left sidebar, click Secrets. Click New secret. Type a name for your secret in the Name input box. Enter the Value for your secret. From the Repository access dropdown list, choose an access policy. Click Add secret.
Only one repository would have the "real" file. The other repository would only contain a symlink to a file that may or may not exist on a different filesystem. Windows systems don't have Linux/Unix-style symlinks, so interoperability may be an issue.
Any actions or workflows stored in the internal repository can be used in workflows defined in other private and internal repositories owned by the same organization, or by any organization owned by the enterprise. Actions and workflows stored in internal repositories cannot be used in public repositories.
The proper way to deal with common code is usually through submodules or subtree merging. However, if you have file assets that are (and will remain) identical, then you can leverage symlinks on filesystems that support them. There are at least three downsides to this approach: Only one repository would have the "real" file.
Option 1: Answer to question above
There is not a current way to do this within github actions directly. There is a couple of steps that you need to go through to make it work.
Per github actions
If you need to access artifacts from a previous workflow run, you'll need to store the artifacts somewhere. For example, you could run a script at the end of your workflow to store build artifacts on Amazon S3 or Artifactory, and then use the storage service's API to retrieve those artifacts in a future workflow.
Resources
Option 2: Work around so that we don't have to deal with external services but stay in GitHub
Run two jobs in the same workflow. One job for build and another job for release. The idea here is that you will only run job release when job build is completes successfully. Also we will need to use artifacts to pass data between jobs
Resource
build:
...
steps:
- name: Build image
run: make ...blah blah
- name: Upload artifact
uses: actions/upload-artifact@v2
with:
name: name-of-build
path: ./path/to/artifact
release:
needs: build # this basically says only run if build job is successful
steps:
- name: Download artifact
uses: actions/download-artifact@v2
with:
name: name-of-build
- name: Release image
...
Resource
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With