Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the base64 encoded scripts in the workflow files #9

Open
ConorMacBride opened this issue Mar 2, 2022 · 9 comments
Open

Remove the base64 encoded scripts in the workflow files #9

ConorMacBride opened this issue Mar 2, 2022 · 9 comments

Comments

@ConorMacBride
Copy link
Member

To run the Python scripts within the reusable workflows we checkout the main branch of this repository. However, this means that if a PR updates a Python script the script that the PR CI tests is the version in the main branch and no the PR branch. Need to change the following in each workflow:

- uses: actions/checkout@v2
  with:
    repository: 'OpenAstronomy/github-actions-workflows'
@ConorMacBride ConorMacBride added the bug Something isn't working label Mar 2, 2022
@ConorMacBride
Copy link
Member Author

Actually, the PR will test the Python script from the PR branch because 'OpenAstronomy/github-actions-workflows' is the default value and in that case the current ref is used.

However, if the reusable workflow is called by a different repository it will checkout the default branch of OpenAstronomy/github-actions-workflows and not e.g. refs/tags/v1 if the workflow was referenced as ...tox.yml@v1. See https://git.luolix.topmunity/t/reusable-workflows-get-the-ref-inside-the-called-workflow/224109

We could ask the user to pass the ref they used as an input to the reusable workflows although that is not ideal. Hopefully there will be a better solution soon.

@astrofrog
Copy link
Contributor

An ugly solution would be that when we are ready to tag, we add a commit that hard-codes the ref of the repo to check out, e.g. v1.0.2, and then tag that commit v1.0.2, then add a commit that removes the ref again. I think it would work, although is pretty ugly.

@astrofrog
Copy link
Contributor

astrofrog commented Mar 13, 2022

Another solution, not very elegant but which would at least work would be to embed the Python scripts inside the workflow YAML, for instance:

echo "
<contents of tox_matrix.py>
" > tox_matrix.py
python tox_matrix.py ...

Yes this is ugly but it might be that the Python script doesn't need to be updated too often so it might not be that bad.

@astrofrog
Copy link
Contributor

See #20 for a proof of concept

@astrofrog
Copy link
Contributor

@ConorMacBride should we close this?

@ConorMacBride
Copy link
Member Author

This is no longer an issue so maybe just close for less noise. But we should switch to a solution that doesn't involve duplication if/when GitHub adds such a feature.

@Cadair Cadair reopened this Dec 3, 2024
@Cadair
Copy link
Member

Cadair commented Dec 3, 2024

I reopened this prompted by this comment from @lpsinger.

It appears that maybe GitHub now provide the path and workflow ref inside the github context: https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/accessing-contextual-information-about-workflow-runs

image

I am not 100% sure if this means that we can access this inside the reuseable workflow, but maybe it's enough that we can checkout the ref and we can get rid of the dirty hack of base64 encoding the scripts?

@Cadair Cadair removed the bug Something isn't working label Dec 5, 2024
@Cadair Cadair changed the title Reference the reusable workflow repo inside the workflow Remove the base64 encoded scripts in the workflow files Dec 5, 2024
@ConorMacBride
Copy link
Member Author

actions/toolkit#1264 seems to suggest it's still not possible via the github context. I'm guessing the github.workflow_ref might refer to the workflow that calls the reusable workflow. Looking through some of the workarounds mentioned in the issue we should be able to use the GitHub API.

When I run the following in the GitHub CLI, I get d68193b68216da64eafaa618f53c59f5d271c56e, which is correct:

gh api repos/astropy/astropy/actions/runs/12186371008 \
    -q '.referenced_workflows | 
        map( 
            select(
                .path | 
                startswith("OpenAstronomy/github-actions-workflows/.github/workflows/tox.yml@")
            )
        ) | 
        .[0].sha'

In a GitHub Action we should be able to do: repos/${{ github.repository }}/actions/runs/${{ github.run_id }}.

However, if someone is calling the same reusable workflow twice in the same workflow file, we'll get multiple results and I don't think we can distinguish which is which. That would only be an issue if they are using multiple versions. The code above just takes the sha from the first match. We could detect multiple versions and just error out saying it's unsupported.

@Cadair
Copy link
Member

Cadair commented Dec 6, 2024

That's annoying. I quite often change one version (or repo) in a workflow while I am testing stuff but I guess I am not a usual usecase in doing that.

An alternative thought I had is if it would be possible to inline the script in plain text?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants