In today's fast-paced development environment, automating workflows is crucial for maintaining efficiency and ensuring consistency across projects. GitHub Actions provides a powerful platform to automate your software development workflows directly in your repository. This guide will walk you through the process of setting up and using GitHub Actions to automate various tasks such as building, testing, and deploying your applications.
Before diving into GitHub Actions, ensure you have the following:
GitHub Actions is a continuous integration and continuous deployment (CI/CD) platform that allows you to automate your software workflows. It enables you to build, test, package, release, and deploy code directly from repositories on GitHub. With GitHub Actions, you can create custom workflows that are triggered by events like pushing code, creating issues, or scheduling tasks.
Workflows in GitHub Actions are defined by YAML files located in the .github/workflows directory of your repository. Let's create a simple workflow to run tests on every push to the main branch.
This will open a new file in the .github/workflows directory with a default YAML template. Replace the content with the following:
name: Test Workflow
on:
push:
branches:
- main
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '14'
- name: Install dependencies
run: npm install
- name: Run tests
run: npm test
main branch.actions/checkout@v2 action to check out your repository.actions/setup-node@v2 action.npm install to install project dependencies.npm test to run your tests.Commit the workflow file to your repository:
git add .github/workflows/test.yml
git commit -m "Add GitHub Actions test workflow"
git push origin main
After pushing the changes, navigate back to the "Actions" tab in your repository. You should see your new workflow listed under "Workflows." Click on it to view the details and monitor the progress of the job.
Let's create a workflow that builds a Docker image and pushes it to Docker Hub.
name: Build and Push Docker Image
on:
push:
branches:
- main
jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Log in to Docker Hub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Build and push Docker image
uses: docker/build-push-action@v2
with:
context: .
push: true
tags: yourusername/yourimage:${{ github.sha }}
To deploy an application to a server, you can use SSH keys and the ssh-agent action.
name: Deploy to Server
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up SSH keys
uses: webfactory/ssh-agent@v0.5.4
with:
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
- name: Deploy to server
run: |
scp -r . user@yourserver:/path/to/deploy
GitHub Actions provides a powerful and flexible way to automate your software development workflows. By following this guide, you should have a solid understanding of how to set up and use GitHub Actions to build, test, deploy, and more. As you become more familiar with GitHub Actions, explore the extensive marketplace of actions available to extend its capabilities even further.
By leveraging GitHub Actions, you can streamline your development process, reduce manual errors, and focus more on writing code and delivering value to your users.