CI/CD GitHub Action ๐
A CI/CD workflow for your maven project!
Getting Started
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 the
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.
Prepare the production server
-
Connect to your server
ssh -i <private key path> <user>@<host>
-
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_USER
andMYSQL_PASSWORD
can be anything.DOCKER_IMAGE_NAME
must match the name of the docker image defined in workflow.GITHUB_USER
must be the user/organisation of the repository in lower case.
This script replaces step 3-7 (optional)
bash <(curl -s https://raw.githubusercontent.com/MathiasReker/CI-CD/develop/install.bash)
-
Update and upgrade packages
sudo apt-get update && sudo apt-get -y upgrade
-
Install docker-compose
sudo apt-get install -y docker-compose
-
Generate a new key named
github-actions
with an empty passwordssh-keygen -t rsa -b 4096 -f ~/.ssh/github-actions -q -P ""
-
Copy the content of
github-actions
intoauthorized_keys
cat ~/.ssh/github-actions.pub >> ~/.ssh/authorized_keys
-
Grap the private key, as you will need it soon
cat ~/.ssh/github-actions
Install action secrets
Navigate to your GitHub repository. Go to settings -> secrets -> actions.
Add the following secrets:
- SSH_USER
- SSH_HOST
- SSH_PRIVATE_KEY
Install the workflow to your project
-
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.
Depending on your project, it is necessary to adapt the docker-compose.yml
and the Dockerfile
.
Package visibility
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.
Usage
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.
Roadmap
- CI workflow
- CD workflow
- Unit test
- Integration test using JPA
See the open issues for a complete list of proposed features (and known issues).
Contributing
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
License
It is distributed under the MIT License. See LICENSE
for more information.