github actions tag add asset - github

I am creating a tag in a github action like so:
- name: create tag
uses: actions/github-script#v6.1.0
with:
script: |
github.rest.git.createRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: 'refs/tags/tag_${{ env.tagname }}',
sha: context.sha
})
Once the tag is created, I can view it in the list of tags for the repo, and associated to it, there are two 'assets', namely both a zip and tar of the source code. I'd like to know if can also add a meta data text file to the assets. For example, I'd like to include in this meta data file, the name of the person who ran the workflow via workflow_dispatch that created the tag.
Thanks to #rethab for suggesting this API:
https://docs.github.com/en/rest/releases/assets#upload-a-release-asset
From it I have taken this code:
curl \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: token <TOKEN>" \
https://uploads.github.com/repos/OWNER/REPO/releases/RELEASE_ID/assets
However there is no place in the post request for me to put the file I am trying to upload. Where should I put this?

Related

GitHub workflow workflow_dispatch missing in actions tab

I created a workflow file and the workflow is not showing up in the GitHub actions tab
name: AZ Deploy Workflow
on:
workflow_dispatch:
inputs:
deploy-environment:
description: 'Environment to deploy to'
required: true
default: 'dev'
image-tag:
description: 'Docker tag to deploy'
required: true
default: 'latest'
any ideas what's the issue.
Earlier I have added other workflow into the .github directory that shows in actions with no issue even if a put a empty file it does but not this one
Make sure your workflow is located in the default branch.
To run a workflow manually, the workflow must be configured to run on the workflow_dispatch event. To trigger the workflow_dispatch event, your workflow must be in the default branch. For more information about configuring the workflow_dispatch event, see "Events that trigger workflows".
For more details, see the Manually running a workflow and about the default branch.
UPD:
Testing it from a branch other than from the main branch can be done using the GitHub REST API:
curl \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer <YOUR-TOKEN>"\
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/OWNER/REPO/actions/workflows/WORKFLOW_ID/dispatches \
-d '{"ref":"test-branch","inputs":{"deploy-environment":"dev","image-tag":"latest"}}'
You can replace WORKFLOW_ID with the workflow file name. For example, you could use main.yaml.
ref - your branch name.
For more details visit the official docs: Create a workflow dispatch event.

Github Workflow - Remove label at the end?

I have a workflow that is triggered when the pull request is labeled (via pull_request_target).
I would to automatically remove the label that triggered the analysis as the last step of that workflow.
How can I do that?
On Github: every pull request is an issue (but not every issue is a pull request).
You can find more informations about this on this stackoverflow answer.
Therefore, you could use the Github API to remove a (specific) label from an issue (or an action doing the same thing) as the last step of your workflow, using the PR number.
Here is the API on Github to remove label from issue (Official Documentation)
If you want to call it directly from the shell in your workflow with curl, it will looks like this:
curl \
-X DELETE \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/<owner>/<repo>/issues/<pr_number>/labels/<label_name>
GuiFalourd does indeed have the correct answer, but I did have to improve on it a bit.
If you want to use the workflow on a private repo, you do need to add an authorization token. Here's an example that uses GitHub context to populate everything besides the label name you want to delete:
curl --silent --fail-with-body \
-X DELETE \
-H 'Accept: application/vnd.github.v3+json' \
-H 'Authorization: token ${{ github.token }}' \
'https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.number }}/labels/YOUR-LABEL'
Also if your label has spaces, use %20 for the whitespace.

how to download specific commit of the file on github

Given a short sha, I want to be able to download the file content of that commit using the github api.
currently i am using following to fetch the content, and it works fine, but how can i download the content for specific sha?
curl -u 'username:password' \
-H 'Accept: application/vnd.github.v3.raw' \
-o output.yaml
-L https://mycompany/api/v3/repos/myorg/myrepo/contents/services/serviceA/helm/manifest.yaml
apparently there is a query parameter ref can be passed to the url
The name of the commit/branch/tag. Default: the repository’s default branch (usually master)

How can a Github workflow read a file on another private repository hosted on the same GitHub account?

I have a private git repository like:
Github.com/acct/repo1
I have a test.json file in another private repository in the same GitHub subscription and account like:
Github.com/acct/repo2/test.json
How can a workflow in repo1 have http get access to the url Github.com/acct/repo2/test.json?
All I need is a simple way to read the content of test.json in a workflow running on repo2.
It works the best for me to read the file using a curl get request.
First of all, you need to get an access token from GitHub. This is described here.
And add it to the secrets for the repository in which the Github Action will run. In your case, this is repo1. Read more about setting secrets.
After that, you can execute the request using curl.
To do this, run the following command:
curl -H 'Authorization: token ${{ secrets.GITHUB_TOKEN }}' \
-H 'Accept: application/vnd.github.v4.raw' \
-L https://api.github.com/repos/OWNER_HERE/REPO_NAME_HERE/contents/FOLDER_OR_FILE_HERE
After execution, the contents of the file will be displayed on the console.
If the contents of the file need to be saved to a variable, then use the following syntax:
VARIABLE_NAME="$(curl -H 'Authorization: token ${{ secrets.GITHUB_TOKEN }}' \
-H 'Accept: application/vnd.github.v4.raw' \
-L https://api.github.com/repos/OWNER_HERE/REPO_NAME_HERE/contents/FOLDER_OR_FILE_HERE)"
Add the -O flag to save the content to a file
curl -H 'Authorization: token ${{ secrets.GITHUB_TOKEN }}' \
-H 'Accept: application/vnd.github.v4.raw' \
-O \
-L https://api.github.com/repos/OWNER_HERE/REPO_NAME_HERE/contents/FOLDER_OR_FILE_HERE
In your case, the step in the .yml file to get the content would look like this:
#...
-name: Get file test.json
run: |
curl -H 'Authorization: token ${{ secrets.GITHUB_TOKEN }}' \
-H 'Accept: application/vnd.github.v4.raw' \
-O \
-L https://api.github.com/repos/acct/repo2/contents/test.json
#...
Please note that the path /contents/ must remain after REPO_NAME_HERE in the request, and after that you can specify the path to the FOLDER_OR_FILE_HERE file.

Invoke GitHub Actions workflow manually and pass parameters

I want to pass some dynamic parameters and invoke my GitHub Actions workflow manually (ideally via some API). Is this possible?
With the workflow_dispatch event trigger, you can do the manual triggers easily.
Flow:
on:
workflow_dispatch:
inputs:
logLevel:
description: 'Log level'
required: true
default: 'warning'
tags:
description: 'Test scenario tags'
Manual trigger screenshot:
Blog post announcement reference, https://github.blog/changelog/2020-07-06-github-actions-manual-triggers-with-workflow_dispatch/
I think the correct answer to this is using a repository_dispatch NOT a workflow_dispatch.
Only repository dispatch allows you to trigger a workflow from an API call.
Docs
Hand Holding example
Summary:
URL Endpoint:
https://api.github.com/repos/{owner}/{repo}/dispatches
Example cURL call:
curl -X POST -H "Accept: application/vnd.github.v3+json" -H "authorization: Bearer <token>" https://api.github.com/repos/{owner}/{repo}/dispatches -d '{"event_type": "type1","client_payload": {"key1": "Hello from CRUD"}}'
To trigger a workflow_dispatch via API, the documentation can be found at https://docs.github.com/en/free-pro-team#latest/rest/reference/actions#create-a-workflow-dispatch-event
POST /repos/{owner}/{repo}/actions/workflows/{workflow_id}/dispatches
where {workflow_id} can also be the filename of the workflow (which makes things a lot easier).
An example curl from the documentation:
curl \
-X POST \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/octocat/hello-world/actions/workflows/42/dispatches \
-d '{"ref":"ref"}'