Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to share code between github actions in the same repository?

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.

like image 289
janekb04 Avatar asked Sep 02 '20 21:09

janekb04


People also ask

How do I share variables between jobs in actions GitHub?

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.

Can you share GitHub codes?

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.

How do I call one workflow from another workflow in GitHub Actions?

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.

Do GitHub Actions jobs run in parallel?

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.

How do I set up a secret repository in GitHub?

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.

Is it possible to have multiple repositories for the same file?

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.

Can an internal repository be used in a public repository?

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.

How to merge common code in a repository?

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.


1 Answers

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.

  1. Build your image
  2. Upload to something like s3 or artifactory after your build is done
  3. Download the image after your previous workflow is done. You can make sure that the workflow is done by triggering second pipeline from the first one
  4. Use the download artifacts

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

like image 128
Edward Romero Avatar answered Sep 18 '22 15:09

Edward Romero