GitHub Actions are used to be a part of CI/CD. They are triggered by so called events, like pushing or pulling to/from a GitHub repository. No third-party tool is needed, when your code is already on GitHub. The setup is quite easy and it's a nice tool for developers.
GitHub event examples:
- Pull request
- Contributor joined
- Issue created
- Pull request merged
- New push
GitHub Actions listen to those events and respond with a workflow. Examples:
- Sort, label, assign, reproduce a newly submitted issue
- After a new commit was pushed: Test, build, push, deploy to server
Different Pipeline examples:
- Node.js App -> Build a Docker Image -> Push to a repository -> Deploy to a production server
- Java App -> Integration Tests Linux/Windows -> Build Docker Image -> Push to AWS repository -> Deploy to AWS EKS
Instead of manually configuring everything of this. We can now define the environment, like:
- Node and Docker
- With a specific version
- Connected to this specific target and push the code there
GitHub offers workflow templates that are ready to use. In a repository, navigate to "Actions" and click on "New Workflow". Pick from the categories:
- Deployment
- Continuous integration
- Automation
- Pages
When selecting a workflow, GitHub automatically creates the .github/workflows/name.yml directory & file and provides to .yml code within. For example, the .yml code of "Deploy a container to an Azure Web App" looks like this:
name: Build and deploy a container to an Azure Web App env: AZURE_WEBAPP_NAME: your-app-name # set this to the name of your Azure Web App on: push: branches: [ "main" ] workflow_dispatch: permissions: contents: read jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3
-
name: Set up Docker Buildx uses: docker/setup-buildx-action@v1
-
name: Log in to GitHub container registry uses: docker/login-action@v1.10.0 with: registry: ghcr.io username: ${{ github.actor }} password: ${{ github.token }}
-
name: Lowercase the repo name and username run: echo "REPO=${GITHUB_REPOSITORY,,}" >>${GITHUB_ENV}
-
name: Build and push container image to registry uses: docker/build-push-action@v2 with: push: true tags: ghcr.io/${{ env.REPO }}:${{ github.sha }} file: ./Dockerfile
deploy: permissions: contents: none runs-on: ubuntu-latest needs: build environment: name: 'Development' url: ${{ steps.deploy-to-webapp.outputs.webapp-url }} steps:
-
name: Lowercase the repo name and username run: echo "REPO=${GITHUB_REPOSITORY,,}" >>${GITHUB_ENV}
-
name: Deploy to Azure Web App id: deploy-to-webapp uses: azure/webapps-deploy@v2 with: app-name: ${{ env.AZURE_WEBAPP_NAME }} publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }} images: 'ghcr.io/${{ env.REPO }}:${{ github.sha }}'