This is the sample app for GitHub Actions Presentation.
- Please fork this repository and practice on it.
- You must enable GitHub Actions on the forked-repository.
- You just click
I understand my workflows...
button. If not, the Workflows may not run.
There is an workflow located at .github/workflows/hello-world.yaml
.
Let's add a new step into the Job named hello
. It should execute the following command:
cat requirements.txt
- In the
main
branch, open.github/workflows/hello-world.yaml
then click the edit button: - Add new step into the job
hello
:
jobs:
hello:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "Hello, World!"
- run: cat requirements.txt
- Commit to forked-repository
- Next, open
Actions
tab in GitHub. You will see the workflowhello-world
. - Finally, make sure that you see the new step completed successfully:
Now, let's create a new workflow django.yaml
and perform Continuous Integration (CI) for the app.
- Open your repository page in GitHub
- Click on
Actions
tab - Click on
New workflow
button in the left column - Next, search workflow templates using keyword
Django
- In the search results, you will see the Django workflow template. Click on
Configure
button. - Now, set the python-version to v3.10:
matrix:
python-version: ["3.10"]
- Commit and make sure that the workflow is running
In this lab, the hello
job should print Hello, $NAME!
instead of Hello, World!
.
The $NAME
is an user-defined repository secret variable that will be injected by GitHub Actions.
- Open
Settings
page - Open
Secrets and variables
>Actions
- Add your repository secrets
- Change the step:
jobs:
hello:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- env:
NAME: ${{ secrets.NAME }}
run: echo "Hello, $NAME!"
There is a Dockerfile
file in this repository. It will be used to build the Docker image for this app.
Let's create a new workflow for automate-building the Docker images in GitHub Actions and push them to GitHub Container Registry.
Requirements:
- The workflow should only trigger when having the
push
andpull_request
event on themain
branch only. - The workflow shouldn't push the images to the registry server during pull request.
- The workflow should push the image with tags:
latest
andbuild-${{ vars.GITHUB_RUN_NUMBER }}
.
- We use an Action Build and push Docker images in GitHub Marketplace
- Scrolling down to the
Git context
section and copy the sample workflow YAML - Prepare your Docker repository secret:
REGISTRY_SERVER=ghcr.io
- Create new workflow
docker.yaml
file, paste the sample workflow. - First, let's modify the event triggers. In this case, we want to trigger this workflow when having new PR/push to the
main
branch (onlymain
branch).
on:
push:
branches:
- 'main'
pull_request:
branches:
- 'main'
- Next, make the login step use the registry server by reading Secrets:
- name: Login to the container registry server
uses: docker/login-action@v3
with:
registry: ${{ secrets.REGISTRY_SERVER }} # User-defined repository secret variable. Value: ghcr.io
username: ${{ github.repository_owner }} # Pre-defined variable by GitHub Actions
password: ${{ secrets.GITHUB_TOKEN }} # Pre-defined variable by GitHub Actions
- Add some args for the
Build and push
step
- name: Build and push
uses: docker/build-push-action@v5
with:
push: ${{ github.event_name == 'push' }} # Only push Docker images to the registry server on the push event
tags: |-
${{ secrets.REGISTRY_SERVER }}/${{ github.repository }}:latest
${{ secrets.REGISTRY_SERVER }}/${{ github.repository }}:build-${{ vars.GITHUB_RUN_NUMBER }}
- Check the workflow logs
In this lab, we will setup continuous deployment for project using SSH key.
Requirements:
- Add new job named
deploy
. It should exec the deploy script. - The
deploy
job will be trigger after the docker built/pushed successfully. If Docker CI failed, the deploy job shouldn't run. - The
deploy
job shouldn't trigger during pull request.
Prepare:
- Generate a new SSH Key and add the public key to file
~/.ssh/authorized_keys
- Make sure you can open SSH connection from your local to the server
- Next, prepare some secret variables for your repository:
SSH_PRIVATE_KEY
: The SSH private key (PEM format)SSH_SERVER_IP
: The IP address of the serverSSH_USER
: The username will be used to authenticate with SSH server
- Now, let's add the deploy job:
deploy:
if: github.event_name == 'push'
runs-on: ubuntu-latest
needs: docker
env:
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
SSH_SERVER_IP: ${{ secrets.SSH_SERVER_IP }}
SSH_USER: ${{ secrets.SSH_USER }}
DOCKER_IMAGE: ${{ secrets.REGISTRY_SERVER }}/${{ github.repository }}:build-${{ vars.GITHUB_RUN_NUMBER }}
steps:
- name: Run ssh-agent in background
run: eval "$(ssh-agent -s)"
- name: Add SSH Key
run: |-
echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
ssh-add -l
- name: Run deploy script
run: |-
ssh $SSH_USER@$SSH_SERVER_IP -t "docker pull $DOCKER_IMAGE"
ssh $SSH_USER@$SSH_SERVER_IP -t "DOCKER_IMAGE=$DOCKER_IMAGE docker-compose up -d"
- Commit and check the workflow logs