GitHub Action - reusable workflow: Run a github actions workflow in the context of the called workflow - github

I got a "caller" workflow and a "called" workflow.
Caller workflow:
name: caller
on:
workflow_dispatch:
jobs:
call-another-workflow:
uses: project/called/.github/workflows/main.yml#main
Called workflow:
name: called
on:
workflow_dispatch:
workflow_call:
jobs:
run_python_script:
runs-on: [ self-hosted ]
steps:
# Following step will be executed in the caller context (should be executed in the called context)
- uses: actions/checkout#v3
- name: Setup python
uses: actions/setup-python#v4
with:
python-version: '3.9'
# Cannot find requirements.txt (wrong repository is checked out)
- name: Install python packages
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Execute the python script
run: python some_python_code.py
Error: <path> can't open file <path> [Errno 2] No such file or directory
-> The wrong repository is checked out (caller repo is checked out instead of the called which is needed). If I trigger the called workflow manually (the 'actions/checkout#v3' is checking out the right repo) the workflow runs successfully.
The caller workflow should 'just' trigger the called workflow in a way, that the workflow runs like it was triggered manually.
Unfortunately the github actions documentation says:
If you reuse a workflow from a different repository, any actions in the called workflow run as if they were part of the caller workflow. For example, if the called workflow uses actions/checkout, the action checks out the contents of the repository that hosts the caller workflow, not the called workflow.
(reference: https://docs.github.com/en/actions/using-workflows/reusing-workflows#overview)
-> Do we have the possibility to just trigger the workflow in a way that it is running like I triggered it manually?
Alternatively, I can trigger the workflow from another workflow via web interface. But then it gets messy. Some ideas?

Related

Github workflows action continuous deployment not working

I want to setup continuous deployment pipeline between Github and AWS Lambda. For this, I've added main.yml file # myrepo/.github/workflows/main.yml
This is my main.yml file
name: deploy to lambda
on:
# Trigger the workflow on push or pull request,
# but only for the main branch
push:
branches:
- main
jobs:
deploy_source:
name: deploy lambda from source
runs-on: ubuntu-latest
steps:
- name: checkout source code
uses: actions/checkout#v1
- name: default deploy
uses: appleboy/lambda-action#master
with:
aws_access_key_id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws_secret_access_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws_region: ${{ secrets.AWS_REGION }}
function_name: my_function
source: function.py
Now when I push changes to main branch nothing happens. It shows There are no workflow runs yet. I have checked the function_name and it is same as the function in AWS Console.
Your deploy_source job has runs-on: ubuntu-latest which tells Actions to use a GitHub Hosted Runner. As per your comment, you are using GitHub Enterprise Server (GHES) which is a virtual appliance on your company's network. At present, GHES does not support using GitHub Hosted Runners (it's worth noting at the time of this writing, it is on the product roadmap for support).
If you wish to run your workflow, you will need to make use of a self-hosted hosted runner. I would recommend working with your GHES administrator to get this workflow to run as there are potentially other settings and/or steps that may need to be modified or taken for this to work.
As tj-cappelletti said in their answer, you should use your hosted runners.
And also, be sure that your pipeline is on your default branch. Otherwise, you wouldn't see it there.
You need to place workflows in .github/workflows/. Note the dot in front of the folder name .github. So for your case the final path should look like this myrepo/.github/workflows/main.yml.

Setup Github action to download a zip file

I have a Google Chrome and Mozilla Firefox extension in a same GitHub repository. They are separated in two branches and I am "exposing" the original URL to download the repository for each branch:
The approach to install a Firefox extension is quite long and messy since it needs to be unzipped and zipped again. So, someone recommended me using Github actions to create a release file from specific branches using this Github action: Zip Release.
According to their documentation I have tried to replicate the YAML file for my use case using the Github action creation wizard and naming that file firefox.yml that created a folder in the repository root: .github/workflows/firefox.yml:
name: Create Archive
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: my-user/the-repo#dev-firefox
- name: Create Firefox Release
uses: thedoctor0/zip-release#main
with:
type: 'zip'
filename: 'dev-firefox.zip'
path: './releases'
exclusions: '*.git* /*node_modules/* .editorconfig /*releases/*'
But after it starts it immediately fails with the following message:
Can't find 'action.yml', 'action.yaml' or 'Dockerfile' under '/home/runner/work/_actions/my-user/the-repo/dev-firefox'. Did you forget to run actions/checkout before running your local action?
I also tried adding - uses: actions/checkout#master just before the line - uses my-user/the-repo#dev-firefox but it won't work.
Not sure how to properly write the workflow YAML configuration. Any suggestions?
You're confusing uses with checking out a repository. uses indicates an action to use, with the part after the # specifying the version of the action. To check out a specific branch of your repo, you can use the checkout action with the ref parameter:
steps:
- uses: actions/checkout#v3.1.0
with:
ref: dev-firefox

GitHub workflow is not triggered after pushing tags?

I have a GitHub workflow as below.
name: Releaser
on:
push:
tags:
- 'v*.*.*'
This workflow will be triggered when I manually push a new tag like v1.1.1-rc1. It works fine.
Now, I want to have another workflow to replace the "manually push".
name: sync-tags
on:
workflow_dispatch:
push:
paths:
- TAGS
jobs:
steps:
- name: foo-example
uses: foo-example
This workflow will be triggered when there's a change made in the TAGS directory. The jobs will create a new tag like v1.1.1-rc1. It works fine as well. But, after the v1.1.1-rc1 is created by the sync-tags, the Releaser is not triggered.
I was wondering why the Releaser can be triggered by manually pushing tags but can't be triggered by tagging from other workflows?
I am having this same problem. It turns out this is intentional behavior from GitHub Actions.
… if a workflow run pushes code using the repository's GITHUB_TOKEN, a new workflow will not run even when the repository contains a workflow configured to run when push events occur.
Explicitly invoking the release workflow works! (Note: this needs GITHUB_TOKEN in the environment, which I happen to do for the entire workflow.)
- name: New tag & launch release process
run: |
echo "Tagging $new_tag"
git tag $new_tag
git push --tags
# Explicitly run our release workflow for this new tag
gh workflow run release.yml --ref $new_tag
My release workflow needed to be enhanced to allow manual runs. The workflow_dispatch: line in the on: section.
on:
push:
tags:
- 'v*.*.*'
workflow_dispatch:
To make sure we're building a release on a tag, I added if: github.ref_type == 'tag' to each job within the release workflow.

Add and run GitHub Actions on feature branch?

I'm currently using the "git-flow" branching model outlined here. Following that model, once I've completed work on a feature branch, I'd like to add new GitHub actions to that branch (for example, to run my feaure's automated tests) before the branch is merged.
Following the branching model, I don't want to define the actions in a workflow file on the default branch before that feature branch is merged into it. Ideally I want to add the actions on the feature branch itself before the merge, but this doesn't appear to work.
I've added the below sample workflow to my feature branch, but GitHub does not detect it. Am I missing something here, or can workflows only detected and run once they're on the default branch? If the latter is true, do people generally merge their branches, then add workflows for them?
# Name workflow
name: Test workflow
# Read only permissions
permissions: read-all
# Triggered once every 15 minutes
on:
workflow_dispatch:
schedule:
- cron: '15 * * * *'
# Listing of jobs to be run
jobs:
# Just output the Python version for now.
python-tests:
name: Python Tests
runs-on: ubuntu-latest
# Use the environment configured with secrets
environment: python-test-environment
# Set the working directory?
defaults:
run:
working-directory: tests
steps:
# Checkout the repository
- name: Checkout
uses: actions/checkout#v2
ref: 'dev-tests'
# Configure Python
- name: Set up Python 3.7
uses: actions/setup-python#v2
with:
python-version: 3.7
# Output the Python version
- name: Display version
run: python -c "import sys; print(sys.version)"
Update: I can see now that the "schedule" trigger only works on the default branch. However, removing it and just using the workflow_dispatch trigger still (on the feature branch YML file) still does not show the workflow on GitHub.

Using GitHub actions like GitLab CI/CD

I just started to migrate all my GitLab repositories to GitHub. I wasn't using GitHub for a while so I stumbled over the - at least for me new feature - GitHub Actions.
Since I just started a new project, I wanted to use GitHub Actions for build and deploy my new application. I've really no idea what I'm doing wrong, I'll attach my workflow file below.
What I want to achieve is, everytime I push to a branch that's not my master and that hasn't the prefix 'release/', I want to execute this build and deploy for my development system. Later I will also setup the same script but for a staging (pre production) system ONLY if I push into a branch with the prefix 'release/' and indeed the same a thrid time for production for the master branch only.
What I'm wondering about is, the actions get - at least for my understanding - executed sporadically. I want an behaviour like I had in GitLab: Everytime I push a feature branch or whatever from my local working machine, the development pipeline should get executed. Then I'll create a pull request. Only if the pipeline was successful, I want to be able to merge. After the merge into a branch (for example feature/... into develop), I would like to automatically execute the pipeline for development.
I'm not even sure if this is possible. Maybe I also didn't understood the concept of actions correctly.
name: Publish Development
on:
push:
branches:
- '**'
- '!master'
- '!release/**'
pull_request:
branches:
- '**'
- '!master'
- '!release/**'
jobs:
build-and-deploy:
name: Build and Deploy
runs-on: ubuntu-latest
steps:
- name: check out repository
uses: actions/checkout#v2
with:
token: ${{ secrets.PRIVATE_ACCESS_TOKEN}}
- name: install dependencies
run: npm install
- name: install dependencies
run: npm --prefix ./functions install ./functions
- name: deploy to firebase
uses: w9jds/firebase-action#master
with:
args: deploy
env:
FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}
PROJECT_ID: ${{ secrets.FIREBASE_PROJECT_ID }}
Thanks!
EDIT: Well it turned out that I just started to try new technology during some service interruption. GitHub was experiencing some issues in their infrastructure. Its working now as expected.
Well it turned out that I just started to try new technology during some service interruption. GitHub was experiencing some issues in their infrastructure. Its working now as expected.