Working with external actions - GitHub Actions (Part 3)
Tags: github, devops, beginners
Published: 2020-12-01 20:06:34

👉 Prerequisites 

If you are new to GitHub Actions, I suggest you to read these articles
* [Introduction to Github Actions - GitHub Actions (Part 1)](https://dev.to/mihinduranasinghe/introduction-to-github-actions-part-1-1d6p)

* [Working with environment variables - GitHub Actions(Part 2)](https://dev.to/mihinduranasinghe/working-with-environment-variables-github-actions-part-2-46po)

---

### Here is the cool thing about GitHub actions.

**Instead of automating each and every step by your own from the scratch , you can use reusable codes in Github Actions.**

Mainly there are two types of reusable actions. 

* We can create private reusable actions and use them inside our local workflows.

* We can use public reusable actions from the marketplace

**In this article,** let's have a look at how we can use publicly available external actions from the *marketplace* and how we can use them inside our workflow.

#### GitHub Actions Marketplace

GitHub Actions Marketplace is where you can find out reusable codes/workflows also known as "actions" implemented by someone in the GitHub Actions community.

<i>Most of the reputed cloud companies like AWS, Google, Azure have published their own public actions for their clients.</i>

Not only companies, also the individuals in the Github Actions Community have build up and published some useful actions in the Marketplace

Link : [Marketplace](https://github.com/marketplace?type=actions)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/0wg9fy113zoqvt712nbi.JPG)

---

* You can search for actions in the search bar of the Marketplace.

* Once you click on an action, you will be redirected to a readme(documentation) page and there you can find a brief about the action and how we can use it in our workflow and what is the **reference(signature)** of the particular workflow and what are the **inputs** we have to give when we use the action in our workflow. 

## How to reference a public action ?

There are several ways that we can reference a public action from the marketplace.

1. Referencing a branch
2. Referencing a version
3. Referencing a commit

Here is the basic structure of a step to use a public action :

```yml
steps:
 - name: Any-relevant-name-for-the-step
   id: step-1-id(Optional)
   uses: #reference
   with: #inputs

```

We have to provide a reference with the "uses:" tag and we can provide relevant inputs under "with:" tag.

* <u>Referencing a branch:</u>

```yml

uses: publisher-username/repo-name@branch-name

```

* <u>Referencing a version:</u>

Find the releases from the marketplace for the particular action

```yml

uses: publisher-username/repo-name@v1.1.0

```

* <u>Referencing a commit ID :</u>

Explore the commit history of the action and find a specific commit ID

```yml

uses: publisher-username/repo-name@commit-ID

```

#### Note :

* If you reference a branch, actually **it is not really preferred**. Some breaking changes might occur when the publisher changes the code in the particular branch you referred.  Then your workflow might fail.

*  If you reference a specific Version, It will help to mitigate this problem.

* **The safest way** is to have a stable workflow is to use specific **commit ID** to reference an action.

---

### An easier way to find the reference of an action

* Go to the action page in the marketplace.
* Click on this green color box or drop down and find the specific version.
* copy paste the reference. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/eojo0al0mypw94pl1zas.JPG)

---

#### For example lets use this [Hello world javascript action](https://github.com/actions/hello-world-javascript-action)

If you have read the documentation, there are few inputs you can provide.

I provide here the "who-to-greet" input with a sample name.

```yml

name: name-of-the-workflow

on: [push]

jobs:
    sample-job-1:
        runs-on: ubuntu-latest
        steps:
            - name: step-1
              uses: actions/hello-world-javascript-action@v1.1
              with:
                 who-to-greet: Mihindu


```

Output :

```
Hello Mihindu!

```

---

#### Another useful action for any workflow.

Before working with the repo files from the workflow steps, we need to clone our repo files into the virtual machine that we created in the job. 

So this is something we do at very first of many workflows. So that you can use this [checkout](https://github.com/marketplace/actions/checkout) public action and clone repo files into VM very easily.

```yml

name: name-of-the-workflow

on: [push]

jobs:
    sample-job-1:
        runs-on: ubuntu-latest
        steps:
            - name: Clone-repo-files-into-vm
              uses: actions/checkout@v2.3.4

            - run: ls


```

There are many more useful actions in the marketplace. Take your time and refer them and use them in your workflows according to the purpose.

👉 What's Next?

  [Using Docker Containers In Jobs - GitHub Actions (Part 4)](https://dev.to/mihinduranasinghe/using-docker-containers-in-jobs-github-actions-3eof)


##**Thank You**

##### ***Hope you all enjoyed and learned something from this. Let me know your comments and suggestions in the discussion section***.

👉 Visit me - [https://mihinduranasinghe.com/](https://mihinduranasinghe.com/)