Github Actions to make changes to a file

Hello, I am learning to use GitHub Actions for a project and I am having difficulties.

How can I use Github Actions to make changes to a file that is in my repository?
I’m doing a gamification project that assigns points to users who commit or other actions in my repository, these points are in a JSON file linked to the user’s name, when the user performs a certain action (such as a commit) this value must be added to that file.

@allisonsampaio ,

To chang the files in the repository, generally you need to setup the steps like as below:

  1. Use the checkout action to check out the repository to the runner machine.

  2. Execute the shell scripts or actions to modify the files.

    • The shell scripts just is like as what you can run in the local machines.
    • Abuout the actions, you can go to the GitHub Marketplace to look for the related actions that can modify the specific types of files that you want to change. If you do not find any available action, you can reference to “Creating actions” to create yourself customized action.
  3. Run the ralated git commands to commit and push the changes to the repository on GitHub.

A simple demo:

jobs:
  job1:
    name: Modifiy repository files
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v2.3.2

      - name: Modifiy the specific files using scripts
        run: |
          --- shell scripts or command lines to execute ---
# If you execute some shell scripts or command lines to modify
# the files, use the 'run' key.
# More details, see "https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idstepsrun"

      - name: Modifiy the specific files using action
        uses: action-owner/action-repo@ref
# If you execute an action to modify the files, use the 'uses' key.
# Just like as the previous step that executes the checkout action.
# More details, see "https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idstepsuses"

      - name: Commit and push changes
        run: |
          git config --global user.name "your username"
          git config --global user.email "your email"

          git add -A
          git commit -m "commit message"
          git push
2 Likes

@allisonsampaio ,
How are things going?
Is my above suggestion helpful to you?
Please try it, and feel free to tell me if you have any update.

Hi @brightran
It helped me a lot, thank you

Do you know how I can access the event data? (as the text of an issue_comment event)

You can add a step like this for debugging purposes (assuming bash as shell):

- name: GitHub Event
  run: |
    cat << 'EOF'
      ${{ toJSON(github.event) }}
    EOF

If you want to react to issue comments, you could trigger a workflow like:

on:
  issue_comment:

Note that this won’t be run for the initial post but only subsequent comments.

To access the raw text of the comment (in Markdown syntax, not converted to HTML), you can use:

${{ github.event.comment.body }}

If the event sub-type is edited, then you can also access the previous text with github.event.changes.body.from.

Full event object example
  {
  "action": "edited",
  "changes": {
    "body": {
      "from": "### Test\r\n\r\nHello!\r\n\r\n> quote\r\n> goes here\r\n\r\n**Bold** _italic_ ~strike~ `code`\r\n\r\n- foo\r\n- bar\r\n\r\n1. One\r\n2. Two\r\n\r\n---\r\n\r\n```js\r\nconsole.log(\"code block\");\r\n```\r\n\r\nMention: @<UserName> \r\n\r\n[Link](http://example.org)"
    }
  },
  "comment": {
    "author_association": "OWNER",
    "body": "### Test\r\n\r\nHello!\r\n\r\n> quote\r\n> goes here\r\n\r\n**Bold** _italic_ ~strike~ `code`\r\n\r\n- foo\r\n- bar\r\n\r\n1. One\r\n2. Two\r\n\r\n---\r\n\r\n```js\r\nconsole.log(\"code block\");\r\n```\r\n\r\nMention: @<UserName> \r\n\r\n[Link](http://example.org)\r\n",
    "created_at": "2020-09-27T19:59:45Z",
    "html_url": "https://github.com/<UserName>/<Repo>/issues/4#issuecomment-987654321",
    "id": 987654321,
    "issue_url": "https://api.github.com/repos/<UserName>/<Repo>/issues/4",
    "node_id": "xYzABcdEc3VlQ29tbWVudDY5OTY4MTE1Ng==",
    "performed_via_github_app": null,
    "updated_at": "2020-09-27T20:57:13Z",
    "url": "https://api.github.com/repos/<UserName>/<Repo>/issues/comments/987654321",
    "user": {
      "avatar_url": "https://avatars3.githubusercontent.com/u/1234567?v=4",
      "events_url": "https://api.github.com/users/<UserName>/events{/privacy}",
      "followers_url": "https://api.github.com/users/<UserName>/followers",
      "following_url": "https://api.github.com/users/<UserName>/following{/other_user}",
      "gists_url": "https://api.github.com/users/<UserName>/gists{/gist_id}",
      "gravatar_id": "",
      "html_url": "https://github.com/<UserName>",
      "id": 1234567,
      "login": "<UserName>",
      "node_id": "xYzABcdEcjc4MTk5OTE=",
      "organizations_url": "https://api.github.com/users/<UserName>/orgs",
      "received_events_url": "https://api.github.com/users/<UserName>/received_events",
      "repos_url": "https://api.github.com/users/<UserName>/repos",
      "site_admin": false,
      "starred_url": "https://api.github.com/users/<UserName>/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/<UserName>/subscriptions",
      "type": "User",
      "url": "https://api.github.com/users/<UserName>"
    }
  },
  "issue": {
    "active_lock_reason": null,
    "assignee": {
      "avatar_url": "https://avatars3.githubusercontent.com/u/1234567?v=4",
      "events_url": "https://api.github.com/users/<UserName>/events{/privacy}",
      "followers_url": "https://api.github.com/users/<UserName>/followers",
      "following_url": "https://api.github.com/users/<UserName>/following{/other_user}",
      "gists_url": "https://api.github.com/users/<UserName>/gists{/gist_id}",
      "gravatar_id": "",
      "html_url": "https://github.com/<UserName>",
      "id": 1234567,
      "login": "<UserName>",
      "node_id": "xYzABcdEcjc4MTk5OTE=",
      "organizations_url": "https://api.github.com/users/<UserName>/orgs",
      "received_events_url": "https://api.github.com/users/<UserName>/received_events",
      "repos_url": "https://api.github.com/users/<UserName>/repos",
      "site_admin": false,
      "starred_url": "https://api.github.com/users/<UserName>/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/<UserName>/subscriptions",
      "type": "User",
      "url": "https://api.github.com/users/<UserName>"
    },
    "assignees": [
      {
        "avatar_url": "https://avatars3.githubusercontent.com/u/1234567?v=4",
        "events_url": "https://api.github.com/users/<UserName>/events{/privacy}",
        "followers_url": "https://api.github.com/users/<UserName>/followers",
        "following_url": "https://api.github.com/users/<UserName>/following{/other_user}",
        "gists_url": "https://api.github.com/users/<UserName>/gists{/gist_id}",
        "gravatar_id": "",
        "html_url": "https://github.com/<UserName>",
        "id": 1234567,
        "login": "<UserName>",
        "node_id": "xYzABcdEcjc4MTk5OTE=",
        "organizations_url": "https://api.github.com/users/<UserName>/orgs",
        "received_events_url": "https://api.github.com/users/<UserName>/received_events",
        "repos_url": "https://api.github.com/users/<UserName>/repos",
        "site_admin": false,
        "starred_url": "https://api.github.com/users/<UserName>/starred{/owner}{/repo}",
        "subscriptions_url": "https://api.github.com/users/<UserName>/subscriptions",
        "type": "User",
        "url": "https://api.github.com/users/<UserName>"
      }
    ],
    "author_association": "OWNER",
    "body": "`issue_comment` event really just triggers for comments, no the initial post.",
    "closed_at": null,
    "comments": 1,
    "comments_url": "https://api.github.com/repos/<UserName>/<Repo>/issues/4/comments",
    "created_at": "2020-09-27T18:10:01Z",
    "events_url": "https://api.github.com/repos/<UserName>/<Repo>/issues/4/events",
    "html_url": "https://github.com/<UserName>/<Repo>/issues/4",
    "id": 709787574,
    "labels": [
      {
        "color": "e4e669",
        "default": true,
        "description": "This doesn't seem right",
        "id": 2067629346,
        "name": "invalid",
        "node_id": "xYzABcdEZWwyMDY3NjI5MzQ2",
        "url": "https://api.github.com/repos/<UserName>/<Repo>/labels/invalid"
      }
    ],
    "labels_url": "https://api.github.com/repos/<UserName>/<Repo>/issues/4/labels{/name}",
    "locked": false,
    "milestone": null,
    "node_id": "xYzABcdEdWU3MDk3ODc1NzQ=",
    "number": 4,
    "performed_via_github_app": null,
    "repository_url": "https://api.github.com/repos/<UserName>/<Repo>",
    "state": "open",
    "title": "Issue comment action test",
    "updated_at": "2020-09-27T20:57:13Z",
    "url": "https://api.github.com/repos/<UserName>/<Repo>/issues/4",
    "user": {
      "avatar_url": "https://avatars3.githubusercontent.com/u/1234567?v=4",
      "events_url": "https://api.github.com/users/<UserName>/events{/privacy}",
      "followers_url": "https://api.github.com/users/<UserName>/followers",
      "following_url": "https://api.github.com/users/<UserName>/following{/other_user}",
      "gists_url": "https://api.github.com/users/<UserName>/gists{/gist_id}",
      "gravatar_id": "",
      "html_url": "https://github.com/<UserName>",
      "id": 1234567,
      "login": "<UserName>",
      "node_id": "xYzABcdEcjc4MTk5OTE=",
      "organizations_url": "https://api.github.com/users/<UserName>/orgs",
      "received_events_url": "https://api.github.com/users/<UserName>/received_events",
      "repos_url": "https://api.github.com/users/<UserName>/repos",
      "site_admin": false,
      "starred_url": "https://api.github.com/users/<UserName>/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/<UserName>/subscriptions",
      "type": "User",
      "url": "https://api.github.com/users/<UserName>"
    }
  },
  "repository": {
    "archive_url": "https://api.github.com/repos/<UserName>/<Repo>/{archive_format}{/ref}",
    "archived": false,
    "assignees_url": "https://api.github.com/repos/<UserName>/<Repo>/assignees{/user}",
    "blobs_url": "https://api.github.com/repos/<UserName>/<Repo>/git/blobs{/sha}",
    "branches_url": "https://api.github.com/repos/<UserName>/<Repo>/branches{/branch}",
    "clone_url": "https://github.com/<UserName>/<Repo>.git",
    "collaborators_url": "https://api.github.com/repos/<UserName>/<Repo>/collaborators{/collaborator}",
    "comments_url": "https://api.github.com/repos/<UserName>/<Repo>/comments{/number}",
    "commits_url": "https://api.github.com/repos/<UserName>/<Repo>/commits{/sha}",
    "compare_url": "https://api.github.com/repos/<UserName>/<Repo>/compare/{base}...{head}",
    "contents_url": "https://api.github.com/repos/<UserName>/<Repo>/contents/{+path}",
    "contributors_url": "https://api.github.com/repos/<UserName>/<Repo>/contributors",
    "created_at": "2020-05-17T15:08:58Z",
    "default_branch": "master",
    "deployments_url": "https://api.github.com/repos/<UserName>/<Repo>/deployments",
    "description": null,
    "disabled": false,
    "downloads_url": "https://api.github.com/repos/<UserName>/<Repo>/downloads",
    "events_url": "https://api.github.com/repos/<UserName>/<Repo>/events",
    "fork": false,
    "forks": 0,
    "forks_count": 0,
    "forks_url": "https://api.github.com/repos/<UserName>/<Repo>/forks",
    "full_name": "<UserName>/<Repo>",
    "git_commits_url": "https://api.github.com/repos/<UserName>/<Repo>/git/commits{/sha}",
    "git_refs_url": "https://api.github.com/repos/<UserName>/<Repo>/git/refs{/sha}",
    "git_tags_url": "https://api.github.com/repos/<UserName>/<Repo>/git/tags{/sha}",
    "git_url": "git://github.com/<UserName>/<Repo>.git",
    "has_downloads": true,
    "has_issues": true,
    "has_pages": false,
    "has_projects": true,
    "has_wiki": true,
    "homepage": null,
    "hooks_url": "https://api.github.com/repos/<UserName>/<Repo>/hooks",
    "html_url": "https://github.com/<UserName>/<Repo>",
    "id": 264693846,
    "issue_comment_url": "https://api.github.com/repos/<UserName>/<Repo>/issues/comments{/number}",
    "issue_events_url": "https://api.github.com/repos/<UserName>/<Repo>/issues/events{/number}",
    "issues_url": "https://api.github.com/repos/<UserName>/<Repo>/issues{/number}",
    "keys_url": "https://api.github.com/repos/<UserName>/<Repo>/keys{/key_id}",
    "labels_url": "https://api.github.com/repos/<UserName>/<Repo>/labels{/name}",
    "language": "Shell",
    "languages_url": "https://api.github.com/repos/<UserName>/<Repo>/languages",
    "license": null,
    "merges_url": "https://api.github.com/repos/<UserName>/<Repo>/merges",
    "milestones_url": "https://api.github.com/repos/<UserName>/<Repo>/milestones{/number}",
    "mirror_url": null,
    "name": "<Repo>",
    "node_id": "xYzABcdEcG9zaXRvcnkyNjQ2OTM4NDY=",
    "notifications_url": "https://api.github.com/repos/<UserName>/<Repo>/notifications{?since,all,participating}",
    "open_issues": 1,
    "open_issues_count": 1,
    "owner": {
      "avatar_url": "https://avatars3.githubusercontent.com/u/1234567?v=4",
      "events_url": "https://api.github.com/users/<UserName>/events{/privacy}",
      "followers_url": "https://api.github.com/users/<UserName>/followers",
      "following_url": "https://api.github.com/users/<UserName>/following{/other_user}",
      "gists_url": "https://api.github.com/users/<UserName>/gists{/gist_id}",
      "gravatar_id": "",
      "html_url": "https://github.com/<UserName>",
      "id": 1234567,
      "login": "<UserName>",
      "node_id": "xYzABcdEcjc4MTk5OTE=",
      "organizations_url": "https://api.github.com/users/<UserName>/orgs",
      "received_events_url": "https://api.github.com/users/<UserName>/received_events",
      "repos_url": "https://api.github.com/users/<UserName>/repos",
      "site_admin": false,
      "starred_url": "https://api.github.com/users/<UserName>/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/<UserName>/subscriptions",
      "type": "User",
      "url": "https://api.github.com/users/<UserName>"
    },
    "private": false,
    "pulls_url": "https://api.github.com/repos/<UserName>/<Repo>/pulls{/number}",
    "pushed_at": "2020-09-27T20:57:01Z",
    "releases_url": "https://api.github.com/repos/<UserName>/<Repo>/releases{/id}",
    "size": 88,
    "ssh_url": "git@github.com:<UserName>/<Repo>.git",
    "stargazers_count": 0,
    "stargazers_url": "https://api.github.com/repos/<UserName>/<Repo>/stargazers",
    "statuses_url": "https://api.github.com/repos/<UserName>/<Repo>/statuses/{sha}",
    "subscribers_url": "https://api.github.com/repos/<UserName>/<Repo>/subscribers",
    "subscription_url": "https://api.github.com/repos/<UserName>/<Repo>/subscription",
    "svn_url": "https://github.com/<UserName>/<Repo>",
    "tags_url": "https://api.github.com/repos/<UserName>/<Repo>/tags",
    "teams_url": "https://api.github.com/repos/<UserName>/<Repo>/teams",
    "trees_url": "https://api.github.com/repos/<UserName>/<Repo>/git/trees{/sha}",
    "updated_at": "2020-09-27T20:57:03Z",
    "url": "https://api.github.com/repos/<UserName>/<Repo>",
    "watchers": 0,
    "watchers_count": 0
  },
  "sender": {
    "avatar_url": "https://avatars3.githubusercontent.com/u/1234567?v=4",
    "events_url": "https://api.github.com/users/<UserName>/events{/privacy}",
    "followers_url": "https://api.github.com/users/<UserName>/followers",
    "following_url": "https://api.github.com/users/<UserName>/following{/other_user}",
    "gists_url": "https://api.github.com/users/<UserName>/gists{/gist_id}",
    "gravatar_id": "",
    "html_url": "https://github.com/<UserName>",
    "id": 1234567,
    "login": "<UserName>",
    "node_id": "xYzABcdEcjc4MTk5OTE=",
    "organizations_url": "https://api.github.com/users/<UserName>/orgs",
    "received_events_url": "https://api.github.com/users/<UserName>/received_events",
    "repos_url": "https://api.github.com/users/<UserName>/repos",
    "site_admin": false,
    "starred_url": "https://api.github.com/users/<UserName>/starred{/owner}{/repo}",
    "subscriptions_url": "https://api.github.com/users/<UserName>/subscriptions",
    "type": "User",
    "url": "https://api.github.com/users/<UserName>"
  }
}
2 Likes