How do I add Label, Milestone and Project with Actions?

I would like to know how to add a label, a milestone and edit a project after a push or pull request with actions, but I don’t understand GitHub documentations, is there an easier way? Thank you!

  1. I chose a random name ; is there a necessity to call workflow files in a specific way?
https://github.com/ojedavidea/hello-world/.github/workflows/test29.yml

  1. This part seems unchanged across almost all workflow file types, is it okay to assume using it exactly as is for all of them in the future will always result with successful runs?
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
    - uses: actions/checkout@v2

What does $GITHUB_WORKSPACE mean? Is it a way to say that the workflow will work for all files from the on: part?

I am stuck at this step :

X check the value for name 0s
##[error]Process completed with exit code 127.
1.   Run .....               # check the name value, if it's not 'Octocat', exit 1 to fail the workflow.
4. /home/runner/work/_temp/20252ea1-0825-4109-9eec-0f613c17d951.sh: line 1: .....: command not found
5. ##[error]Process completed with exit code 127.

it seems that this ..... and/or this # check the name value, if it's not 'Octocat', exit 1 to fail the workflow. are not recognized lines of code.

The “|” is to write multiple line scripts/commands. It is a syntax in yaml file.
You need to write your own bash scripts to reach your requirement.
Lines start with # is comment, there will not be executed .
There is my example when workflow is triggered by push event:

on:
  push:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
      with: 
        fetch-depth: 2
    - run: |
        file=$( git diff-tree --no-commit-id --name-only -r ${{ github.sha }} )
        echo $file
        line=$(sed -n '1p' $file)
        SUB='Octocat'
        if grep -q "$SUB" <<< "$line"; 
        then
           echo "Octocat's there"
        else
           echo "No Octocat"
           exit 1
        fi

The code could just be used when workflow is triggered by push event. When the workflow is triggered by pull_request event, github.sha will be the merge commit, not the commit you just pushed.


I modified my workflow , and I add if conditional to distinguish trigger event.

name: example
on:
  push:
  pull_request:
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
      if: github.event_name == 'push'
      with: 
        fetch-depth: 2
    - uses: actions/checkout@v2
      if: github.event_name == 'pull_request'
      with: 
        ref: ${{github.head_ref}}
        fetch-depth: 2
    - name: Dump GitHub context
      env:
          GITHUB_CONTEXT: ${{ toJson(github) }}
      run: echo "$GITHUB_CONTEXT" 
    - name: push event     
      if: github.event_name == 'pull_request'
      run: |
        file=$( git diff-tree --no-commit-id --name-only -r ${{ github.event.after }} )
        echo $file
        line=$(sed -n '1p' $file)
        echo $line
        SUB='name: Octocat'
        if grep -q "$SUB" <<< "$line"; 
        then
           echo "Octocat's there"
        else
           echo "No Octocat"
           exit 1
        fi
    
    - name: push event     
      if: github.event_name == 'push'
      run: |
        file=$( git diff-tree --no-commit-id --name-only -r ${{ github.sha }} )
        echo $file
        line=$(sed -n '1p' $file)
        SUB='name: Octocat'
        if grep -q "$SUB" <<< "$line"; 
        then
           echo "Octocat's there"
        else
           echo "No Octocat"
           exit 1
        fi

Could someone please tell me what I am doing wrong? I am struggling.

name: action

on:    
  pull_request:
    
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - name: checkout
      uses: actions/checkout@v2

    - name: Check value and label milestone and comment
      run: |
           STR="several words", "several", "words"
           file=$( git diff-tree --no-commit-id --name-only -r ${{ github.event.after }} )
           echo $file
           line=$(sed -n '1p' $file)
           echo $line
           SUB='name: Octocat'
           if grep -q "$SUB" <<< "$line"; 
           if [ `grep -c "$STR" 0/1` -ne '0' ];
           then
             echo "Found"
             curl --request PATCH \
             --url ${{ github.event.pull_request.issue_url }} \
             --header 'authorization: token ${{ secrets.GITHUB_TOKEN }}' \
             --data '{"milestone": Name}'
             curl --request PATCH \
             --url ${{ github.event.pull_request.issue_url }} \
             --header 'authorization: token ${{ secrets.GITHUB_TOKEN }}' \
             --data '{"labels": [ "one" ]}'
             --data '{"labels": [ "blue" ]}'
             curl --request POST \
             --url ${{ github.event.pull_request.issue_url }}/comments \
             --header 'authorization: token ${{ secrets.GITHUB_TOKEN }}' \
             --data '{"body": "You may continue"}'
           else
             echo "Not Found"
             curl --request PATCH \
             --url ${{ github.event.pull_request.issue_url }} \
             --header 'authorization: token ${{ secrets.GITHUB_TOKEN }}' \
             --data '{"labels": [ "try again" ]}'
             curl --request POST \
             --url ${{ github.event.pull_request.issue_url }}/comments \
             --header 'authorization: token ${{ secrets.GITHUB_TOKEN }}' \
             --data '{"body": "You may try again"}'
             exit 1
           fi

@ojedavidea I changed the title of your topic to make it clearer that you were requesting assistance and not simply reporting a bug or making a complaint. Doing things like this makes it more likely that you’ll get the assistance you need.

As for why I didn’t attempt to answer your question in that moment, I wasn’t certain what it was that you were asking. But there are others that also answer Actions questions such as @yanjingzhu that know more than I do, so that was also a decision that I made trying to get you the best possible assistance and answers.

The topic title is for you to communicate what your topic is about, not to create noise with all caps pleas for attention or make off-topic comments about how we attempt to make our forum run more smoothly for the benefit of everyone. Please be mindful of that in the future and please do not edit the topic title again.

How do I add more labels at once with a PR with this:

             curl --request PATCH \
             --url ${{ github.event.pull_request.issue_url }} \
             --header 'authorization: token ${{ secrets.GITHUB_TOKEN }}' \
             --data '{"labels": [ "letters" ]}'

when I try with more lines of --data '{"labels": [ "qweryriw" ]}' it deletes former labels and add the last one

@ojedavidea,

According to the following description from the docs about the Pull Request API,

Every pull request is an issue, but not every issue is a pull request. For this reason, “shared” actions for both features, like manipulating assignees, labels and milestones, are provided within the Issues API.

you should use the API “Add labels to an issue” to add labels on a pull request.
If you want to add multiple labels to a PR or issue, you can set the request body of the API like as below:

{"labels":["label-1", "label-2", "label-3"]}

Below is a simple example as reference:

jobs:
  add_labels:
    name: Add labels
    runs-on: ubuntu-latest
    steps:
      - name: Add labels to PR
        if: ${{ github.event_name == 'pull_request' }}
        run: |
          curl --request POST \
          --url 'https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/labels' \
          --header 'Authorization: token ${{ secrets.GITHUB_TOKEN }}' \
          --header 'Accept: application/vnd.github.v3+json' \
          --data-raw '{"labels":["skip CI", "question", "wontfix"]}'

result:

2 Likes