This is an example of building a CI & CD pipeline using GitHub actions.
The CI pipeline depends on maven to compile and test the project's code. JPA/MySQL is supported.
The CD pipeline will create a docker image of the project's code and deploy the image as a docker container along with a MySQL container on a remote server using SSH. The bridge driver of docker networks connects these containers.
In the docker-compose.yml, you can configure the tag of the images. By default, the tag of the project is dev.
However, you can change the tag to a specific version to control the deployment. The workflow supports tags in
sem-version format like v*.*.*.
To create a new release using a specific tag navigate to your GitHub repository. Go to tags -> create new release -> publish the release.
-
Connect to your server
ssh -i <private key path> <user>@<host>
-
Update and upgrade packages
sudo apt-get update && sudo apt-get -y upgrade -
Install docker-compose
sudo apt install -y docker-compose
-
Add environment variables
echo "MYSQL_ROOT_PASSWORD=myrootpw MYSQL_DATABASE=mydb MYSQL_USER=myuser MYSQL_PASSWORD=mypw DOCKER_IMAGE_NAME=ci-cd GITHUB_USER=mathiasreker" >".env"
MYSQL_ROOT_PASSWORD,MYSQL_DATABASE,MYSQL_USERandMYSQL_PASSWORDcan be anything.DOCKER_IMAGE_NAMEmust match the name of the docker image defined in workflow.GITHUB_USERmust be the user/organisation of the repository in lower case. -
Generate a new key named
github-actionswith empty passwordcd ~/.ssh ssh-keygen -t rsa -b 4096 cat github-actions.pub >> authorized_keys
Navigate to your GitHub repository. Go to settings -> secrets -> actions.
Add the following secrets:
- SSH_USER
- SSH_HOST
- SSH_PRIVATE_KEY
-
Copy the workflow to this path of your repository:
/.github/workflows/ci-cd.yml. -
Copy the docker-compose.yml to the root folder of your project.
-
Copy the Dockerfile to the root folder of your project.
It is necessary to adapt the docker-compose.yml and the Dockerfile depending on your project.
To see the package, the CD pipeline must run at least once. The pipeline will create a package linked to your GitHub repository.
Navigate to your GitHub repository. Go to the package -> select package settings -> select danger zone -> change visibility -> make it public.
The CI pipeline is triggered on any change to the code on your repository.
The CD pipeline is triggered on push events. The CD pipeline will be skipped if the CI pipeline fails.
- CI workflow
- CD workflow
- Unit test
- Integration test using JPA
See the open issues for a complete list of proposed features (and known issues).
If you have a suggestion to improve this, please fork the repo and create a pull request. You can also open an issue with the tag "enhancement". Finally, don't forget to give the project a star! Thanks again!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature) - Commit your Changes (
git commit -m 'Add some AmazingFeature') - Push to the Branch (
git push origin feature/AmazingFeature) - Open a Pull Request
It is distributed under the MIT License. See LICENSE for more information.