How to allow GitHub Access Token to create and update GitHub Workflow

How to allow GitHub Access Token to create and update GitHub Workflow

github_workflow_scope_access

Here in this article we will see how we can create GitHub Actions Workflow for a GitHub repository. We will also see how we can allow our GitHub Personal Access Token access to create and update Workflows in our Github repository.

Test Environment

Ubuntu 22.04

What is GitHub Actions

GitHub Actions makes it easy to automate every step of a CICD pipeline also can be called as GitHub Workflow to build, package, deploy and test your software projects right from GitHub. GitHub Workflows can be triggered based on any GitHub event like push, pull request or a new release. These workflows are nothing but a combination of GitHub Actions for the services that we use which are build and maintained by the GitHub community.

GitHub Actions can help you automate nearly every aspect of your application development processes.

What is GitHub Workflow

GitHub Workflow contains one or more jobs which can run in sequential order or in parallel. Each job consists of multiple steps which are executed within a GitHub runner which is a isolated environment. This isolated environment can be a virtual machine or container. Each step within a job runs a script or an action, which are reusable extensions that can simplify the workflow. GitHub Workflows are defined using YAML definition.

If you are interested in watching the video. Here is the YouTube video on the same step by step procedure outlined below.

Procedure

Step1: Clone Repository

As a first we are cloning a sample GitHub repository from my GitHub Account. You can fork this repository or create your own repository and use it for this activity.

[admin@fedser github_space]$ git clone https://github.com/novicejava1/learngit.git

Step2: Create a Workflow

GitHub Workflow can contains one or more jobs which can run in sequential order or in parallel. Each job will run inside its own virtual machine runner, or inside a container, and has one or more steps that either run a script that you define or run an action, which is a reusable extension that can simplify your workflow.

Contexts are a way to access information about workflow runs, variables, runner environments, jobs, and steps. Each context is an object that contains properties, which can be strings or other objects. We can access context information in workflows and actions.

There are a couple of contexts that we have used in this workflow which help us in getting the information in the workflow. You can get more details about these in GitHub Contexts.

  • github.actor
  • github.event_name
  • runner.os
  • github.ref
  • github.repository
  • github.workspace
  • job.status
[admin@fedser learngit]$ cat .github/workflows/gitworkflow.yml 
name: GitHub Actions Demo
run-name: ${{ github.actor }} is testing out GitHub Actions 🚀
on: [push]
jobs:
  Explore-GitHub-Actions:
    runs-on: ubuntu-latest
    steps:
      - run: echo "🎉 The job was automatically triggered by a ${{ github.event_name }} event."
      - run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by GitHub!"
      - run: echo "🔎 The name of your branch is ${{ github.ref }} and your repository is ${{ github.repository }}."
      - name: Check out repository code
        uses: actions/checkout@v3
      - run: echo "💡 The ${{ github.repository }} repository has been cloned to the runner."
      - run: echo "🖥️ The workflow is now ready to test your code on the runner."
      - name: List files in the repository
        run: |
          ls ${{ github.workspace }}
      - run: echo "🍏 This job's status is ${{ job.status }}."

Step3: Push Changes

Once our workflow is ready we can push the changes to our GitHub repository as shown below.

[admin@fedser learngit]$ git add .

[admin@fedser learngit]$ git commit -m "create and push a workflow"

[admin@fedser learngit]$ git push -u origin main

If you encounter below error while pushing your workflow. You need to update your access token settings as shown in next step.

Error creating workflow

! [remote rejected] main -> main (refusing to allow a Personal Access Token to create or update workflow `.github/workflows/gitworkflow.yml` without `workflow` scope)
error: failed to push some refs to 'https://github.com/novicejava1/learngit.git'

Step4: Update Personal Access Token Scope

Go to – Profile – Settings – Developer Settings – Personal Access Token – Tokens. Click on your Token that you use to authenticate with GitHub.

Provide your token with the “workflow” scope and click on the Update Token button below.

Step5: Re Push Workflow

Once the updates are done to your access token you can try to repush your changes.

[admin@fedser learngit]$ git add .

[admin@fedser learngit]$ git commit -m "create and push a workflow"

[admin@fedser learngit]$ git push -u origin main

Step6: Validate Workflow

Here is the screenshot of the successfully executed workflow which gets triggered when we push changes to our repository.

Hope you enjoyed reading this article. Thank you..